简体   繁体   English

错误:超过 2000 毫秒的超时。 对于异步测试和钩子,在 nodejs 中使用 Mocha 进行测试时确保“done()”

[英]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.

相关问题 摩卡:错误:超过 2000 毫秒超时。 对于异步测试和钩子,确保调用“done()”; 如果返回 Promise,请确保它已解决 - Mocha: Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves Mocha&Chai-超过2000毫秒的超时。 确保在此测试中调用done()回调。“ - Mocha & Chai- “timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.” 噩梦:错误:超时超过2000毫秒。 对于异步测试和挂钩 - Nightmare: Error: Timeout of 2000ms exceeded. For async tests and hooks 错误:超时超过2000毫秒。 承诺的单元测试 - Error: timeout of 2000ms exceeded. Unit test with promises 错误:超时超过30000ms。 对于异步测试和挂钩,请确保调用了“ done()”。 如果返回承诺,请确保其解决 - Error: Timeout of 30000ms exceeded. For async tests and hooks, ensure “done()” is called; if returning a Promise, ensure it resolves 摩卡:超过 2000 毫秒的错误超时 - Mocha: Error Timeout of 2000ms exceeded 错误:超出2000毫秒的超时。 保证在测试运行之前不返回 - Error: Timeout of 2000ms exceeded. promise not returning before test runs 在Mocha中收到“超过2000ms超时”错误 - Getting 'Timeout of 2000ms exceeded' error in mocha 2000ms的超时超过了摩卡 - timeout of 2000ms exceeded mocha 为什么我总是收到错误消息:异步Mocha超过了2000ms的超时时间? - Why am I keep getting Error: Timeout of 2000ms exceeded from Mocha on async?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM