Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit 97547b7

Browse files
charlierudolphdasilvacontin
authored andcommitted
Add --forbid-only and --forbid-pending options (mochajs#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
1 parent cf81cb2 commit 97547b7

File tree

9 files changed

+124
-1
lines changed

9 files changed

+124
-1
lines changed

bin/_mocha

+11-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ program
112112
.option('--use_strict', 'enforce strict mode')
113113
.option('--watch-extensions <ext>,...', 'additional extensions to monitor with --watch', list, [])
114114
.option('--delay', 'wait for async suite definition')
115-
.option('--allow-uncaught', 'enable uncaught errors to propagate');
115+
.option('--allow-uncaught', 'enable uncaught errors to propagate')
116+
.option('--forbid-only', 'causes test marked with only to fail the suite')
117+
.option('--forbid-pending', 'causes pending tests and test marked with skip to fail the suite');
116118

117119
program._name = 'mocha';
118120

@@ -334,6 +336,14 @@ if (program.retries) {
334336
mocha.suite.retries(program.retries);
335337
}
336338

339+
// --forbid-only
340+
341+
if (program.forbidOnly) mocha.forbidOnly();
342+
343+
// --forbid-pending
344+
345+
if (program.forbidPending) mocha.forbidPending();
346+
337347
// custom compiler support
338348

339349
var extensions = ['js'];

lib/mocha.js

+20
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,24 @@ Mocha.prototype.delay = function delay () {
483483
return this;
484484
};
485485

486+
/**
487+
* Tests marked only fail the suite
488+
* @returns {Mocha}
489+
*/
490+
Mocha.prototype.forbidOnly = function () {
491+
this.options.forbidOnly = true;
492+
return this;
493+
};
494+
495+
/**
496+
* Pending tests and tests marked skip fail the suite
497+
* @returns {Mocha}
498+
*/
499+
Mocha.prototype.forbidPending = function () {
500+
this.options.forbidPending = true;
501+
return this;
502+
};
503+
486504
/**
487505
* Run tests and invoke `fn()` when complete.
488506
*
@@ -504,6 +522,8 @@ Mocha.prototype.run = function (fn) {
504522
runner.hasOnly = options.hasOnly;
505523
runner.asyncOnly = options.asyncOnly;
506524
runner.allowUncaught = options.allowUncaught;
525+
runner.forbidOnly = options.forbidOnly;
526+
runner.forbidPending = options.forbidPending;
507527
if (options.grep) {
508528
runner.grep(options.grep, options.invert);
509529
}

lib/runner.js

+6
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,12 @@ Runner.prototype.run = function (fn) {
820820

821821
// callback
822822
this.on('end', function () {
823+
if (self.forbidOnly && self.hasOnly) {
824+
self.failures += self.stats.tests;
825+
}
826+
if (self.forbidPending) {
827+
self.failures += self.stats.pending;
828+
}
823829
debug('end');
824830
process.removeListener('uncaughtException', uncaught);
825831
fn(self.failures);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
describe('forbid only - test marked with only', function () {
4+
it('test1', function () {});
5+
it.only('test2', function () {});
6+
it('test3', function () {});
7+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
describe('forbid only - `.only` is not used', function () {
4+
it('test1', function () {});
5+
it('test2', function () {});
6+
it('test3', function () {});
7+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
describe('forbid pending - all test pass', function () {
4+
it('test1', function () {});
5+
it('test2', function () {});
6+
it('test3', function () {});
7+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
describe('forbid pending - test without function', function () {
4+
it('test1', function () {});
5+
it('test2');
6+
it('test3', function () {});
7+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
describe('forbid pending - test marked with skip', function () {
4+
it('test1', function () {});
5+
it.skip('test2', function () {});
6+
it('test3', function () {});
7+
});

test/integration/options.spec.js

+52
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,56 @@ describe('options', function () {
180180
});
181181
});
182182
});
183+
184+
describe('--forbid-only', function () {
185+
before(function () {
186+
args = ['--forbid-only'];
187+
});
188+
189+
it('succeeds if there are only passed tests', function (done) {
190+
run('options/forbid-only/passed.js', args, function (err, res) {
191+
assert(!err);
192+
assert.equal(res.code, 0);
193+
done();
194+
});
195+
});
196+
197+
it('fails if there are tests marked only', function (done) {
198+
run('options/forbid-only/only.js', args, function (err, res) {
199+
assert(!err);
200+
assert.equal(res.code, 1);
201+
done();
202+
});
203+
});
204+
});
205+
206+
describe('--forbid-pending', function () {
207+
before(function () {
208+
args = ['--forbid-pending'];
209+
});
210+
211+
it('succeeds if there are only passed tests', function (done) {
212+
run('options/forbid-pending/passed.js', args, function (err, res) {
213+
assert(!err);
214+
assert.equal(res.code, 0);
215+
done();
216+
});
217+
});
218+
219+
it('fails if there are tests marked skip', function (done) {
220+
run('options/forbid-pending/skip.js', args, function (err, res) {
221+
assert(!err);
222+
assert.equal(res.code, 1);
223+
done();
224+
});
225+
});
226+
227+
it('fails if there are pending tests', function (done) {
228+
run('options/forbid-pending/pending.js', args, function (err, res) {
229+
assert(!err);
230+
assert.equal(res.code, 1);
231+
done();
232+
});
233+
});
234+
});
183235
});

0 commit comments

Comments
 (0)