From 1b327ac0590235e2e6cc773c260381dbdd7bd09a Mon Sep 17 00:00:00 2001 From: Outsider Date: Thu, 6 Dec 2018 02:41:31 +0900 Subject: [PATCH] fail-fast when .skip encountered in Suite w/ --forbid-pending Signed-off-by: Outsider --- lib/interfaces/common.js | 29 +++++++++++---- .../forbid-pending/skip-empty-suite.js | 3 ++ test/integration/options.spec.js | 37 ++++++++++++++++++- 3 files changed, 60 insertions(+), 9 deletions(-) create mode 100644 test/integration/fixtures/options/forbid-pending/skip-empty-suite.js diff --git a/lib/interfaces/common.js b/lib/interfaces/common.js index 3d31ab7eb2..3c6dffa96f 100644 --- a/lib/interfaces/common.js +++ b/lib/interfaces/common.js @@ -12,6 +12,22 @@ var utils = require('../utils'); * @return {Object} An object containing common functions. */ module.exports = function(suites, context, mocha) { + /** + * Check if the suite should be tested. + * + * @private + * @param {Suite} suite - suite to check + * @returns {boolean} + */ + function shouldBeTested(suite) { + return ( + !mocha.options.grep || + (mocha.options.grep && + mocha.options.grep.test(suite.fullTitle()) && + !mocha.options.invert) + ); + } + return { /** * This is only present if flag --delay is passed into Mocha. It triggers @@ -108,18 +124,17 @@ module.exports = function(suites, context, mocha) { suite.file = opts.file; suites.unshift(suite); if (opts.isOnly) { - if ( - mocha.options.forbidOnly && - (!mocha.options.grep || - (mocha.options.grep && - mocha.options.grep.test(suite.fullTitle()) && - !mocha.options.invert)) - ) { + if (mocha.options.forbidOnly && shouldBeTested(suite)) { throw new Error('`.only` forbidden'); } suite.parent._onlySuites = suite.parent._onlySuites.concat(suite); } + if (suite.pending) { + if (mocha.options.forbidPending && shouldBeTested(suite)) { + throw new Error('Pending test forbidden'); + } + } if (typeof opts.fn === 'function') { var result = opts.fn.call(suite); if (typeof result !== 'undefined') { diff --git a/test/integration/fixtures/options/forbid-pending/skip-empty-suite.js b/test/integration/fixtures/options/forbid-pending/skip-empty-suite.js new file mode 100644 index 0000000000..0e9fec396f --- /dev/null +++ b/test/integration/fixtures/options/forbid-pending/skip-empty-suite.js @@ -0,0 +1,3 @@ +'use strict'; + +describe.skip('forbid pending - suite marked with skip', function() {}); diff --git a/test/integration/options.spec.js b/test/integration/options.spec.js index 1251333a45..0088b08ceb 100644 --- a/test/integration/options.spec.js +++ b/test/integration/options.spec.js @@ -417,13 +417,46 @@ describe('options', function() { }); }); + it('fails if there are tests in suites marked skip', function(done) { + runMocha('options/forbid-pending/skip-suite.js', args, function( + err, + res + ) { + if (err) { + done(err); + return; + } + expect(res, 'to satisfy', { + code: 1, + output: new RegExp(pendingErrorMessage) + }); + done(); + }); + }); + + it('fails if there is empty suite marked pending', function(done) { + runMocha('options/forbid-pending/skip-empty-suite.js', args, function( + err, + res + ) { + if (err) { + done(err); + return; + } + expect(res, 'to satisfy', { + code: 1, + output: new RegExp(pendingErrorMessage) + }); + done(); + }); + }); + var forbidPendingFailureTests = { 'fails if there are tests marked skip': 'skip.js', 'fails if there are pending tests': 'pending.js', 'fails if tests call `skip()`': 'this.skip.js', 'fails if beforeEach calls `skip()`': 'beforeEach-this.skip.js', - 'fails if before calls `skip()`': 'before-this.skip.js', - 'fails if there are tests in suites marked skip': 'skip-suite.js' + 'fails if before calls `skip()`': 'before-this.skip.js' }; Object.keys(forbidPendingFailureTests).forEach(function(title) {