Skip to content

Commit 0be3f78

Browse files
authored
Fix exception when skipping tests programmatically (#4165)
1 parent c0f1d14 commit 0be3f78

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

lib/runner.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ Runner.prototype.runTests = function(suite, fn) {
654654
self.emit(constants.EVENT_TEST_END, test);
655655
// skip inner afterEach hooks below errSuite level
656656
var origSuite = self.suite;
657-
self.suite = errSuite;
657+
self.suite = errSuite || self.suite;
658658
return self.hookUp(HOOK_TYPE_AFTER_EACH, function(e, eSuite) {
659659
self.suite = origSuite;
660660
next(e, eSuite);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
const Mocha = require('../../../../lib/mocha');
3+
4+
const mocha = new Mocha({reporter: 'json'});
5+
mocha.addFile("./test/integration/fixtures/__default__.fixture.js");
6+
7+
const runner = mocha.run();
8+
runner.on('test', function (test) { test.pending = true; });

test/integration/helpers.js

+15
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ module.exports = {
143143

144144
invokeMochaAsync: invokeMochaAsync,
145145

146+
invokeNode: invokeNode,
147+
146148
/**
147149
* Resolves the path to a fixture to the full path.
148150
*/
@@ -227,6 +229,19 @@ function invokeMochaAsync(args, opts) {
227229
return [mochaProcess, resultPromise];
228230
}
229231

232+
/**
233+
* Invokes Node without Mocha binary with the given arguments,
234+
* when Mocha is used programmatically.
235+
*/
236+
function invokeNode(args, fn, opts) {
237+
if (typeof args === 'function') {
238+
opts = fn;
239+
fn = args;
240+
args = [];
241+
}
242+
return _spawnMochaWithListeners(args, fn, opts);
243+
}
244+
230245
function invokeSubMocha(args, fn, opts) {
231246
if (typeof args === 'function') {
232247
opts = fn;

test/integration/pending.spec.js

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
'use strict';
22

33
var assert = require('assert');
4-
var run = require('./helpers').runMochaJSON;
5-
var runMocha = require('./helpers').runMocha;
6-
var splitRegExp = require('./helpers').splitRegExp;
4+
var helpers = require('./helpers');
5+
var run = helpers.runMochaJSON;
6+
var runMocha = helpers.runMocha;
7+
var splitRegExp = helpers.splitRegExp;
8+
var invokeNode = helpers.invokeNode;
9+
var toJSONRunResult = helpers.toJSONRunResult;
710
var args = [];
811

912
describe('pending', function() {
@@ -323,4 +326,21 @@ describe('pending', function() {
323326
});
324327
});
325328
});
329+
330+
describe('programmatic usage', function() {
331+
it('should skip the test by listening to test event', function(done) {
332+
var path = require.resolve('./fixtures/pending/programmatic.fixture.js');
333+
invokeNode([path], function(err, res) {
334+
if (err) {
335+
return done(err);
336+
}
337+
var result = toJSONRunResult(res);
338+
expect(result, 'to have passed')
339+
.and('to have passed test count', 0)
340+
.and('to have pending test count', 1)
341+
.and('to have pending test order', 'should succeed');
342+
done();
343+
});
344+
});
345+
});
326346
});

0 commit comments

Comments
 (0)