我正在与同事讨论Jest测试中的done()。
他在JavaScript方面的经验要比我高出几个数量级,我是在async / await被普遍接受之后加入的,而且我来自.NET环境,所以我习惯了。
我这样编写测试:
it("should return 200 OK for POST method",async () => {
await request(app).post("SOMEENDPOINT")
.attach("file","file")
.expect(200);
});
他更习惯诺言,所以会这样写他的测试:
it("should return 200 OK for POST method",(done) => {
request(app).post("SOMEENDPOINT")
.attach("file","file")
.expect(200,done);
});
他对异步/异步的推送没什么问题,但是坚持认为我必须包括done,这样我要么要做他的版本修改版本:
it("should return 200 OK for POST method",async (done) => {
await request(app).post("SOMEENDPOINT")
.attach("file",done);
});
或者:
it("should return 200 OK for POST method",async (done) => {
const res = await request(app).post("SOMEENDPOINT")
.attach("file","file");
expect(res.status).toBe(200);
done();
});
虽然我认识到将done()作为参数包含进来是完全必要的,但在这种情况下,当我使用async / await时,完全没有必要。
请求是supertest.request。
我的问题是,我需要在asnyc / await中完全使用完成吗?