Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test

Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test

我写了测试代码,它运行良好,但在我添加了与当前代码无关的其他路由后,代码变得损坏,尤其是在这一点上:

1) "before each" hook for "should create new todo": Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test`.

const todos = [{
  _id: new ObjectID(),
  text: 'first test todo',
  completed: false,
  completedAt: null
}, {
  _id: new ObjectID(),
  text: 'second test todo',
  completed: true,
  completedAt: 333
}];


beforeEach((done) => {
  Todo.remove({}).then(() => {
    return Todo.insertMany(todos);
  }).then((docs) => {
    done();
  });
})
describe('post /todo', () => {
  it('should create new todo', (done) => {
    let text = 'here from supertest';
    request(app)
      .post('/todos')
      .send({
        text
      })
      .expect(200)
      .expect((res) => {
        expect(res.body.text).toBe(text);

      })
      .end((err, res) => {
        if (err) {
          return done(err);
        }

        Todo.find({
            text
          }).then((res) => {
            expect(res.length).toBe(1);
            expect(res[0].text).toBe(text);
            done();
          })
          .catch((e) => {
            done(e);
          });
      });
  });
});

整个项目是 in this github libary called TestMongo,您可以在其中检查最后两个提交,这些提交存在我面临的测试问题,但是当您通过邮递员尝试时,它工作正常。当一个人恢复到最后的第三次提交时,测试工作正常。

可能我们需要将测试超时增加到更大的数字

beforeEach(function(done) { // dont use arrow function to use this.timeout
  this.timeout(5000); // override default 2000 ms

  Todo.remove({}).then(() => {
    return Todo.insertMany(todos);
  }).then((docs) => {
    done();
  });
})

希望对您有所帮助