Skip to content

Commit

Permalink
Fix for #1001
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Dec 31, 2021
1 parent 6dd0d2c commit 4c39dee
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 53 deletions.
5 changes: 2 additions & 3 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,6 @@ internals.options = function () {
'lint-fix': false,
'lint-errors-threshold': 0,
'lint-warnings-threshold': 0,
'max-diff-length': 20 * 1024, // 20K should be more than most people want logged to their console. The diff algorithm becomes very slow for larger strings.
paths: ['test'],
reporter: 'console',
retries: 5,
Expand Down Expand Up @@ -469,8 +468,8 @@ internals.options = function () {
'coverage-path', 'coverage-all', 'coverage-flat', 'coverage-module', 'coverage-pattern',
'default-plan-threshold', 'dry', 'environment', 'flat', 'globals', 'grep',
'lint', 'lint-errors-threshold', 'lint-fix', 'lint-options', 'lint-warnings-threshold',
'linter', 'max-diff-length', 'output', 'pattern', 'reporter', 'retries', 'seed', 'shuffle', 'silence',
'silent-skips', 'sourcemaps', 'threshold', 'timeout', 'transform', 'types', 'types-test', 'verbose'];
'linter', 'output', 'pattern', 'reporter', 'retries', 'seed', 'shuffle', 'silence', 'silent-skips',
'sourcemaps', 'threshold', 'timeout', 'transform', 'types', 'types-test', 'typescript', 'verbose'];

for (let i = 0; i < keys.length; ++i) {
if (argv.hasOwnProperty(keys[i]) && argv[keys[i]] !== undefined && argv[keys[i]] !== null) {
Expand Down
16 changes: 7 additions & 9 deletions lib/reporters/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const SupportsColor = require('supports-color');


const internals = {
width: 50
width: 50,
maxDiffLength: 20 * 1024 // 20Kb
};


Expand Down Expand Up @@ -212,15 +213,12 @@ internals.Reporter.prototype.end = function (notebook) {
let actual = internals.stringify(test.err.actual);
let expected = internals.stringify(test.err.expected);

if (this.settings['max-diff-length'] > 0) {
const maxLen = this.settings['max-diff-length'];
if (actual.length > maxLen) {
actual = actual.substr(0, maxLen) + '[truncated]';
}
if (actual.length > internals.maxDiffLength) {
actual = actual.substr(0, internals.maxDiffLength) + '[truncated]';
}

if (expected.length > maxLen) {
expected = expected.substr(0, maxLen) + '[truncated]';
}
if (expected.length > internals.maxDiffLength) {
expected = expected.substr(0, internals.maxDiffLength) + '[truncated]';
}

output += ' ' + whiteRedBg('actual') + ' ' + blackGreenBg('expected') + '\n\n ';
Expand Down
45 changes: 4 additions & 41 deletions test/reporters.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,51 +362,14 @@ describe('Reporter', () => {

script.test('works', () => {

expect({ a: 1 }).to.equal({ b: 1 });
const long = new Array(21 * 1024).fill('a');
expect({ a: long }).to.equal({ b: long });
});
});

const { code, output } = await Lab.report(script, { reporter: 'console', colors: false, output: false, 'max-diff-length': 8 });
expect(code).to.equal(1);
expect(output).to.contain('ab: 1[truncated]');
expect(output).to.contain('1 of 1 tests failed');
expect(output).to.contain('Test duration:');
expect(output).to.contain('Leaks: No issues');
});

it('does not truncate if max diff length is set to 0', async () => {

const script = Lab.script();
script.experiment('test', () => {

script.test('works', () => {

expect({ a: 1 }).to.equal({ b: 1 });
});
});

const { code, output } = await Lab.report(script, { reporter: 'console', colors: false, output: false, 'max-diff-length': 0 });
expect(code).to.equal(1);
expect(output).to.contain('ab: 1\n');
expect(output).to.contain('1 of 1 tests failed');
expect(output).to.contain('Test duration:');
expect(output).to.contain('Leaks: No issues');
});

it('does not truncate if diff length less than the maximum', async () => {

const script = Lab.script();
script.experiment('test', () => {

script.test('works', () => {

expect({ a: 1 }).to.equal({ b: 1 });
});
});

const { code, output } = await Lab.report(script, { reporter: 'console', colors: false, output: false, 'max-diff-length': 10 * 1024 });
const { code, output } = await Lab.report(script, { reporter: 'console', colors: false, output: false });
expect(code).to.equal(1);
expect(output).to.not.contain('[truncated]');
expect(output).to.contain('[truncated]');
expect(output).to.contain('1 of 1 tests failed');
expect(output).to.contain('Test duration:');
expect(output).to.contain('Leaks: No issues');
Expand Down

0 comments on commit 4c39dee

Please sign in to comment.