Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在期待之后开玩笑不是';我没打电话_Javascript_Testing_Jestjs_Babel Jest - Fatal编程技术网

Javascript 在期待之后开玩笑不是';我没打电话

Javascript 在期待之后开玩笑不是';我没打电话,javascript,testing,jestjs,babel-jest,Javascript,Testing,Jestjs,Babel Jest,我的玩笑并没有像我预料的那样奏效。见: const res = { send: (content) => { expect(content).toEqual({ app_status: 501, errors: { jwt: { location: 'body',

我的玩笑并没有像我预料的那样奏效。见:

const res = {
        send: (content) => {

            expect(content).toEqual({
                app_status: 501,
                errors: {
                    jwt: {
                        location: 'body',
                        param: 'jwt',
                        value: undefined,
                        msg: 'The jwt is required'
                    }
                }
            });

            console.log("after expect");

            done();

        },
};

基本上,
expect(content)之后的所有内容。
res.send
中的toEqual…
不会被调用。我觉得这很令人困惑。我没有收到任何错误,除了我的测试花费的时间太长(因为
done
)没有被调用,测试也没有“关闭”。所以我的问题是,我是否明显遗漏了什么?

下面的方法应该可以。我添加了异步,因为可以异步调用
send

const createResponse = () => {
  var resolve;
  const p = new Promise(
    (r,reject)=>resolve=r
  );
  return [
    {
      send: (value) => {
        resolve(value)
      }
    },
    p
  ];
};

test('(async) fail', done => {
  //Router
  const router = express.Router();
  //Endpoint to fetch version
  router.get('/api/version', (req, res) => {
    setTimeout(x=>res.send('v1'),10)
  });
  const request = {
    method: 'GET'
  };
  let [response,p] = createResponse()
  router.stack[0].handle(request, response, () => {});
  p.then(
    x=>expect(x).toBe('v2'),
    reject=>expect("should not reject").toBe(reject)
  ).then(
    x=>done()
    //,x=>done() //this will cause all tests to pass
  );
});
在评论中回答您的问题;考虑下面的代码:

const express = require('express');

const router = express.Router();
//Endpoint to fetch version
router.get('/api/version', (req, res) => {
  res.send("Hello World");
});
const request = {
  method: 'GET'
};
const response = {
  send: (value) => {
    debugger;//pause here and see the stack
    throw new Error("Hello Error.");
  }
};
router.stack[0].handle(
  request,
  response, 
  //this is the function done in route.js:
  // https://github.com/expressjs/express/blob/master/lib/router/route.js#L127
  err => {
    console.error(err.message);//console errors Hello Error.
  }
);
Send抛出一个错误,但是Send由您的mock调用,而mock由express调用。所以express捕获异常,然后结束(
done
不是来自just,而是您的回调)

因此,它会用一个错误调用您的回调,从jist中跳过
done
,并且不会抛出任何内容(可能在日志中显示某些内容)。因为你的回调没有做任何事情,所以它超时了

