Skip to content

Commit

Permalink
fail-fast when .skip encountered in Suite w/ --forbid-pending
Browse files Browse the repository at this point in the history
Signed-off-by: Outsider <outsideris@gmail.com>
  • Loading branch information
outsideris committed Dec 5, 2018
1 parent d0c0cc0 commit 1b327ac
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 9 deletions.
29 changes: 22 additions & 7 deletions lib/interfaces/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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') {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

describe.skip('forbid pending - suite marked with skip', function() {});
37 changes: 35 additions & 2 deletions test/integration/options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 1b327ac

Please sign in to comment.