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 a description with the reason why a test is pending #3324

Closed
wants to merge 12 commits into from
5 changes: 3 additions & 2 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ Context.prototype.slow = function(ms) {
*
* @api private
* @throws Pending
* @param {string} reason
*/
Context.prototype.skip = function() {
this.runnable().skip();
Context.prototype.skip = function(reason) {
this.runnable().skip(reason);
};

/**
Expand Down
9 changes: 8 additions & 1 deletion lib/reporters/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,25 @@ function JSONReporter(runner) {
* @return {Object}
*/
function clean(test) {
var res;
var err = test.err || {};
if (err instanceof Error) {
err = errorJSON(err);
}

return {
res = {
title: test.title,
fullTitle: test.fullTitle(),
duration: test.duration,
currentRetry: test.currentRetry(),
err: cleanCycles(err)
};

if (test.reason) {
res.reason = test.reason;
}

return res;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion lib/reporters/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ function Spec(runner) {

runner.on('pending', function(test) {
var fmt = indent() + color('pending', ' - %s');
console.log(fmt, test.title);
console.log(
fmt,
test.title + (test.reason ? ' (' + test.reason + ')' : '')
);
});

runner.on('pass', function(test) {
Expand Down
9 changes: 7 additions & 2 deletions lib/runnable.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,14 @@ Runnable.prototype.enableTimeouts = function(enabled) {
* @memberof Mocha.Runnable
* @public
* @api public
* @param {string} reason - Reason the Runnable is being skipped.
*/
Runnable.prototype.skip = function() {
throw new Pending('sync skip');
Runnable.prototype.skip = function(reason) {
if (arguments.length > 0) {
reason = String(reason);
this.reason = reason;
}
Copy link
Member

Choose a reason for hiding this comment

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

Why just check if (reason) {?

In a case ofit.skip(), arguments is { '0': undefined } and arguments.length is 1, because of this.runnable().skip(reason) in lib/context.js.
Therefore, reason is 'undefined' as string and it print like this:

- should skip immediately with reason (undefined)

Copy link
Author

Choose a reason for hiding this comment

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

Ah, good catch. Originally I has if (reason) {, but thought this would be better for some reason. I'll change it back.

Copy link
Author

@rdennis rdennis May 19, 2018

Choose a reason for hiding this comment

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

I also considered using utils.isString to validate the input and throw an exception if invalid, but wasn't sure if that was needed here.

throw new Pending(reason || 'sync skip');
};

/**
Expand Down
12 changes: 12 additions & 0 deletions test/integration/fixtures/pending/skip-with-reason.fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

describe('skip in test with reason', function () {
it('should skip immediately with reason', function () {
this.skip('skip reason');
throw new Error('never thrown');
});

it('should run other tests in the suite', function () {
// Do nothing
});
});
15 changes: 15 additions & 0 deletions test/integration/pending.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ describe('pending', function() {
done();
});
});

it('should allow a skip reason', function(done) {
run('pending/skip-with-reason.fixture.js', args, function(err, res) {
if (err) {
done(err);
return;
}
assert.equal(res.stats.pending, 1);
assert.equal(res.stats.passes, 1);
assert.equal(res.stats.failures, 0);
assert.equal(res.code, 0);
assert.equal(res.pending[0].reason, 'skip reason');
done();
});
});
});

describe('in before', function() {
Expand Down
14 changes: 14 additions & 0 deletions test/reporters/spec.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('Spec reporter', function() {
var runner;
var useColors;
var expectedTitle = 'expectedTitle';
var expectedReason = 'expected reason';

beforeEach(function() {
stdout = [];
Expand Down Expand Up @@ -52,6 +53,19 @@ describe('Spec reporter', function() {
var expectedArray = [' - ' + expectedTitle + '\n'];
expect(stdout, 'to equal', expectedArray);
});
it('should return title and reason', function() {
var suite = {
title: expectedTitle,
reason: expectedReason
};
runner = createMockRunner('pending test', 'pending', null, null, suite);
Spec.call({epilogue: function() {}}, runner);
process.stdout.write = stdoutWrite;
var expectedArray = [
' - ' + expectedTitle + ' (' + expectedReason + ')' + '\n'
];
expect(stdout, 'to equal', expectedArray);
});
});
describe('on pass', function() {
describe('if test speed is slow', function() {
Expand Down
4 changes: 3 additions & 1 deletion test/reporters/xunit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('XUnit reporter', function() {
var expectedMessage = 'some message';
var expectedStack = 'some-stack';
var expectedWrite = null;
var expectedReason = 'some reason';

beforeEach(function() {
stdout = [];
Expand Down Expand Up @@ -255,7 +256,8 @@ describe('XUnit reporter', function() {
return expectedClassName;
}
},
duration: 1000
duration: 1000,
reason: expectedReason
};
xunit.test.call(
{
Expand Down