Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

Add --forbid-only and --forbid-pending options (#2696)

Charlie Rudolph2bc9c4d1d638
diff --git a/bin/_mocha b/bin/_mocha
index e47d0bd8..c37d49fe 100755
--- a/bin/_mocha
+++ b/bin/_mocha
@@ -112,7 +112,9 @@ program
.option('--use_strict', 'enforce strict mode')
.option('--watch-extensions <ext>,...', '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 8bacf27b..bfc0238d 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 b024e0dc..039065a2 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 00000000..c09e8041
--- /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 00000000..6c4d4ac1
--- /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 00000000..cd12668f
--- /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 00000000..96abd0bf
--- /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 00000000..c6fd31d3
--- /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 7c7a3cc7..325fbada 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();
+ });
+ });
+ });
});