Skip to content

Commit

Permalink
Browser compability fixes
Browse files Browse the repository at this point in the history
Plus remove console.error statment
Plus add tests for option handling
  • Loading branch information
norla committed Feb 13, 2022
1 parent cc5653b commit 8299390
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
15 changes: 8 additions & 7 deletions lib/reporters/base.js
Expand Up @@ -196,11 +196,13 @@ function stringifyDiffObjs(err) {

var generateDiff = (exports.generateDiff = function (actual, expected) {
try {
const maxLen = exports.maxDiffSize || Number.MAX_SAFE_INTEGER;
const skipped = Math.max(actual.length - maxLen, expected.length - maxLen);

actual = actual.slice(0, maxLen);
expected = expected.slice(0, maxLen);
var maxLen = exports.maxDiffSize;
var skipped = 0;
if (maxLen > 0) {
skipped = Math.max(actual.length - maxLen, expected.length - maxLen);
actual = actual.slice(0, maxLen);
expected = expected.slice(0, maxLen);
}
let result = exports.inlineDiffs
? inlineDiff(actual, expected)
: unifiedDiff(actual, expected);
Expand Down Expand Up @@ -328,8 +330,7 @@ function Base(runner, options) {

var maxDiffSizeOpt =
this.options.reporterOption && this.options.reporterOption.maxDiffSize;
console.error();
if (maxDiffSizeOpt !== undefined && !Number.isNaN(Number(maxDiffSizeOpt))) {
if (maxDiffSizeOpt !== undefined && !isNaN(Number(maxDiffSizeOpt))) {
exports.maxDiffSize = Number(maxDiffSizeOpt);
}

Expand Down
18 changes: 18 additions & 0 deletions test/reporters/base.spec.js
Expand Up @@ -10,6 +10,9 @@ var Base = reporters.Base;
var chaiExpect = chai.expect;
var createElements = helpers.createElements;
var makeTest = helpers.makeTest;
var Mocha = require('../../');
var Suite = Mocha.Suite;
var Runner = Mocha.Runner;

describe('Base reporter', function () {
var stdout;
Expand Down Expand Up @@ -505,3 +508,18 @@ describe('Base reporter', function () {
});
});
});
describe('when "reporterOption.maxDiffSize" is provided', function () {
it('should be the effective value used for tuncating diffs', function () {
var options = {
reporterOption: {
maxDiffSize: 4
}
};
var mocha = new Mocha();
var suite = new Suite('Dummy suite', 'root');
var runner = new Runner(suite);
// eslint-disable-next-line no-unused-vars
var mochaReporter = new mocha._reporter(runner, options);
expect(Base.maxDiffSize, 'to be', 4);
});
});

0 comments on commit 8299390

Please sign in to comment.