diff --git a/bin/_mocha b/bin/_mocha index cb7b5e63ce..b3c142425c 100755 --- a/bin/_mocha +++ b/bin/_mocha @@ -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') @@ -250,6 +251,12 @@ if (program.inlineDiffs) { mocha.useInlineDiffs(true); } +// --no-diff + +if (process.argv.indexOf('--no-diff') !== -1) { + mocha.hideDiff(true); +} + // --slow if (program.slow) { diff --git a/lib/mocha.js b/lib/mocha.js index 5d2ab9ac89..2484ea5be8 100644 --- a/lib/mocha.js +++ b/lib/mocha.js @@ -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. * @@ -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) { diff --git a/lib/reporters/base.js b/lib/reporters/base.js index 0ebc5e7db3..b10f12ad9b 100644 --- a/lib/reporters/base.js +++ b/lib/reporters/base.js @@ -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/); diff --git a/test/integration/fixtures/no-diff.fixture.js b/test/integration/fixtures/no-diff.fixture.js new file mode 100644 index 0000000000..22b80ae86e --- /dev/null +++ b/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']); + }); +}); diff --git a/test/integration/no-diff.spec.js b/test/integration/no-diff.spec.js new file mode 100644 index 0000000000..5766abce51 --- /dev/null +++ b/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); + }); + }); +}); diff --git a/test/reporters/base.spec.js b/test/reporters/base.spec.js index e38bdb9fa2..e9118186a7 100644 --- a/test/reporters/base.spec.js +++ b/test/reporters/base.spec.js @@ -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 () {