【发布时间】:2020-03-01 22:44:01
【问题描述】:
我在 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);
});
他对我推送到async/await 没有意见,但坚持我必须包括done,这样我要么做他的版本修改版本:
it("should return 200 OK for POST method", async (done) => {
await request(app).post("SOMEENDPOINT")
.attach("file", "file")
.expect(200, 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。
我的问题是,我是否需要将done 与async/await 一起使用?
【问题讨论】:
-
如您在文档中所见,不需要:jestjs.io/docs/en/tutorial-async.html#async-await
-
这也是我最初的论点。
标签: javascript node.js async-await jestjs supertest