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 @@ -107,7 +107,9 @@ 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('--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 @@ -323,6 +325,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
9 changes: 8 additions & 1 deletion lib/runner.js
Expand Up @@ -821,9 +821,16 @@ Runner.prototype.run = function (fn) {

// callback
this.on('end', function () {
var failures = self.failures;
if (self.forbidOnly && self.hasOnly) {
failures += self.stats.tests;
}
if (self.forbidPending) {
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/forbid-only/only.js
@@ -0,0 +1,5 @@
describe('forbid only - test marked with only', function () {
it('test1', function () {});
it.only('test2', function () {});
it('test3', function () {});
});
5 changes: 5 additions & 0 deletions test/integration/fixtures/options/forbid-only/passed.js
@@ -0,0 +1,5 @@
describe('forbid only - all test pass', function () {
Copy link
Contributor

Choose a reason for hiding this comment

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

test -> tests

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 'forbid only - `.only` is not used' would describe the suite better.

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

it('test1', function () {});
it('test2', function () {});
it('test3', function () {});
});
5 changes: 5 additions & 0 deletions test/integration/fixtures/options/forbid-pending/passed.js
@@ -0,0 +1,5 @@
describe('forbid pending - all test pass', function () {
it('test1', function () {});
it('test2', function () {});
it('test3', function () {});
});
5 changes: 5 additions & 0 deletions test/integration/fixtures/options/forbid-pending/pending.js
@@ -0,0 +1,5 @@
describe('forbid pending - test without function', function () {
it('test1', function () {});
it('test2');
it('test3', function () {});
});
5 changes: 5 additions & 0 deletions test/integration/fixtures/options/forbid-pending/skip.js
@@ -0,0 +1,5 @@
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();
});
});
});
});