我想为测试写一个测试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)有什么问题 ?我该如何解决?
1 回答

杨魅力
TA贡献1811条经验 获得超6个赞
这里可以检查一些解决方案。
"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() 在测试结束时被调用。
添加回答
举报
0/150
提交
取消