diff --git a/lib/runner.js b/lib/runner.js index 697ffb65ac..b78af9e83c 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -313,15 +313,10 @@ Runner.prototype.hook = function (name, fn) { } if (err) { if (err instanceof Pending) { - if (name === 'beforeEach' || name === 'afterEach') { - self.test.pending = true; - } else { - utils.forEach(suite.tests, function (test) { - test.pending = true; + utils.forEach(suite.tests.concat(suite._beforeEach), + function (runnable) { + runnable.pending = true; }); - // a pending hook won't be executed twice. - hook.pending = true; - } } else { self.failHook(hook, err); @@ -567,10 +562,6 @@ Runner.prototype.runTests = function (suite, fn) { } self.emit('test end', test); - if (err instanceof Pending) { - return next(); - } - return self.hookUp('afterEach', next); } diff --git a/test/integration/regression.spec.js b/test/integration/regression.spec.js index ffd17aa979..d9bc69f692 100644 --- a/test/integration/regression.spec.js +++ b/test/integration/regression.spec.js @@ -5,6 +5,7 @@ var fs = require('fs'); var path = require('path'); var run = require('./helpers').runMocha; var runJSON = require('./helpers').runMochaJSON; +var map = require('../../lib/utils').map; describe('regressions', function () { it('issue-1327: should run all 3 specs exactly once', function (done) { @@ -54,17 +55,118 @@ describe('regressions', function () { }); }); - describe('issue-2286: after doesn\'t execute if test was skipped in beforeEach', function () { - var afterWasRun = false; - describe('suite with skipped test for meta test', function () { - beforeEach(function () { this.skip(); }); - after(function () { afterWasRun = true; }); - it('should be pending', function () {}); - }); - after('meta test', function () { - afterWasRun.should.be.ok(); + describe( + "issue-2286: after doesn't execute if test was skipped in beforeEach", + function () { + /** + * Generates tests for behavior of `this.skip()` + * @param {string} name - Name of Runnable abort via `this.skip()` + * @param {Array.} expected - For each of the Runnable types, + * whether or not the Runnable should have been run. The order is + * "beforeAll" x 2, "beforeEach" x 2, "test" x 2, "afterEach" x 2, then + * "afterAll" x 2. There should be 10 items in this array. + */ + function testPendingRunnables (name, expected) { + var spies = []; + + function spy (skip) { + function wrapped () { + wrapped.wasRun = true; + if (skip) { + this.skip(); + } + } + + wrapped.wasRun = false; + spies.push(wrapped); + return wrapped; + } + + describe(name, function () { + describe('meta', function () { + before(spy(name === 'beforeAll')); + before(spy(name === 'beforeAll')); + beforeEach(spy(name === 'beforeEach')); + beforeEach(spy(name === 'beforeEach')); + it('should be pending', spy(name === 'test')); + it('should be pending', spy(name === 'test')); + afterEach(spy(name === 'afterEach')); + afterEach(spy(name === 'afterEach')); + after(spy(name === 'afterAll')); + after(spy(name === 'afterAll')); + }); + + after(name, function () { + map(spies, function (spy) { + return spy.wasRun; + }) + .should + .deepEqual(expected); + }); + }); + } + + testPendingRunnables('beforeAll', [ + true, // beforeAll + true, + false, // beforeEach + false, + false, // test + false, + false, // afterEach + false, + true, // afterAll + true + ]); + testPendingRunnables('beforeEach', [ + true, // beforeAll + true, + true, // beforeEach + false, + false, // test + false, + false, // afterEach + false, + true, // afterAll + true + ]); + testPendingRunnables('test', [ + true, // beforeAll + true, + true, // beforeEach + true, + true, // test + true, + true, // afterEach + true, + true, // afterAll + true + ]); + testPendingRunnables('afterEach', [ + true, // beforeAll + true, + true, // beforeEach + true, + true, // test + false, + true, // afterEach + true, + true, // afterAll + true + ]); + testPendingRunnables('afterAll', [ + true, // beforeAll + true, + true, // beforeEach + true, + true, // test + true, + true, // afterEach + true, + true, // afterAll + true + ]); }); - }); it('issue-2315: cannot read property currentRetry of undefined', function (done) { runJSON('regression/issue-2315.js', [], function (err, res) {