使用mochaJS为每个嵌套的“描述”运行“之后”钩子

时间:2019-07-11 07:14:37

标签: node.js mocha bdd

对于每个嵌套的describe,我要运行以下序列:

  1. before->创建帐户
  2. it->运行测试
  3. after->删除创建的帐户

我的问题是after挂钩仅在运行所有describe之后运行,而不是在每个describe上运行。

如何按上述顺序运行测试?

let res;

const randomAccount = {
  'email': 'automation17@yahoo.com',
  'password': '123456',
};


describe('Create accounts with email and password', function() {
  describe('should create a valid account', function() {
    before(function() {
      return accountsHelper.createNewAccount(randomAccount)
        .then(function(response) {
          res = response;
        });
    });

    it('should return status code 200', function(done) {
      expect(res.status).to.equal(commonData.statusCode.ok);
      done();
    });

    it('should return response', function(done) {
      expect(res.body.admin).to.equal(false);
      done();
    });

    after(function() {
      return accountsHelper.deleteAccount();
    });
  });

  describe('should create an invalid account', function() {

    before(function() {
      return accountsHelper.createNewAccount(randomAccount)
        .then(function(response) {
          res = response;
        });
    });

    it('should return response', function(done) {
      expect(res.body.admin).to.equal(false);
      done();
    });

    after(function() {
      return accountsHelper.deleteAccount();
    });
  });
});

0 个答案:

没有答案