From 49eb9b44e9382628459edbbabd84cc8daf395450 Mon Sep 17 00:00:00 2001 From: Capacitor Set Date: Thu, 13 Oct 2016 09:00:16 +0200 Subject: [PATCH] Add --no-diff option (fixes mochajs/mocha#2514) --- bin/_mocha | 7 +++++++ lib/mocha.js | 15 +++++++++++++++ lib/reporters/base.js | 3 ++- test/integration/fixtures/no-diff.fixture.js | 8 ++++++++ test/integration/no-diff.spec.js | 14 ++++++++++++++ test/reporters/base.spec.js | 15 +++++++++++++++ 6 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 test/integration/fixtures/no-diff.fixture.js create mode 100644 test/integration/no-diff.spec.js diff --git a/bin/_mocha b/bin/_mocha index 6cfc094ba1..b3f8d2f505 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('--interfaces', 'display available interfaces') .option('--no-deprecation', 'silence deprecation warnings') .option('--no-exit', 'require a clean shutdown of the event loop: mocha will not call process.exit') @@ -242,6 +243,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 dcc1492f5a..378c5ddbd3 100644 --- a/lib/mocha.js +++ b/lib/mocha.js @@ -380,6 +380,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. * @@ -509,6 +523,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 205488426a..961c07e449 100644 --- a/lib/reporters/base.js +++ b/lib/reporters/base.js @@ -200,8 +200,9 @@ exports.list = function (failures) { if (err.uncaught) { msg = 'Uncaught ' + msg; } + // explicitly show diff - if (err.showDiff !== false && sameType(actual, expected) && expected !== undefined) { + if (exports.hideDiff !== true && err.showDiff !== false && sameType(actual, expected) && expected !== undefined) { escape = false; if (!(utils.isString(actual) && utils.isString(expected))) { err.actual = actual = utils.stringify(actual); 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 6282d882ff..9bcbaf561a 100644 --- a/test/reporters/base.spec.js +++ b/test/reporters/base.spec.js @@ -88,6 +88,21 @@ describe('Base reporter', function () { errOut.should.not.match(/\- actual/); errOut.should.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 () {