Skip to content

Commit

Permalink
Merge e9e56eb into bc06b85
Browse files Browse the repository at this point in the history
  • Loading branch information
charlierudolph committed Feb 1, 2017
2 parents bc06b85 + e9e56eb commit 90cb521
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 2 deletions.
12 changes: 11 additions & 1 deletion bin/_mocha
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
});

// uncaught exception
Expand Down
5 changes: 5 additions & 0 deletions test/integration/fixtures/options/forbid-only/only.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('forbid only - 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/passed.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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();
});
});
});
});

0 comments on commit 90cb521

Please sign in to comment.