首页 文章

nodeJS Supertest,mocha失败错误:超出2000ms超时

提问于
浏览
0

我的"/todos"路由工作,实际上我可以在 end 回调中的if语句之后调用done()并且它'll update the database, but when I add a my mongodb query to verfiy it has been added, it doesn' t更新我的数据库它给了我

错误:超过2000毫秒的超时 . 确保在此测试中调用done()回调 .

describe('POST /todos', () => {
  it('should create a new todo', (done) => {
    var text = 'Test todo text';

    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((todos) => {
          expect(todos.length).toBe(1);
          expect(todos[0].text).toBe(text);
          done();
        }).catch((e) => done(e));
      })
  });
});

1 回答

  • 0

    即使定义/调用 done 回调,测试仍然有2秒的默认超时,因为它可以在调用异步函数时超时 . 如果服务器正常运行且测试代码正确,您可以使用timeout as described in this answer来增加超时并防止其过早拒绝 .

    示例包括我知道如何做到这一点(可能还有更多 - 我只学到了我需要的东西):

    describe('#something', function () {
      this.timeout(milliseconds)
    
      it('does something...slowly.', function () {
        this.timeout(milliseconds)
      }).timeout(milliseconds)
    })
    

    这当然是所有人都挤在一起的;我的想法是根据需要选择一个 .

相关问题