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
7 changes: 6 additions & 1 deletion bin/_mocha
Expand Up @@ -107,7 +107,8 @@ program
.option('--trace-deprecation', 'show stack traces on deprecations')
.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('--delay', 'wait for async suite definition')
.option('--production', 'causes only, pending, and skipped tests to fail the suite');

program._name = 'mocha';

Expand Down Expand Up @@ -323,6 +324,10 @@ if (program.retries) {
mocha.suite.retries(program.retries);
}

// --production

if (program.production) mocha.productionMode();

// custom compiler support

var extensions = ['js'];
Expand Down
10 changes: 10 additions & 0 deletions lib/mocha.js
Expand Up @@ -483,6 +483,15 @@ Mocha.prototype.delay = function delay () {
return this;
};

/**
* Pending tests and test marked only or skip fail the suite
* @returns {Mocha}
*/
Mocha.prototype.productionMode = function () {
this.options.productionMode = true;
return this;
};

/**
* Run tests and invoke `fn()` when complete.
*
Expand All @@ -504,6 +513,7 @@ Mocha.prototype.run = function (fn) {
runner.hasOnly = options.hasOnly;
runner.asyncOnly = options.asyncOnly;
runner.allowUncaught = options.allowUncaught;
runner.productionMode = options.productionMode;
if (options.grep) {
runner.grep(options.grep, options.invert);
}
Expand Down
10 changes: 9 additions & 1 deletion lib/runner.js
Expand Up @@ -821,9 +821,17 @@ Runner.prototype.run = function (fn) {

// callback
this.on('end', function () {
var failures = self.failures;
if (self.productionMode) {
if (self.hasOnly) {
failures += self.stats.tests;
} else {
failures += self.stats.pending;
}
}
debug('end');
process.removeListener('uncaughtException', uncaught);
fn(self.failures);
fn(failures);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like updating self.failures to its new value before calling fn would be good for consistency.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

});

// uncaught exception
Expand Down
5 changes: 5 additions & 0 deletions test/integration/fixtures/options/production/only.js
@@ -0,0 +1,5 @@
describe('production mode - only', function () {
it('test1', function () {});
it.only('stest2', function () {});
it('test3', function () {});
});
5 changes: 5 additions & 0 deletions test/integration/fixtures/options/production/pending.js
@@ -0,0 +1,5 @@
describe('production mode - pending', function () {
it('test1', function () {});
it('test2');
it('test3', function () {});
});
5 changes: 5 additions & 0 deletions test/integration/fixtures/options/production/skipped.js
@@ -0,0 +1,5 @@
describe('production mode - skipped', function () {
it('test1', function () {});
it.skip('test2', function () {});
it('test3', function () {});
});
30 changes: 30 additions & 0 deletions test/integration/options.spec.js
Expand Up @@ -180,4 +180,34 @@ describe('options', function () {
});
});
});

describe('--production', function () {
before(function () {
args = ['--production'];
});

it('fails skipped tests', function (done) {
run('options/production/skipped.js', args, function (err, res) {
assert(!err);
assert.equal(res.code, 1);
done();
});
});

it('fails pending tests', function (done) {
run('options/production/pending.js', args, function (err, res) {
assert(!err);
assert.equal(res.code, 1);
done();
});
});

it('fails tests marked only', function (done) {
run('options/production/only.js', args, function (err, res) {
assert(!err);
assert.equal(res.code, 1);
done();
});
});
});
});