Skip to content

Commit 28eb54b

Browse files
committed
fix weird "pending" behavior; closes #2286
1 parent 4662b31 commit 28eb54b

File tree

2 files changed

+125
-22
lines changed

2 files changed

+125
-22
lines changed

lib/runner.js

+13-12
Original file line numberDiff line numberDiff line change
@@ -313,15 +313,20 @@ Runner.prototype.hook = function (name, fn) {
313313
}
314314
if (err) {
315315
if (err instanceof Pending) {
316-
if (name === 'beforeEach' || name === 'afterEach') {
317-
self.test.pending = true;
318-
} else {
319-
utils.forEach(suite.tests, function (test) {
320-
test.pending = true;
321-
});
322-
// a pending hook won't be executed twice.
323-
hook.pending = true;
316+
var runnables = suite.tests;
317+
switch (name) {
318+
case 'beforeAll':
319+
runnables = runnables.concat(suite._beforeAll);
320+
// falls through
321+
case 'afterAll':
322+
runnables = runnables.concat(suite._afterAll);
323+
// falls through
324+
default:
325+
runnables = runnables.concat(suite._beforeEach);
324326
}
327+
utils.forEach(runnables, function (runnable) {
328+
runnable.pending = true;
329+
});
325330
} else {
326331
self.failHook(hook, err);
327332

@@ -567,10 +572,6 @@ Runner.prototype.runTests = function (suite, fn) {
567572
}
568573
self.emit('test end', test);
569574

570-
if (err instanceof Pending) {
571-
return next();
572-
}
573-
574575
return self.hookUp('afterEach', next);
575576
}
576577

test/integration/regression.spec.js

+112-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var fs = require('fs');
55
var path = require('path');
66
var run = require('./helpers').runMocha;
77
var runJSON = require('./helpers').runMochaJSON;
8+
var map = require('../../lib/utils').map;
89

910
describe('regressions', function () {
1011
it('issue-1327: should run all 3 specs exactly once', function (done) {
@@ -54,17 +55,118 @@ describe('regressions', function () {
5455
});
5556
});
5657

57-
describe('issue-2286: after doesn\'t execute if test was skipped in beforeEach', function () {
58-
var afterWasRun = false;
59-
describe('suite with skipped test for meta test', function () {
60-
beforeEach(function () { this.skip(); });
61-
after(function () { afterWasRun = true; });
62-
it('should be pending', function () {});
63-
});
64-
after('meta test', function () {
65-
afterWasRun.should.be.ok();
58+
describe(
59+
"issue-2286: after doesn't execute if test was skipped in beforeEach",
60+
function () {
61+
/**
62+
* Generates tests for behavior of `this.skip()`
63+
* @param {string} name - Name of Runnable abort via `this.skip()`
64+
* @param {Array.<boolean>} expected - For each of the Runnable types,
65+
* whether or not the Runnable should have been run. The order is
66+
* "beforeAll" x 2, "beforeEach" x 2, "test" x 2, "afterEach" x 2, then
67+
* "afterAll" x 2. There should be 10 items in this array.
68+
*/
69+
function testPendingRunnables (name, expected) {
70+
var spies = [];
71+
72+
function spy (skip) {
73+
function wrapped () {
74+
wrapped.wasRun = true;
75+
if (skip) {
76+
this.skip();
77+
}
78+
}
79+
80+
wrapped.wasRun = false;
81+
spies.push(wrapped);
82+
return wrapped;
83+
}
84+
85+
describe(name, function () {
86+
describe('meta', function () {
87+
before(spy(name === 'beforeAll'));
88+
before(spy(name === 'beforeAll'));
89+
beforeEach(spy(name === 'beforeEach'));
90+
beforeEach(spy(name === 'beforeEach'));
91+
it('should be pending', spy(name === 'test'));
92+
it('should be pending', spy(name === 'test'));
93+
afterEach(spy(name === 'afterEach'));
94+
afterEach(spy(name === 'afterEach'));
95+
after(spy(name === 'afterAll'));
96+
after(spy(name === 'afterAll'));
97+
});
98+
99+
after(name, function () {
100+
map(spies, function (spy) {
101+
return spy.wasRun;
102+
})
103+
.should
104+
.deepEqual(expected);
105+
});
106+
});
107+
}
108+
109+
testPendingRunnables('beforeAll', [
110+
true, // beforeAll
111+
false,
112+
false, // beforeEach
113+
false,
114+
false, // test
115+
false,
116+
false, // afterEach
117+
false,
118+
false, // afterAll
119+
false
120+
]);
121+
testPendingRunnables('beforeEach', [
122+
true, // beforeAll
123+
true,
124+
true, // beforeEach
125+
false,
126+
false, // test
127+
false,
128+
false, // afterEach
129+
false,
130+
true, // afterAll
131+
true
132+
]);
133+
testPendingRunnables('test', [
134+
true, // beforeAll
135+
true,
136+
true, // beforeEach
137+
true,
138+
true, // test
139+
true,
140+
true, // afterEach
141+
true,
142+
true, // afterAll
143+
true
144+
]);
145+
testPendingRunnables('afterEach', [
146+
true, // beforeAll
147+
true,
148+
true, // beforeEach
149+
true,
150+
true, // test
151+
false,
152+
true, // afterEach
153+
true,
154+
true, // afterAll
155+
true
156+
]);
157+
testPendingRunnables('afterAll', [
158+
true, // beforeAll
159+
true,
160+
true, // beforeEach
161+
true,
162+
true, // test
163+
true,
164+
true, // afterEach
165+
true,
166+
true, // afterAll
167+
false
168+
]);
66169
});
67-
});
68170

69171
it('issue-2315: cannot read property currentRetry of undefined', function (done) {
70172
runJSON('regression/issue-2315.js', [], function (err, res) {

0 commit comments

Comments
 (0)