【发布时间】:2021-09-05 18:42:34
【问题描述】:
我正在使用 mocha 进行测试
it('should allow a POST to /users', async function () {
const res = await request.post('/users').send(firstUserBody);
expect(res.status).to.equal(201);
expect(res.body).not.to.be.empty;
expect(res.body).to.be.an('object');
expect(res.body.id).to.be.a('string');
firstUserIdTest = res.body.id;
});
但我有一个错误
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
如果我使用 done() 执行此操作,则该函数不是异步的,但它应该是
it('should allow a POST to /users', async function (done) {
const res = await request.post('/users').send(firstUserBody);
expect(res.status).to.equal(201);
expect(res.body).not.to.be.empty;
expect(res.body).to.be.an('object');
expect(res.body.id).to.be.a('string');
firstUserIdTest = res.body.id;
done();
});
我该怎么办?
【问题讨论】:
-
不异步是什么意思?
-
我修复了代码 2,如果我编写像 2 这样的代码,'await' 会出错。
-
什么样的错误?
-
并且是否 POST 会在 2 秒内解决?如果测试回调是异步的,则不需要接受 done 参数。
-
idk 但似乎没有。我有超过 2000 毫秒的超时错误
标签: node.js typescript mocha.js supertest