首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mocha.js:异步函数打破嵌套结构

Mocha.js:异步函数打破嵌套结构
EN

Stack Overflow用户
提问于 2020-05-22 22:14:03
回答 3查看 211关注 0票数 2

当使用mocha测试异步函数的结果时,await之后的测试会从嵌套结构中弹出,比如下面的前两个测试:

代码语言:javascript
复制
✓ email
✓ phone
current unit
    fetch data
    ✓ is 5==5

3 passing (10ms)

我们怎样才能让测试出现在合适的位置呢?

代码:

代码语言:javascript
复制
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*/));}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-05-23 09:27:51

这是使用before的测试文件

代码语言:javascript
复制
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*/));}

以下是上述测试的输出

代码语言:javascript
复制
  current unit
    fetch data
      ✓ is 5==5
      ✓ email
      ✓ phone

通过使用"fetch data“测试套件中的before表达式,可以使测试显示在适当的位置

票数 0
EN

Stack Overflow用户

发布于 2020-05-22 23:34:02

如果您将代码从async/await语法“转换”为Promise语法,那么解释起来会更加清晰:

代码语言:javascript
复制
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,而emailphone规范在另一个作用域中(在本例中,作用域是free describe),那么这些规范将出现在顶部。

getUserData只是“等待”1ms,然后您可以看到emailphone规范,如果您将该值增加到100ms (或更高),您将不会看到这些规范,因为getUserData().then是一个同步块。

千万不要在describe的body中直接调用异步动作,让我们使用beforeEach,或者写在it的body中。

使用beforeEach

代码语言:javascript
复制
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"); });
  });
});
代码语言:javascript
复制
  current unit
    fetch data
      ✓ is 5==5
      ✓ email
      ✓ phone
  3 passing (357ms)

it块中写入:

代码语言:javascript
复制
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");
    });
  });
});
代码语言:javascript
复制
  current unit
    fetch data
      ✓ is 5==5
      ✓ should return correct email and phone (108ms)
  2 passing (149ms)
票数 1
EN

Stack Overflow用户

发布于 2020-05-22 22:34:35

您需要在测试的异步部分之后调用done()函数。下面是一个例子:

代码语言:javascript
复制
it ("email", (done) => {
  UserData.email.should.equal("example@g.com");
  done();
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61956993

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档