From 97547b7ce4e797116e9f0037990b09f8be4589e9 Mon Sep 17 00:00:00 2001 From: Charlie Rudolph Date: Sun, 4 Jun 2017 05:04:11 -0700 Subject: [PATCH] Add --forbid-only and --forbid-pending options (#2696) * add --production option * update comment * fix lint * add tests * remove only * update * simplify * remove onlys * fix and * update self.failures * update test description * Remove breaking semicolon * fix lint --- bin/_mocha | 12 ++++- lib/mocha.js | 20 +++++++ lib/runner.js | 6 +++ .../fixtures/options/forbid-only/only.js | 7 +++ .../fixtures/options/forbid-only/passed.js | 7 +++ .../fixtures/options/forbid-pending/passed.js | 7 +++ .../options/forbid-pending/pending.js | 7 +++ .../fixtures/options/forbid-pending/skip.js | 7 +++ test/integration/options.spec.js | 52 +++++++++++++++++++ 9 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 test/integration/fixtures/options/forbid-only/only.js create mode 100644 test/integration/fixtures/options/forbid-only/passed.js create mode 100644 test/integration/fixtures/options/forbid-pending/passed.js create mode 100644 test/integration/fixtures/options/forbid-pending/pending.js create mode 100644 test/integration/fixtures/options/forbid-pending/skip.js diff --git a/bin/_mocha b/bin/_mocha index e47d0bd81b..c37d49fef5 100755 --- a/bin/_mocha +++ b/bin/_mocha @@ -112,7 +112,9 @@ program .option('--use_strict', 'enforce strict mode') .option('--watch-extensions ,...', 'additional extensions to monitor with --watch', list, []) .option('--delay', 'wait for async suite definition') - .option('--allow-uncaught', 'enable uncaught errors to propagate'); + .option('--allow-uncaught', 'enable uncaught errors to propagate') + .option('--forbid-only', 'causes test marked with only to fail the suite') + .option('--forbid-pending', 'causes pending tests and test marked with skip to fail the suite'); program._name = 'mocha'; @@ -334,6 +336,14 @@ if (program.retries) { mocha.suite.retries(program.retries); } +// --forbid-only + +if (program.forbidOnly) mocha.forbidOnly(); + +// --forbid-pending + +if (program.forbidPending) mocha.forbidPending(); + // custom compiler support var extensions = ['js']; diff --git a/lib/mocha.js b/lib/mocha.js index 8bacf27b17..bfc0238d46 100644 --- a/lib/mocha.js +++ b/lib/mocha.js @@ -483,6 +483,24 @@ Mocha.prototype.delay = function delay () { return this; }; +/** + * Tests marked only fail the suite + * @returns {Mocha} + */ +Mocha.prototype.forbidOnly = function () { + this.options.forbidOnly = true; + return this; +}; + +/** + * Pending tests and tests marked skip fail the suite + * @returns {Mocha} + */ +Mocha.prototype.forbidPending = function () { + this.options.forbidPending = true; + return this; +}; + /** * Run tests and invoke `fn()` when complete. * @@ -504,6 +522,8 @@ Mocha.prototype.run = function (fn) { runner.hasOnly = options.hasOnly; runner.asyncOnly = options.asyncOnly; runner.allowUncaught = options.allowUncaught; + runner.forbidOnly = options.forbidOnly; + runner.forbidPending = options.forbidPending; if (options.grep) { runner.grep(options.grep, options.invert); } diff --git a/lib/runner.js b/lib/runner.js index b024e0dcb5..039065a2e0 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -820,6 +820,12 @@ Runner.prototype.run = function (fn) { // callback this.on('end', function () { + if (self.forbidOnly && self.hasOnly) { + self.failures += self.stats.tests; + } + if (self.forbidPending) { + self.failures += self.stats.pending; + } debug('end'); process.removeListener('uncaughtException', uncaught); fn(self.failures); diff --git a/test/integration/fixtures/options/forbid-only/only.js b/test/integration/fixtures/options/forbid-only/only.js new file mode 100644 index 0000000000..c09e804126 --- /dev/null +++ b/test/integration/fixtures/options/forbid-only/only.js @@ -0,0 +1,7 @@ +'use strict'; + +describe('forbid only - test marked with only', function () { + it('test1', function () {}); + it.only('test2', function () {}); + it('test3', function () {}); +}); diff --git a/test/integration/fixtures/options/forbid-only/passed.js b/test/integration/fixtures/options/forbid-only/passed.js new file mode 100644 index 0000000000..6c4d4ac1d4 --- /dev/null +++ b/test/integration/fixtures/options/forbid-only/passed.js @@ -0,0 +1,7 @@ +'use strict'; + +describe('forbid only - `.only` is not used', function () { + it('test1', function () {}); + it('test2', function () {}); + it('test3', function () {}); +}); diff --git a/test/integration/fixtures/options/forbid-pending/passed.js b/test/integration/fixtures/options/forbid-pending/passed.js new file mode 100644 index 0000000000..cd12668f4d --- /dev/null +++ b/test/integration/fixtures/options/forbid-pending/passed.js @@ -0,0 +1,7 @@ +'use strict'; + +describe('forbid pending - all test pass', function () { + it('test1', function () {}); + it('test2', function () {}); + it('test3', function () {}); +}); diff --git a/test/integration/fixtures/options/forbid-pending/pending.js b/test/integration/fixtures/options/forbid-pending/pending.js new file mode 100644 index 0000000000..96abd0bf47 --- /dev/null +++ b/test/integration/fixtures/options/forbid-pending/pending.js @@ -0,0 +1,7 @@ +'use strict'; + +describe('forbid pending - test without function', function () { + it('test1', function () {}); + it('test2'); + it('test3', function () {}); +}); diff --git a/test/integration/fixtures/options/forbid-pending/skip.js b/test/integration/fixtures/options/forbid-pending/skip.js new file mode 100644 index 0000000000..c6fd31d303 --- /dev/null +++ b/test/integration/fixtures/options/forbid-pending/skip.js @@ -0,0 +1,7 @@ +'use strict'; + +describe('forbid pending - test marked with skip', function () { + it('test1', function () {}); + it.skip('test2', function () {}); + it('test3', function () {}); +}); diff --git a/test/integration/options.spec.js b/test/integration/options.spec.js index 7c7a3cc75e..325fbada8f 100644 --- a/test/integration/options.spec.js +++ b/test/integration/options.spec.js @@ -180,4 +180,56 @@ describe('options', function () { }); }); }); + + describe('--forbid-only', function () { + before(function () { + args = ['--forbid-only']; + }); + + it('succeeds if there are only passed tests', function (done) { + run('options/forbid-only/passed.js', args, function (err, res) { + assert(!err); + assert.equal(res.code, 0); + done(); + }); + }); + + it('fails if there are tests marked only', function (done) { + run('options/forbid-only/only.js', args, function (err, res) { + assert(!err); + assert.equal(res.code, 1); + done(); + }); + }); + }); + + describe('--forbid-pending', function () { + before(function () { + args = ['--forbid-pending']; + }); + + it('succeeds if there are only passed tests', function (done) { + run('options/forbid-pending/passed.js', args, function (err, res) { + assert(!err); + assert.equal(res.code, 0); + done(); + }); + }); + + it('fails if there are tests marked skip', function (done) { + run('options/forbid-pending/skip.js', args, function (err, res) { + assert(!err); + assert.equal(res.code, 1); + done(); + }); + }); + + it('fails if there are pending tests', function (done) { + run('options/forbid-pending/pending.js', args, function (err, res) { + assert(!err); + assert.equal(res.code, 1); + done(); + }); + }); + }); });