在每个套件之前运行Mocha设置,而不是在每个测试之前

时间:2014-09-29 19:05:18

标签: javascript node.js mocha

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

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

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

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 two'

或者换句话说,在S1测试之前,'和' S2测试一个'但不是在S1测试二'。

之前

可以吗?

4 个答案:

答案 0 :(得分:20)

没有类似于beforeEachbefore的调用可以满足您的需求。但它不是必需的,因为你可以这样做:

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();
  });
});

答案 1 :(得分:9)

你也可以用这种更灵活的方式做到这一点:

require('./setupStuff');

describe('Suite one', function(){
  loadBeforeAndAfter(); //<-- added
  it('S1 Test one', function(done){
    ...
  });
  it('S1 Test two', function(done){
    ...
  });
});
describe('Suite two', function(){
  loadBeforeAndAfter();//<-- added
  it('S2 Test one', function(done){
    ...
  });
});
describe('Suite three', function(){
  //use some other loader here, before/after, or nothing
  it('S3 Test one', function(done){
    ...
  });
});

function loadBeforeAndAfter() {
  before(function () {
    console.log("shared before");
  });
  after(function () {
    console.log("shared after");
  });
}

答案 2 :(得分:0)

我发现这种方法适用于我,它修补了所有描述套件。

function suitePatches()
{
    before(function()
    {
        // before suite behaviour
    });
    after(function()
    {
        // after suite behaviour
    });
}

let origDescribe = describe;
describe = function(n,tests)
{
    origDescribe(n,function()
    {
        suitePatches();
        tests.bind(this)();
    });
}
let origOnly = origDescribe.only;
describe.only = function(n,tests)
{
    origOnly(n,function()
    {
        suitePatches();
        tests.bind(this)();
    });
}
describe.skip = origDescribe.skip;

与其他答案的区别在于:

  • 使用bind来调用tests,以确保如果他们调用this上的函数,例如this.timeout(1000)仍然有效。
  • 处理.skip.only意味着您仍然可以使用套件上的内容,例如describe.skip暂时禁止套件。
  • 按名称替换describe函数可以减少注入侵入性。
    • 这可能不符合每个人的口味,在这种情况下,显然可以使用替代功能名称,同时仍然使用正确处理调用testsonly以及skip

答案 3 :(得分:0)

@ya_dimon的解决方案正在工作,但是如果要包装callback的{​​{1}}函数,并按如下所示将参数传递给它。

it

为什么定义function dynamicTestCase(a) { return function(done){ console.log(a); } // a is undefined } describe("test", function(){ before(function(){ a = 8; }); it('POST /verifications receiveCode', dynamicTestCase(a)); // a is undefined }) ?因为ait中的before之前执行。在这种情况下,@ya_dimon的解决方案不起作用,但是您可以按照以下说明巧妙地完成它。

describe

希望此帮助可以确定执行顺序。

相关问题