From 0be3f78491bbbcdc4dcea660ee7bfd557a225d9c Mon Sep 17 00:00:00 2001 From: Juerg B <44573692+juergba@users.noreply.github.com> Date: Sun, 26 Jan 2020 06:52:47 +0100 Subject: [PATCH] Fix exception when skipping tests programmatically (#4165) --- lib/runner.js | 2 +- .../fixtures/pending/programmatic.fixture.js | 8 ++++++ test/integration/helpers.js | 15 +++++++++++ test/integration/pending.spec.js | 26 ++++++++++++++++--- 4 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 test/integration/fixtures/pending/programmatic.fixture.js diff --git a/lib/runner.js b/lib/runner.js index bf1d14b183..ceb1a24a4f 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -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); diff --git a/test/integration/fixtures/pending/programmatic.fixture.js b/test/integration/fixtures/pending/programmatic.fixture.js new file mode 100644 index 0000000000..65720be64c --- /dev/null +++ b/test/integration/fixtures/pending/programmatic.fixture.js @@ -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; }); diff --git a/test/integration/helpers.js b/test/integration/helpers.js index 78251c986b..0d65e91e6b 100644 --- a/test/integration/helpers.js +++ b/test/integration/helpers.js @@ -143,6 +143,8 @@ module.exports = { invokeMochaAsync: invokeMochaAsync, + invokeNode: invokeNode, + /** * Resolves the path to a fixture to the full path. */ @@ -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; diff --git a/test/integration/pending.spec.js b/test/integration/pending.spec.js index 7179ff187b..ae6a57f0ca 100644 --- a/test/integration/pending.spec.js +++ b/test/integration/pending.spec.js @@ -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() { @@ -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(); + }); + }); + }); });