4
describe('1', function () {
  beforeEach(function () {
    // do this before each it EXCEPT 1.5
  });
  it('1.1', function () {

  });
  it('1.2', function () {

  });
  it('1.3', function () {

  });
  it('1.4', function () {

  });
  it('1.5', function () {
    // beforeEach shouldn't run before this
  });
});

I'd like to prevent a beforeEach from running before it block 1.5. How can I do that?

Adam Zerner
  • 17,797
  • 15
  • 90
  • 156
  • You can probably get more insights on this thread http://stackoverflow.com/questions/32723167/how-to-programmatically-skip-a-test-in-mocha other than the answer provided by chriskelly – gnomeria Apr 21 '16 at 21:33

2 Answers2

3

Option 1

I would suggest using nesting your describes, e.g.:

describe('1', function () {

  describe('1 to 4', function () {

    beforeEach(function () {
      // do this before each it EXCEPT 1.5
    });
    it('1.1', function () {

    });
    it('1.2', function () {

    });
    it('1.3', function () {

    });
    it('1.4', function () {

    });
  });

  describe('only 5', function () {
     it('1.5', function () {
     // beforeEach shouldn't run before this
  });

});

Behind the scenes describe will register the beforeEach function which will get called for all itFunctions if it exists.


Option 2

The it functions will be called sequentially so you could also use a closure to control when beforeEach gets run - but it's a bit hacky - e.g.:

describe('1', function () {
  var runBefore = true
  beforeEach(function () {
    // do this before each it EXCEPT 1.5
    if (runBefore) {
        // actual code
    }
  });
  // functions removed for brevity    
  it('1.4', function () {
      runBefore = false;
  });
  it('1.5', function () {
    // beforeEach shouldn't run before this

    // turn it back on for 1.6
    runBefore = true;
  });
});
sompylasar
  • 924
  • 11
  • 15
chriskelly
  • 7,526
  • 3
  • 32
  • 50
0

You can achieve that by avoiding nesting when you're testing. The idea is to avoid unnecessary abstraction and instead extract some function(s) that will set up your test case, and then call this function(s) wherever it's necessary.

This leads to code that is more readable and easier to maintain. Instead of figuring out what is happening in a test case by following all the nested beforeEach calls, you can simply read it line-by-line. And it makes your problem trivial to solve:

const setupTestCase = () => {
  // put the code here, instead of in beforeEach
  // if you're doing multiple actions, put them in separate functions and call them one by one
  // this makes your test more readable and easier to maintain
};

describe('1', function () {
  it('1.1', function () {
    setupTestCase();
    // do test stuff
  });
  it('1.2', function () {
    setupTestCase();
    // do test stuff
  });
  it('1.3', function () {
    setupTestCase();
    // do test stuff
  });
  it('1.4', function () {
    setupTestCase();
    // do test stuff
  });
  it('1.5', function () {
    // here we simply don't call setupTestCase()
    // do test stuff
  });
});

PS. also in many cases you don't need that top-level describe block and can make your code more readable and save yourself one level of nesting by simply moving each top-level describe into a separate file.

szaman
  • 2,159
  • 1
  • 14
  • 30