Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add --forbid-only and --forbid-pending options #2696

Merged
merged 15 commits into from Jun 4, 2017
12 changes: 11 additions & 1 deletion bin/_mocha
Expand Up @@ -111,7 +111,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';

Expand Down Expand Up @@ -333,6 +335,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'];
Expand Down
20 changes: 20 additions & 0 deletions lib/mocha.js
Expand Up @@ -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.
*
Expand All @@ -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);
}
Expand Down
6 changes: 6 additions & 0 deletions lib/runner.js
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions 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 () {});
});
7 changes: 7 additions & 0 deletions 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 () {});
});
7 changes: 7 additions & 0 deletions 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 () {});
});
7 changes: 7 additions & 0 deletions 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 () {});
});
7 changes: 7 additions & 0 deletions 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 () {});
});
52 changes: 52 additions & 0 deletions test/integration/options.spec.js
Expand Up @@ -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();
});
});
});
});