Skip to content

Commit

Permalink
Add --no-diff option (fixes #2514)
Browse files Browse the repository at this point in the history
  • Loading branch information
CapacitorSet authored and boneskull committed Dec 12, 2017
1 parent f7d6d8b commit cbdb02e
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 0 deletions.
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: 3 additions & 0 deletions lib/reporters/base.js
Expand Up @@ -208,7 +208,10 @@ exports.list = function (failures) {
if (err.uncaught) {
msg = 'Uncaught ' + msg;
}

// explicitly show diff
if (exports.hideDiff !== true && err.showDiff !== false && sameType(actual, expected) && expected !== undefined) {
escape = false;
if (showDiff(err)) {
stringifyDiffObjs(err);
fmt = color('error title', ' %s) %s:\n%s') + color('error stack', '\n%s\n');
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

0 comments on commit cbdb02e

Please sign in to comment.