Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit 2f2b30b

Browse files
CapacitorSetboneskull
authored andcommitted
Add --no-diff option (fixes mochajs#2514)
1 parent e706222 commit 2f2b30b

File tree

6 files changed

+62
-0
lines changed

6 files changed

+62
-0
lines changed

bin/_mocha

+7
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ program
9090
.option('--preserve-symlinks', 'Instructs the module loader to preserve symbolic links when resolving and caching modules')
9191
.option('--icu-data-dir', 'include ICU data')
9292
.option('--inline-diffs', 'display actual/expected differences inline within each string')
93+
.option('--no-diff', 'do not show a diff on failure')
9394
.option('--inspect', 'activate devtools in chrome')
9495
.option('--inspect-brk', 'activate devtools in chrome and break on the first line')
9596
.option('--interfaces', 'display available interfaces')
@@ -250,6 +251,12 @@ if (program.inlineDiffs) {
250251
mocha.useInlineDiffs(true);
251252
}
252253

254+
// --no-diff
255+
256+
if (process.argv.indexOf('--no-diff') !== -1) {
257+
mocha.hideDiff(true);
258+
}
259+
253260
// --slow <ms>
254261

255262
if (program.slow) {

lib/mocha.js

+15
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,20 @@ Mocha.prototype.useInlineDiffs = function (inlineDiffs) {
389389
return this;
390390
};
391391

392+
/**
393+
* Do not show diffs at all.
394+
*
395+
* @param {Boolean} hideDiff
396+
* @return {Mocha}
397+
* @api public
398+
* @param {boolean} hideDiff
399+
* @return {Mocha}
400+
*/
401+
Mocha.prototype.hideDiff = function (hideDiff) {
402+
this.options.hideDiff = hideDiff !== undefined && hideDiff;
403+
return this;
404+
};
405+
392406
/**
393407
* Set the timeout in milliseconds.
394408
*
@@ -545,6 +559,7 @@ Mocha.prototype.run = function (fn) {
545559
exports.reporters.Base.useColors = options.useColors;
546560
}
547561
exports.reporters.Base.inlineDiffs = options.useInlineDiffs;
562+
exports.reporters.Base.hideDiff = options.hideDiff;
548563

549564
function done (failures) {
550565
if (reporter.done) {

lib/reporters/base.js

+3
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,10 @@ exports.list = function (failures) {
208208
if (err.uncaught) {
209209
msg = 'Uncaught ' + msg;
210210
}
211+
211212
// explicitly show diff
213+
if (exports.hideDiff !== true && err.showDiff !== false && sameType(actual, expected) && expected !== undefined) {
214+
escape = false;
212215
if (showDiff(err)) {
213216
stringifyDiffObjs(err);
214217
fmt = color('error title', ' %s) %s:\n%s') + color('error stack', '\n%s\n');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
var assert = require('assert');
3+
4+
describe('Example test', function () {
5+
it('should fail', function () {
6+
assert.deepEqual([1, 2, 3], ['foo', 'bar', 'baz']);
7+
});
8+
});

test/integration/no-diff.spec.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
var helpers = require('./helpers');
4+
var run = helpers.runMocha;
5+
6+
describe('no-diff', function () {
7+
it('should be honoured', function (done) {
8+
run('no-diff.fixture.js', ['--no-diff'], function (err, res) {
9+
res.output.should.not.match(/\+ expected/);
10+
res.output.should.not.match(/- actual/);
11+
done(err);
12+
});
13+
});
14+
});

test/reporters/base.spec.js

+15
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,21 @@ describe('Base reporter', function () {
9696
expect(errOut).to.not.match(/- actual/);
9797
expect(errOut).to.not.match(/\+ expected/);
9898
});
99+
100+
it('should not show diffs when hideDiff is set', function () {
101+
var err = new Assert({ actual: 'foo', expected: 'bar' });
102+
var errOut;
103+
104+
var test = makeTest(err);
105+
106+
Base.hideDiff = true;
107+
Base.list([test]);
108+
Base.hideDiff = false; // Revert to original value
109+
110+
errOut = stdout.join('\n');
111+
errOut.should.not.match(/\- actual/);
112+
errOut.should.not.match(/\+ expected/);
113+
});
99114
});
100115

101116
describe('Getting two strings', function () {

0 commit comments

Comments
 (0)