Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Mocha aborts when 'test' is set to 'pending' programmatically #4165

Merged
merged 3 commits into from
Jan 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ Runner.prototype.runTests = function(suite, fn) {
self.emit(constants.EVENT_TEST_END, test);
// skip inner afterEach hooks below errSuite level
var origSuite = self.suite;
self.suite = errSuite;
self.suite = errSuite || self.suite;
return self.hookUp(HOOK_TYPE_AFTER_EACH, function(e, eSuite) {
self.suite = origSuite;
next(e, eSuite);
Expand Down
8 changes: 8 additions & 0 deletions test/integration/fixtures/pending/programmatic.fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';
const Mocha = require('../../../../lib/mocha');

const mocha = new Mocha({reporter: 'json'});
mocha.addFile("./test/integration/fixtures/__default__.fixture.js");

const runner = mocha.run();
runner.on('test', function (test) { test.pending = true; });
15 changes: 15 additions & 0 deletions test/integration/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ module.exports = {

invokeMochaAsync: invokeMochaAsync,

invokeNode: invokeNode,

/**
* Resolves the path to a fixture to the full path.
*/
Expand Down Expand Up @@ -227,6 +229,19 @@ function invokeMochaAsync(args, opts) {
return [mochaProcess, resultPromise];
}

/**
* Invokes Node without Mocha binary with the given arguments,
* when Mocha is used programmatically.
*/
function invokeNode(args, fn, opts) {
if (typeof args === 'function') {
opts = fn;
fn = args;
args = [];
}
return _spawnMochaWithListeners(args, fn, opts);
}

function invokeSubMocha(args, fn, opts) {
if (typeof args === 'function') {
opts = fn;
Expand Down
26 changes: 23 additions & 3 deletions test/integration/pending.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
'use strict';

var assert = require('assert');
var run = require('./helpers').runMochaJSON;
var runMocha = require('./helpers').runMocha;
var splitRegExp = require('./helpers').splitRegExp;
var helpers = require('./helpers');
var run = helpers.runMochaJSON;
var runMocha = helpers.runMocha;
var splitRegExp = helpers.splitRegExp;
var invokeNode = helpers.invokeNode;
var toJSONRunResult = helpers.toJSONRunResult;
var args = [];

describe('pending', function() {
Expand Down Expand Up @@ -323,4 +326,21 @@ describe('pending', function() {
});
});
});

describe('programmatic usage', function() {
it('should skip the test by listening to test event', function(done) {
var path = require.resolve('./fixtures/pending/programmatic.fixture.js');
invokeNode([path], function(err, res) {
if (err) {
return done(err);
}
var result = toJSONRunResult(res);
expect(result, 'to have passed')
.and('to have passed test count', 0)
.and('to have pending test count', 1)
.and('to have pending test order', 'should succeed');
done();
});
});
});
});