错误:超过 2000 毫秒的超时。对于异步测试和钩子,在 nodejs 中使用 Mocha

我想为测试写一个测试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)


有什么问题 ?我该如何解决?



温温酱
浏览 117回答 1
1回答

杨魅力

这里可以检查一些解决方案。"scripts": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "test": "mocha --timeout 10000"&nbsp; <= increase this from 1000 to 10000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;},#### OR ###it("Test Post Request", function(done) {&nbsp; &nbsp; &nbsp;this.timeout(10000);&nbsp; //add timeout.});也可以应用特定于测试的超时,或者this.timeout(0)一起使用来禁用超时:it('should take less than 500ms', function(done){&nbsp; this.timeout(500);&nbsp; setTimeout(done, 300);});如果两者都不起作用。尝试这个const delay = require('delay')describe('Test', function() {&nbsp; &nbsp; it('should resolve', async function() {&nbsp; &nbsp; &nbsp; await delay(1000)&nbsp; &nbsp; })})不知何故,异步函数中 done 参数的存在会破坏测试,即使它没有被使用,即使 done() 在测试结束时被调用。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript