Mocha: this.skip() in before fails to skip tests with nested describe calls

Created on 23 May 2017  ·  14Comments  ·  Source: mochajs/mocha

Based on the documentation:

To skip multiple tests in this manner, use this.skip() in a “before” hook

I expected the test below to never run.

describe('skip', function() {
  before(function() {
    this.skip();
  });

  describe('bug', function() {
    it('should never be called', function() {
      throw new Error('Why is this reached?');
    });
  });
});

However, here is the output.

✗ mocha bug.js


  skip
    bug
      1) should never be called


  0 passing (7ms)
  1 failing

  1) skip bug should never be called:
     Error: Why is this reached?
      at Context.<anonymous> (bug.js:8:19)

This seems like a bug to me. If it is expected behavior, then the documentation should be updated to reflect this.

needs-review

Most helpful comment

The workaround in https://github.com/mochajs/mocha/issues/2683#issuecomment-375629901 worked for me with [email protected]

All 14 comments

@fearphage I'm able to reproduce this with Mocha v3.4.2. It appears that calling this.skip() doesn't work correctly for nested suites. 😕

I've come across this issue as well. [email protected]

Just ran into this issue as well in v4.0.1

ran into this issue too, any process?

cc @Munter @boneskull

I see this behavior too. I'm pretty sure it's a bug that was introduced at some point between late 2016 and the date that this thread was opened. I've been editing some tests that skipped correctly when I looked at them a year ago and now aren't being skipped.

5.0.4, ran into this issue too

I suspect there is something being mis-handled with "current test" and before resolution. This feels like it may be a different symptom of the problem underlying #2134

I confirm I have this bug as well

Yet another confirmation post. Is anyone going to get on this?

Looking at the number of open issues, it's doubtful :)

I have worked around this by setting a scoped skipping variable true if skipping, and then checking that in a before function in each subsuite. It's not pretty, but it's a nice chance to link to this issue :)

describe('outer', function() {
  let skipping = false;
  before(function() {
    if (...) {
      this.pending = 1;
      skipping = 1;
    }
  });

  describe('inner', function() {
    // work around https://github.com/mochajs/mocha/issues/2819
    before(function() {
      if (skipping) {
        this.pending = 1;
      }
    });
  });
});

There is a simpler workaround imo, using beforeEach instead:

beforeEach(function() {
  if (skipping) this.skip()
})

The workaround in https://github.com/mochajs/mocha/issues/2683#issuecomment-375629901 worked for me with [email protected]

@fearphage I think the fix for this has now gone into master (via #3225). Closing this issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

danielserrao picture danielserrao  ·  3Comments

Aarbel picture Aarbel  ·  3Comments

luoxi001713 picture luoxi001713  ·  3Comments

seelikes picture seelikes  ·  3Comments

smithamax picture smithamax  ·  4Comments