Skip to content

Commit 3bd2d28

Browse files
authored
Forbid this.skip() within afterAll hooks (#4136)
1 parent 24c22be commit 3bd2d28

File tree

4 files changed

+29
-32
lines changed

4 files changed

+29
-32
lines changed

docs/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -666,9 +666,9 @@ describe('outer', function() {
666666
});
667667
```
668668

669-
Skipping a test within an "after all" hook is deprecated and will throw an exception in a future version of Mocha. Use a return statement or other means to abort hook execution.
669+
> _Updated in v7.0.0. Skipping a test within an "after all" hook is disallowed and will throw an exception. Use a return statement or other means to abort hook execution._
670670
671-
> Before Mocha v3.0.0, `this.skip()` was not supported in asynchronous tests and hooks.
671+
Before Mocha v3.0.0, `this.skip()` was not supported in asynchronous tests and hooks.
672672

673673
## Retry Tests
674674

lib/runner.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ var sQuote = utils.sQuote;
2424
var stackFilter = utils.stackTraceFilter();
2525
var stringify = utils.stringify;
2626
var type = utils.type;
27-
var createInvalidExceptionError = require('./errors')
28-
.createInvalidExceptionError;
27+
var errors = require('./errors');
28+
var createInvalidExceptionError = errors.createInvalidExceptionError;
29+
var createUnsupportedError = errors.createUnsupportedError;
2930

3031
/**
3132
* Non-enumerable globals.
@@ -387,12 +388,6 @@ Runner.prototype.hook = function(name, fn) {
387388
}
388389
// conditional skip
389390
if (hook.pending) {
390-
if (name === HOOK_TYPE_AFTER_ALL) {
391-
utils.deprecate(
392-
'Skipping a test within an "after all" hook is DEPRECATED and will throw an exception in a future version of Mocha. ' +
393-
'Use a return statement or other means to abort hook execution.'
394-
);
395-
}
396391
if (name === HOOK_TYPE_AFTER_EACH) {
397392
// TODO define and implement use case
398393
if (self.test) {
@@ -405,14 +400,18 @@ Runner.prototype.hook = function(name, fn) {
405400
self.emit(constants.EVENT_HOOK_END, hook);
406401
hook.pending = false; // activates hook for next test
407402
return fn(new Error('abort hookDown'));
408-
} else {
409-
// TODO throw error for afterAll
403+
} else if (name === HOOK_TYPE_BEFORE_ALL) {
410404
suite.tests.forEach(function(test) {
411405
test.pending = true;
412406
});
413407
suite.suites.forEach(function(suite) {
414408
suite.pending = true;
415409
});
410+
} else {
411+
hook.pending = false;
412+
var errForbid = createUnsupportedError('`this.skip` forbidden');
413+
self.failHook(hook, errForbid);
414+
return fn(errForbid);
416415
}
417416
} else if (err) {
418417
self.failHook(hook, err);

test/integration/fixtures/pending/skip-sync-after.fixture.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
describe('skip in after', function () {
44
it('should run this test-1', function () {});
55

6-
after('should print DeprecationWarning', function () {
6+
after('should throw "this.skip forbidden"', function () {
77
this.skip();
8-
throw new Error('never throws this error');
98
});
109

1110
describe('inner suite', function () {

test/integration/pending.spec.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,24 +73,23 @@ describe('pending', function() {
7373
});
7474

7575
describe('in after', function() {
76-
it('should run all tests', function(done) {
77-
runMocha(
78-
'pending/skip-sync-after.fixture.js',
79-
args,
80-
function(err, res) {
81-
if (err) {
82-
return done(err);
83-
}
84-
expect(res, 'to have passed').and('to satisfy', {
85-
passing: 3,
86-
failing: 0,
87-
pending: 0,
88-
output: expect.it('to contain', '"after all" hook is DEPRECATED')
89-
});
90-
done();
91-
},
92-
'pipe'
93-
);
76+
it('should throw, but run all tests', function(done) {
77+
run('pending/skip-sync-after.fixture.js', args, function(err, res) {
78+
if (err) {
79+
return done(err);
80+
}
81+
expect(res, 'to have failed with error', '`this.skip` forbidden')
82+
.and('to have failed test count', 1)
83+
.and('to have pending test count', 0)
84+
.and('to have passed test count', 3)
85+
.and(
86+
'to have passed test order',
87+
'should run this test-1',
88+
'should run this test-2',
89+
'should run this test-3'
90+
);
91+
done();
92+
});
9493
});
9594
});
9695

0 commit comments

Comments
 (0)