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 --no-diff option (fixes #2514) #2536

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions bin/_mocha
Expand Up @@ -90,6 +90,7 @@ program
.option('--preserve-symlinks', 'Instructs the module loader to preserve symbolic links when resolving and caching modules')
.option('--icu-data-dir', 'include ICU data')
.option('--inline-diffs', 'display actual/expected differences inline within each string')
.option('--no-diff', 'do not show a diff on failure')
.option('--inspect', 'activate devtools in chrome')
.option('--inspect-brk', 'activate devtools in chrome and break on the first line')
.option('--interfaces', 'display available interfaces')
Expand Down Expand Up @@ -250,6 +251,12 @@ if (program.inlineDiffs) {
mocha.useInlineDiffs(true);
}

// --no-diff

if (process.argv.indexOf('--no-diff') !== -1) {
mocha.hideDiff(true);
}

// --slow <ms>

if (program.slow) {
Expand Down
15 changes: 15 additions & 0 deletions lib/mocha.js
Expand Up @@ -389,6 +389,20 @@ Mocha.prototype.useInlineDiffs = function (inlineDiffs) {
return this;
};

/**
* Do not show diffs at all.
*
* @param {Boolean} hideDiff
* @return {Mocha}
* @api public
* @param {boolean} hideDiff
* @return {Mocha}
*/
Mocha.prototype.hideDiff = function (hideDiff) {
this.options.hideDiff = hideDiff !== undefined && hideDiff;
return this;
};

/**
* Set the timeout in milliseconds.
*
Expand Down Expand Up @@ -545,6 +559,7 @@ Mocha.prototype.run = function (fn) {
exports.reporters.Base.useColors = options.useColors;
}
exports.reporters.Base.inlineDiffs = options.useInlineDiffs;
exports.reporters.Base.hideDiff = options.hideDiff;

function done (failures) {
if (reporter.done) {
Expand Down
3 changes: 2 additions & 1 deletion lib/reporters/base.js
Expand Up @@ -208,8 +208,9 @@ exports.list = function (failures) {
if (err.uncaught) {
msg = 'Uncaught ' + msg;
}

// explicitly show diff
if (showDiff(err)) {
if (!exports.hideDiff && showDiff(err)) {
stringifyDiffObjs(err);
fmt = color('error title', ' %s) %s:\n%s') + color('error stack', '\n%s\n');
var match = message.match(/^([^:]+): expected/);
Expand Down
8 changes: 8 additions & 0 deletions test/integration/fixtures/no-diff.fixture.js
@@ -0,0 +1,8 @@
'use strict';
var assert = require('assert');

describe('Example test', function () {
it('should fail', function () {
assert.deepEqual([1, 2, 3], ['foo', 'bar', 'baz']);
});
});
14 changes: 14 additions & 0 deletions test/integration/no-diff.spec.js
@@ -0,0 +1,14 @@
'use strict';

var helpers = require('./helpers');
var run = helpers.runMocha;

describe('no-diff', function () {
it('should be honoured', function (done) {
run('no-diff.fixture.js', ['--no-diff'], function (err, res) {
res.output.should.not.match(/\+ expected/);
res.output.should.not.match(/- actual/);
done(err);
});
});
});
15 changes: 15 additions & 0 deletions test/reporters/base.spec.js
Expand Up @@ -96,6 +96,21 @@ describe('Base reporter', function () {
expect(errOut).to.not.match(/- actual/);
expect(errOut).to.not.match(/\+ expected/);
});

it('should not show diffs when hideDiff is set', function () {
var err = new Assert({ actual: 'foo', expected: 'bar' });
var errOut;

var test = makeTest(err);

Base.hideDiff = true;
Base.list([test]);
Base.hideDiff = false; // Revert to original value

errOut = stdout.join('\n');
errOut.should.not.match(/\- actual/);
errOut.should.not.match(/\+ expected/);
});
});

describe('Getting two strings', function () {
Expand Down