当使用mocha测试异步函数的结果时,await之后的测试会从嵌套结构中弹出,比如下面的前两个测试:
✓ email
✓ phone
current unit
fetch data
✓ is 5==5
3 passing (10ms)我们怎样才能让测试出现在合适的位置呢?
代码:
const chai = require('chai');
chai.should();
describe ("current unit", async () => {
describe ("fetch data", async () => {
it ("is 5==5", () => { chai.expect(5).to.equal(5); });
const UserData = await getUserData("UserName");
it ("email", () => { UserData.email.should.equal("example@g.com"); });
it ("phone", () => { UserData.phone.should.equal("+1 (800) 123 4567"); });
});
});
function getUserData(param) { return new Promise(resolve => setTimeout(() => resolve({ email:"example@g.com",phone:"+1 (800) 123 4567" }), 1/*ms*/));}发布于 2020-05-23 09:27:51
这是使用before的测试文件
const chai = require('chai');
chai.should();
describe ("current unit", async () => {
describe ("fetch data", async () => {
let UserData
before(async () => {
UserData = await getUserData("UserName");
})
it ("is 5==5", () => { chai.expect(5).to.equal(5); });
it ("email", () => { UserData.email.should.equal("example@g.com"); });
it ("phone", () => { UserData.phone.should.equal("+1 (800) 123 4567"); });
});
});
function getUserData(param) { return new Promise(resolve => setTimeout(() => resolve({ email:"example@g.com",phone:"+1 (800) 123 4567" }), 1/*ms*/));}以下是上述测试的输出
current unit
fetch data
✓ is 5==5
✓ email
✓ phone通过使用"fetch data“测试套件中的before表达式,可以使测试显示在适当的位置
发布于 2020-05-22 23:34:02
如果您将代码从async/await语法“转换”为Promise语法,那么解释起来会更加清晰:
describe("current unit", () => {
describe("fetch data", () => {
it("is 5==5", () => { chai.expect(5).to.equal(5); });
getUserData("UserName")
.then(UserData => {
it("email", () => { UserData.email.should.equal("example@g.com"); });
it("phone", () => { UserData.phone.should.equal("+1 (800) 123 4567"); });
});
});
});如你所见,"fetch data“只包括is 5==5,而email,phone规范在另一个作用域中(在本例中,作用域是free describe),那么这些规范将出现在顶部。
getUserData只是“等待”1ms,然后您可以看到email,phone规范,如果您将该值增加到100ms (或更高),您将不会看到这些规范,因为getUserData().then是一个同步块。
千万不要在describe的body中直接调用异步动作,让我们使用beforeEach,或者写在it的body中。
使用beforeEach
describe("current unit", () => { // remove async keyword, it does not make sense
let UserData; // define variable
beforeEach(async () => { // async
UserData = await getUserData("UserName"); // init
});
describe("fetch data", () => { // remove async keyword
it("is 5==5", () => { chai.expect(5).to.equal(5); });
it("email", () => { UserData.email.should.equal("example@g.com"); });
it("phone", () => { UserData.phone.should.equal("+1 (800) 123 4567"); });
});
}); current unit
fetch data
✓ is 5==5
✓ email
✓ phone
3 passing (357ms)在it块中写入:
describe("current unit", () => { // remove async keyword, it does not make sense
describe("fetch data", () => { // remove async keyword
it("is 5==5", () => { chai.expect(5).to.equal(5); });
it("should return correct email and phone", async () => { // compile into 1 spec
const UserData = await getUserData("UserName");
UserData.email.should.equal("example@g.com");
UserData.phone.should.equal("+1 (800) 123 4567");
});
});
}); current unit
fetch data
✓ is 5==5
✓ should return correct email and phone (108ms)
2 passing (149ms)发布于 2020-05-22 22:34:35
您需要在测试的异步部分之后调用done()函数。下面是一个例子:
it ("email", (done) => {
UserData.email.should.equal("example@g.com");
done();
});https://stackoverflow.com/questions/61956993
复制相似问题