您可以尝试在回调中从jist调用
done
(在
console.error(err.message);

[更新]

仔细尝试捕捉
expect
引发的错误,以下内容将告诉我1个测试已通过:

test('(async) fail', done => {
  try{
    expect(true).toBe(false);
  }catch(e){

  }
  done();
});

以下方法应该可以很好地工作。我添加了异步,因为可以异步调用
send

const createResponse = () => {
  var resolve;
  const p = new Promise(
    (r,reject)=>resolve=r
  );
  return [
    {
      send: (value) => {
        resolve(value)
      }
    },
    p
  ];
};

test('(async) fail', done => {
  //Router
  const router = express.Router();
  //Endpoint to fetch version
  router.get('/api/version', (req, res) => {
    setTimeout(x=>res.send('v1'),10)
  });
  const request = {
    method: 'GET'
  };
  let [response,p] = createResponse()
  router.stack[0].handle(request, response, () => {});
  p.then(
    x=>expect(x).toBe('v2'),
    reject=>expect("should not reject").toBe(reject)
  ).then(
    x=>done()
    //,x=>done() //this will cause all tests to pass
  );
});
在评论中回答您的问题;考虑下面的代码:

const express = require('express');

const router = express.Router();
//Endpoint to fetch version
router.get('/api/version', (req, res) => {
  res.send("Hello World");
});
const request = {
  method: 'GET'
};
const response = {
  send: (value) => {
    debugger;//pause here and see the stack
    throw new Error("Hello Error.");
  }
};
router.stack[0].handle(
  request,
  response, 
  //this is the function done in route.js:
  // https://github.com/expressjs/express/blob/master/lib/router/route.js#L127
  err => {
    console.error(err.message);//console errors Hello Error.
  }
);
Send抛出一个错误,但是Send由您的mock调用,而mock由express调用。所以express捕获异常,然后结束(
done
不是来自just,而是您的回调)

因此,它会用一个错误调用您的回调,从jist中跳过
done
,并且不会抛出任何内容(可能在日志中显示某些内容)。因为你的回调没有做任何事情,所以它超时了

您可以尝试在回调中从jist调用
done
(在
console.error(err.message);

[更新]

仔细尝试捕捉
expect
引发的错误,以下内容将告诉我1个测试已通过:

test('(async) fail', done => {
  try{
    expect(true).toBe(false);
  }catch(e){

  }
  done();
});


您确定调用了
res.send
?我没有看到任何代码这样做。您可以尝试使用一些
console.log
语句调试代码,看看它们是否出现,或者只是使用
debugger
语句,看看当它在那里中断时会发生什么:是的,调用它。我已经通过在expect之前和之后记录内容对其进行了测试。似乎expect正在冻结或阻止进一步的执行。如果您将expect更改为:
expect(true)。toEqual(true)
,然后故意以
expect(true)失败。toEqual(false)
?如果执行了
expect(true)。toBeTruthy()
,则可以。所以我猜一个“failed”
expect
会停止进一步的执行,这就是为什么永远不会调用
done()
。遗憾的是,错误没有显示出来,只是显示的
超时-异步回调没有在jasmine指定的超时内调用。默认的\u Timeout\u INTERVAL
如果没有失败的
expect
我认为失败的测试不应该阻止调用done,请尝试
expect(false)。toBeTruthy()
并查看这是否会导致不调用
done
。可能您的expect抛出了一个静默捕获的异常,或者内容有一个循环引用。您可以尝试记录内容:
console.log(“content:,JSON.stringify(content,undefined,2))
确保您在记录时对对象进行JSON stringify,当对象发生变异时,变异显示在控制台中,并且您查看的对象是现在的样子,而不是记录时的样子。\n是否确实调用了
res.send
?我没有看到任何代码这样做。您可以尝试使用一些
console.log
语句调试代码,看看它们是否出现,或者只是使用
debugger
语句,看看当它在那里中断时会发生什么:是的,调用它。我已经通过在expect之前和之后记录内容对其进行了测试。似乎expect正在冻结或阻止进一步的执行。如果您将expect更改为:
expect(true)。toEqual(true)
,然后故意以
expect(true)失败。toEqual(false)
?如果执行了
expect(true)。toBeTruthy()
,则可以。所以我猜一个“failed”
expect
会停止进一步的执行,这就是为什么永远不会调用
done()
。遗憾的是,错误没有显示出来,只是显示的
超时-异步回调没有在jasmine指定的超时内调用。默认的\u Timeout\u INTERVAL
如果没有失败的
expect
我认为失败的测试不应该阻止调用done,请尝试
expect(false)。toBeTruthy()
并查看这是否会导致不调用
done
。可能您的expect抛出了一个静默捕获的异常,或者内容有一个循环引用。您可以尝试记录内容:
console.log(“content:,JSON.stringify(content,undefined,2))
确保您在记录时对对象进行JSON stringify,当对象发生变异时,变异显示在控制台中,并且您查看的对象是现在的样子,而不是记录时的样子。\n我为您构建了一个示例:。克隆它就行了。运行npm运行测试,您将得到超时。我也提出了承诺,但我想知道expect在express路由器中的问题。似乎express路由器以某种方式改变了jest的行为。我编写了自己的expect函数,该函数接受jest expect并完成。在try-catch中执行expect,如果捕获到错误,则测试失败。虽然我不喜欢那种解决方法works@pesdfa我更新了答案,express正在默默地捕捉jist抛出的错误。我认为最好在
test
方法中保留您的期望值,而不是在mock中。@pesdfa最后又做了一次更新,似乎jist
expect
需要在测试中抛出才能失败