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

Commit

Permalink
Print failures for pending/only forbidden (mochajs#2874)
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottFreeCode authored Sep 29, 2017
1 parent 2cd5d8e commit 3f42565
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 9 deletions.
34 changes: 25 additions & 9 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,10 @@ Runner.prototype.runTest = function (fn) {
if (!test) {
return;
}
if (this.forbidOnly && this.hasOnly) {
fn(new Error('`.only` forbidden'));
return;
}
if (this.asyncOnly) {
test.asyncOnly = true;
}
Expand Down Expand Up @@ -529,7 +533,13 @@ Runner.prototype.runTests = function (suite, fn) {
}

if (test.isPending()) {
self.emit('pending', test);
if (self.forbidPending) {
test.isPending = alwaysFalse;
self.fail(test, new Error('Pending test forbidden'));
delete test.isPending;
} else {
self.emit('pending', test);
}
self.emit('test end', test);
return next();
}
Expand All @@ -538,7 +548,13 @@ Runner.prototype.runTests = function (suite, fn) {
self.emit('test', self.test = test);
self.hookDown('beforeEach', function (err, errSuite) {
if (test.isPending()) {
self.emit('pending', test);
if (self.forbidPending) {
test.isPending = alwaysFalse;
self.fail(test, new Error('Pending test forbidden'));
delete test.isPending;
} else {
self.emit('pending', test);
}
self.emit('test end', test);
return next();
}
Expand All @@ -550,7 +566,9 @@ Runner.prototype.runTests = function (suite, fn) {
test = self.test;
if (err) {
var retry = test.currentRetry();
if (err instanceof Pending) {
if (err instanceof Pending && self.forbidPending) {
self.fail(test, new Error('Pending test forbidden'));
} else if (err instanceof Pending) {
test.pending = true;
self.emit('pending', test);
} else if (retry < test.retries()) {
Expand Down Expand Up @@ -586,6 +604,10 @@ Runner.prototype.runTests = function (suite, fn) {
next();
};

function alwaysFalse () {
return false;
}

/**
* Run the given `suite` and invoke the callback `fn()` when complete.
*
Expand Down Expand Up @@ -820,12 +842,6 @@ 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
5 changes: 5 additions & 0 deletions test/integration/fixtures/options/forbid-only/only-suite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

describe.only('forbid only - suite marked with only', function () {
it('test1', function () {});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

describe('forbid pending - before calls `skip()`', function () {
it('test', function () {});
before(function () { this.skip(); });
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

describe('forbid pending - beforeEach calls `skip()`', function () {
it('test', function () {});
beforeEach(function () { this.skip(); });
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

describe.skip('forbid pending - suite marked with skip', function () {
it('test1', function () {});
});
7 changes: 7 additions & 0 deletions test/integration/fixtures/options/forbid-pending/this.skip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

describe('forbid pending - test calls `skip()`', function () {
it('test1', function () {});
it('test2', function () { this.skip(); });
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 @@ -215,6 +215,8 @@ describe('options', function () {
});

describe('--forbid-only', function () {
var onlyErrorMessage = '`.only` forbidden';

before(function () {
args = ['--forbid-only'];
});
Expand All @@ -237,12 +239,24 @@ describe('options', function () {
return;
}
assert.equal(res.code, 1);
assert.equal(res.failures[0].err.message, onlyErrorMessage);
done();
});
});

it('fails if there are tests in suites marked only', function (done) {
run('options/forbid-only/only-suite.js', args, function (err, res) {
assert(!err);
assert.equal(res.code, 1);
assert.equal(res.failures[0].err.message, onlyErrorMessage);
done();
});
});
});

describe('--forbid-pending', function () {
var pendingErrorMessage = 'Pending test forbidden';

before(function () {
args = ['--forbid-pending'];
});
Expand All @@ -265,6 +279,7 @@ describe('options', function () {
return;
}
assert.equal(res.code, 1);
assert.equal(res.failures[0].err.message, pendingErrorMessage);
done();
});
});
Expand All @@ -276,6 +291,43 @@ describe('options', function () {
return;
}
assert.equal(res.code, 1);
assert.equal(res.failures[0].err.message, pendingErrorMessage);
done();
});
});

it('fails if tests call `skip()`', function (done) {
run('options/forbid-pending/this.skip.js', args, function (err, res) {
assert(!err);
assert.equal(res.code, 1);
assert.equal(res.failures[0].err.message, pendingErrorMessage);
done();
});
});

it('fails if beforeEach calls `skip()`', function (done) {
run('options/forbid-pending/beforeEach-this.skip.js', args, function (err, res) {
assert(!err);
assert.equal(res.code, 1);
assert.equal(res.failures[0].err.message, pendingErrorMessage);
done();
});
});

it('fails if before calls `skip()`', function (done) {
run('options/forbid-pending/before-this.skip.js', args, function (err, res) {
assert(!err);
assert.equal(res.code, 1);
assert.equal(res.failures[0].err.message, pendingErrorMessage);
done();
});
});

it('fails if there are tests in suites marked skip', function (done) {
run('options/forbid-pending/skip-suite.js', args, function (err, res) {
assert(!err);
assert.equal(res.code, 1);
assert.equal(res.failures[0].err.message, pendingErrorMessage);
done();
});
});
Expand Down

0 comments on commit 3f42565

Please sign in to comment.