Skip to content

Commit

Permalink
Skip child suites when using this.skip()
Browse files Browse the repository at this point in the history
When using `this.skip()` in a before hook, it skips all the tests in
the current suite, but runs the tests in all the child suites.

This patch expands the fixtures in skip-[a]sync-before fixtures to
cover the more expected behavior, and corrects the handling of Pending
exceptions in runner.js accordingly.

Fixes mochajs#2819
  • Loading branch information
leedm777 committed Feb 9, 2018
1 parent 73d55ac commit 46b0e14
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/index.md
Expand Up @@ -594,6 +594,8 @@ before(function() {
});
```

This will skip all the tests and beforeEach/afterEach hooks in the current suite, and all hooks and tests of any child suites. It will, however, continue running the before all and after all hooks of the current suite.

> Before Mocha v3.0.0, `this.skip()` was not supported in asynchronous tests and hooks.
## Retry Tests
Expand Down
3 changes: 3 additions & 0 deletions lib/runner.js
Expand Up @@ -314,6 +314,9 @@ Runner.prototype.hook = function (name, fn) {
suite.tests.forEach(function (test) {
test.pending = true;
});
suite.suites.forEach(function (suite) {
suite.pending = true;
});
// a pending hook won't be executed twice.
hook.pending = true;
}
Expand Down
30 changes: 30 additions & 0 deletions test/integration/fixtures/pending/skip-async-before.fixture.js
Expand Up @@ -8,11 +8,41 @@ describe('skip in before', function () {
}, 50);
});

beforeEach(function () {
throw new Error('never thrown');
});

afterEach(function () {
throw new Error('never thrown');
});

it('should never run this test', function () {
throw new Error('never thrown');
});

it('should never run this test', function () {
throw new Error('never thrown');
});

describe('in child suites', function () {
before(function () {
throw new Error('never thrown');
});

after(function () {
throw new Error('never thrown');
});

beforeEach(function () {
throw new Error('never thrown');
});

afterEach(function () {
throw new Error('never thrown');
});

it('should never run this test', function () {
throw new Error('never thrown');
});
});
});
30 changes: 30 additions & 0 deletions test/integration/fixtures/pending/skip-sync-before.fixture.js
Expand Up @@ -5,11 +5,41 @@ describe('skip in before', function () {
this.skip();
});

beforeEach(function () {
throw new Error('never thrown');
});

afterEach(function () {
throw new Error('never thrown');
});

it('should never run this test', function () {
throw new Error('never thrown');
});

it('should never run this test', function () {
throw new Error('never thrown');
});

describe('in child suites', function () {
before(function () {
throw new Error('never thrown');
});

after(function () {
throw new Error('never thrown');
});

beforeEach(function () {
throw new Error('never thrown');
});

afterEach(function () {
throw new Error('never thrown');
});

it('should never run this test', function () {
throw new Error('never thrown');
});
});
});
4 changes: 2 additions & 2 deletions test/integration/pending.spec.js
Expand Up @@ -58,7 +58,7 @@ describe('pending', function () {
done(err);
return;
}
assert.equal(res.stats.pending, 2);
assert.equal(res.stats.pending, 3);
assert.equal(res.stats.passes, 0);
assert.equal(res.stats.failures, 0);
assert.equal(res.code, 0);
Expand Down Expand Up @@ -108,7 +108,7 @@ describe('pending', function () {
done(err);
return;
}
assert.equal(res.stats.pending, 2);
assert.equal(res.stats.pending, 3);
assert.equal(res.stats.passes, 0);
assert.equal(res.stats.failures, 0);
assert.equal(res.code, 0);
Expand Down

0 comments on commit 46b0e14

Please sign in to comment.