在每个套件之前运行Mocha设置,而不是在每个测试之前 [英] Running Mocha setup before each suite rather than before each test

查看:87
本文介绍了在每个套件之前运行Mocha设置,而不是在每个测试之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用NodeJS和Mocha进行测试。我想我明白before()和beforeEach()是如何工作的。问题是,我想添加一个在每个描述之前运行的设置脚本,而不是在每个它之前运行。

Using NodeJS and Mocha for testing. I think I understand how before() and beforeEach() work. Problem is, I'd like to add a setup script that runs before each "describe" rather than before each "it".

如果我使用 before()它只对整个套件运行一次,如果我使用 beforeEach(),它将在每次测试之前执行,所以我试图找到一个中间地带。

If I use before() it will run only once for the entire suite, and if I use beforeEach() it will execute before every single test, so I'm trying to find a middle ground.

所以,如果这是我的测试文件:

So, if this is my test file:

require('./setupStuff');

describe('Suite one', function(){
  it('S1 Test one', function(done){
    ...
  });
  it('S1 Test two', function(done){
    ...
  });
});
describe('Suite two', function(){
  it('S2 Test one', function(done){
    ...
  });
});

我想让setupStuff包含一个在Suite one之前运行的功能套房二'

I'd like to have "setupStuff" contain a function that runs before 'Suite one' and 'Suite two'

或者换句话说,在'S1 Test one'和'S2 Test one'之前,但不是在'S1 Test two'之前。

Or, in other words, before 'S1 Test one' and 'S2 Test one' but NOT before 'S1 Test two'.

能否完成?

推荐答案

没有类似于 beforeEach 或之前做你想做的事。但它不是必需的,因为你可以这样做:

There's no call similar to beforeEach or before that does what you want. But it is not needed because you can do it this way:

function makeSuite(name, tests) {
    describe(name, function () {
        before(function () {
            console.log("shared before");
        });
        tests();
        after(function () {
            console.log("shared after");
        });
    });
}

makeSuite('Suite one', function(){
  it('S1 Test one', function(done){
      done();
  });
  it('S1 Test two', function(done){
      done();
  });
});

makeSuite('Suite two', function(){
  it('S2 Test one', function(done){
    done();
  });
});

这篇关于在每个套件之前运行Mocha设置,而不是在每个测试之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