最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" when using Mocha fo

programmeradmin0浏览0评论

i want to write a test for 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. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (F:\Projects\Nodejs\MongooDB\test\create-user_test.js) at listOnTimeout (internal/timers.js:549:17) at processTimers (internal/timers.js:492:7)

whats the problem ? how can i solve that ??

i want to write a test for 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. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (F:\Projects\Nodejs\MongooDB\test\create-user_test.js) at listOnTimeout (internal/timers.js:549:17) at processTimers (internal/timers.js:492:7)

whats the problem ? how can i solve that ??

Share Improve this question asked Jul 2, 2020 at 18:07 kianoush dortajkianoush dortaj 4519 silver badges28 bronze badges 1
  • Does this answer your question? In mocha testing while calling asynchronous function how to avoid the timeout Error: timeout of 2000ms exceeded – Dez Commented Jul 2, 2020 at 18:31
Add a ment  | 

1 Answer 1

Reset to default 3

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:

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.

发布评论

评论列表(0)

  1. 暂无评论