node.js 对于异步测试和钩子,确保调用“done()”;如果返回 Promise,请确保它已解决
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44149096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves
提问by Gerald Brigen
I have this test of nodejs when testing I get a error of done function not declared.
我在测试时对 nodejs 进行了此测试,但出现未声明完成函数的错误。
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
My test code is, I have the done call back but still getting the error to call the done();
我的测试代码是,我已经完成回调,但仍然收到错误调用 done();
it('remove existing subdocument', (done) => {
const Vic = new User({
name: 'Vic',
posts: [{ title: 'Leaning Nodejs' }]
});
vic.save()
.then(() => User.findOne({ name: 'Vic' }))
.then((user) => {
const post = user.posts[0];
post.remove();
return user.save();
})
.then(() => User.findOne({ name: 'Vic' }))
.then((user) => {
assert(user.posts.length === 0);
done();
});
});
采纳答案by avck
I was facing the same issue, @MFAL's link in comment helped. I am expanding upon it.
我遇到了同样的问题,@MFAL 在评论中的链接有帮助。我正在扩展它。
When there is an error/incorrect assertion an error is raised inside the promise. This leads to promise rejection. Once rejected done is never called and mocha reports time out.
I solved this by writing a .catchblock and chaining it with the promise:
当存在错误/不正确的断言时,promise 中会引发错误。这会导致承诺拒绝。一旦被拒绝 done 永远不会被调用并且 mocha 报告超时。我通过编写一个.catch块并将其与承诺链接来解决了这个问题:
it('resolves', (done) => {
fooAsyncPromise(arg1, arg2).then((res, body) => {
expect(res.statusCode).equal(incorrectValue);
done();
}).catch(done);
});
Other ways as mentioned in the Wietse's blogare:
Wietse 的博客中提到的其他方法是:
To chain a then(done, done)which handles both resolve and reject of the promise.
链接 athen(done, done)来处理对承诺的解决和拒绝。
it('resolves', (done) => {
resolvingPromise.then( (result) => {
expect(result).to.equal('promise resolved');
}).then(done, done);
});
Return a promise:
返回一个承诺:
it('resolves', () => {
return resolvingPromise.then( (result) => {
expect(result).to.equal('promise resolved');
});
});
Use async/wait:
使用异步/等待:
it('assertion success', async () => {
const result = await resolvingPromise;
expect(result).to.equal('promise resolved');
});
回答by Sabunkar Tejas Sahailesh
I know an ugly way of doing it, just by increasing default timeout of Mocha from 2 seconds to 10 secondsthis can be done by adding a flag --timeout 10000 in the test scripts i.e -
我知道一种丑陋的方法,只需将 Mocha 的默认超时从 2 秒增加到 10 秒,就可以通过在测试脚本中添加一个标志 --timeout 10000 来完成,即 -
package.json
包.json
"scripts": {
"start": "SET NODE_ENV=dev && node server.js",
"test": "mocha --timeout 10000"
}
回答by Sandeep chand
in package.json file you can fix this error by using --timeout 1500 just like i use below.
在 package.json 文件中,您可以使用 --timeout 1500 来修复此错误,就像我在下面使用的那样。
"scripts": {
"start": "node server/server.js",
"test": "export NODE_ENV=test || SET \"NODE_ENV=test\" && mocha --timeout 15000 server/**/*.test.js",
"test-watch": "nodemon --exec 'npm test'"
}
this error occurs because of mocha.
发生此错误是因为摩卡咖啡。
回答by Ajibola L'Don Jibson Olayanju
You can just add timeout to the specific test to increase/override the default timeout which is 2 seconds. I had the same issue but was able to by pass it using:
您可以将超时添加到特定测试以增加/覆盖默认超时 2 秒。我遇到了同样的问题,但能够使用以下方法绕过它:
it('Test', (done) => {
//your code
done();
}).timeout(10000);
回答by Abdennour TOUMI
Happe with me when I returned back maintaining an old node-module from Node6 to Node 13. The fix is simple :
当我返回维护从 Node6 到 Node 13 的旧节点模块时,请对我感到满意。修复很简单:
- adding a file
mocha.optsunder the folder of tests. - The content of this file is one line :
--timeout 10000
mocha.opts在测试文件夹下添加一个文件。- 该文件的内容是一行:
--timeout 10000
回答by Mr.B
The idea is to increase the timeout.
Alternative way is to do that in required method only:
这个想法是增加超时。
另一种方法是仅在所需的方法中执行此操作:
it('remove existing subdocument', function(done) {
this.timeout(10000);
//your code is here
done();
});
That help me to resolve the problem.
这有助于我解决问题。

