Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" when using Mocha for test in nodejs
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" when using Mocha for test in nodejs
我想为测试createUser
写一个测试。
我写了这段代码:
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();
});
});
});
当我 运行 测试使用是在数据库中创建的,但它向我显示此错误并且测试失败 :
错误:超时超过 2000 毫秒。对于异步测试和挂钩,确保调用“done()”;如果返回 Promise,请确保它已解析。 (F:\Projects\Nodejs\MongooDB\test\create-user_test.js)
在 listOnTimeout(internal/timers.js:549:17)
在 processTimers (internal/timers.js:492:7)
有什么问题吗?我该如何解决??
这里有几个方案可以查一下
"scripts": {
"test": "mocha --timeout 10000" <= increase this from 1000 to 10000
},
#### OR ###
it("Test Post Request", function(done) {
this.timeout(10000); //add timeout.
});
也可以应用特定于测试的超时,或者使用 this.timeout(0)
一起禁用超时:
it('should take less than 500ms', function(done){
this.timeout(500);
setTimeout(done, 300);
});
如果两者都不行。试试这个
const delay = require('delay')
describe('Test', function() {
it('should resolve', async function() {
await delay(1000)
})
})
异步函数中 done 参数的存在以某种方式破坏了测试,即使它没有被使用,即使在测试结束时调用了 done()。
我想为测试createUser
写一个测试。
我写了这段代码:
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();
});
});
});
当我 运行 测试使用是在数据库中创建的,但它向我显示此错误并且测试失败 :
错误:超时超过 2000 毫秒。对于异步测试和挂钩,确保调用“done()”;如果返回 Promise,请确保它已解析。 (F:\Projects\Nodejs\MongooDB\test\create-user_test.js) 在 listOnTimeout(internal/timers.js:549:17) 在 processTimers (internal/timers.js:492:7)
有什么问题吗?我该如何解决??
这里有几个方案可以查一下
"scripts": {
"test": "mocha --timeout 10000" <= increase this from 1000 to 10000
},
#### OR ###
it("Test Post Request", function(done) {
this.timeout(10000); //add timeout.
});
也可以应用特定于测试的超时,或者使用 this.timeout(0)
一起禁用超时:
it('should take less than 500ms', function(done){
this.timeout(500);
setTimeout(done, 300);
});
如果两者都不行。试试这个
const delay = require('delay')
describe('Test', function() {
it('should resolve', async function() {
await delay(1000)
})
})
异步函数中 done 参数的存在以某种方式破坏了测试,即使它没有被使用,即使在测试结束时调用了 done()。