[英]Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure “done()” when using Mocha for test in nodejs
i want to write a test for test createUser
.我想为 test
createUser
编写一个测试。
i write this code:我写了这段代码:
const User = require("../src/Entite/User");
describe("Create User", () => {
it(" Save and Create User ", (done) => {
const addUser = new User({
name: "Kianoush",
family: "Dortaj",
});
addUser
.save()
.then(() => {
assert(!addUser.isNew);
done();
});
});
});
when i run the test use was created in database but it show me this error and test fail:当我运行测试时,使用是在数据库中创建的,但它显示了这个错误并且测试失败:
Error: Timeout of 2000ms exceeded.错误:超过 2000 毫秒的超时。 For async tests and hooks, ensure "done()" is called;
对于异步测试和钩子,确保调用了“done()”; if returning a Promise, ensure it resolves.
如果返回 Promise,请确保它已解决。 (F:\Projects\Nodejs\MongooDB\test\create-user_test.js) at listOnTimeout (internal/timers.js:549:17) at processTimers (internal/timers.js:492:7)
(F:\Projects\Nodejs\MongooDB\test\create-user_test.js) 在 listOnTimeout (internal/timers.js:549:17) 在 processTimers (internal/timers.js:492:7)
whats the problem?有什么问题? how can i solve that??
我该如何解决?
Here a few solutions can be checked.这里可以检查一些解决方案。
"scripts": {
"test": "mocha --timeout 10000" <= increase this from 1000 to 10000
},
#### OR ###
it("Test Post Request", function(done) {
this.timeout(10000); //add timeout.
});
Test-specific timeouts may also be applied, or the use of this.timeout(0)
to disable timeouts all together:也可以应用特定于测试的超时,或者使用
this.timeout(0)
一起禁用超时:
it('should take less than 500ms', function(done){
this.timeout(500);
setTimeout(done, 300);
});
If both do not work.如果两者都不起作用。 Try this
尝试这个
const delay = require('delay')
describe('Test', function() {
it('should resolve', async function() {
await delay(1000)
})
})
Somehow the presence of the done argument in the async function breaks the test, even if it's not used, and even if done() is called at the end of the test.不知何故,异步 function 中的 done 参数会破坏测试,即使它没有被使用,即使在测试结束时调用了 done()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.