Skip to content

Commit

Permalink
node 8 fix. For #915
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed May 11, 2019
1 parent 46bd195 commit d39ee55
Showing 1 changed file with 45 additions and 7 deletions.
52 changes: 45 additions & 7 deletions lib/reporters/console.js
Expand Up @@ -10,12 +10,47 @@ const internals = {
};


internals.stringify = (obj, indent) => {
internals.stringify = function (obj) {

return Util.inspect(obj, { compact: !indent, depth: Infinity, maxArrayLength: Infinity, breakLength: 80, sorted: true });
// $lab:coverage:off$
if (process.version[1] !== '1') {
obj = internals.sortObject(obj);
}
// $lab:coverage:on$

return Util.inspect(obj, { compact: false, depth: Infinity, maxArrayLength: Infinity, breakLength: 80, sorted: true });
};


// $lab:coverage:off$
internals.sortObject = (obj, _seen) => {

if (!obj ||
typeof obj !== 'object') {

return obj;
}

const seen = _seen || new Map();

if (seen.has(obj)) {
return seen.get(obj);
}

const output = Array.isArray(obj) ? [] : {};
seen.set(obj, output);

const keys = Object.keys(obj).sort();
for (const key of keys) {
const value = obj[key];
output[key] = internals.sortObject(value, seen);
}

return output;
};
// $lab:coverage:on$


exports = module.exports = internals.Reporter = function (options) {

this.settings = options;
Expand Down Expand Up @@ -58,8 +93,6 @@ internals.Reporter.prototype.test = function (test) {
this.report(test.err ? this.colors.red('x') : (test.skipped || test.todo ? this.colors.magenta('-') : '.'));
}
else {

// Can't be fully covereed but it's ok
// $lab:coverage:off$
const check = process.platform === 'win32' ? '\u221A' : '\u2714';
const asterisk = process.platform === 'win32' ? '\u00D7' : '\u2716';
Expand Down Expand Up @@ -158,8 +191,8 @@ internals.Reporter.prototype.end = function (notebook) {
if (test.err.actual !== undefined &&
test.err.expected !== undefined) {

const actual = internals.stringify(test.err.actual, true);
const expected = internals.stringify(test.err.expected, true);
const actual = internals.stringify(test.err.actual);
const expected = internals.stringify(test.err.expected);

output += ' ' + whiteRedBg('actual') + ' ' + blackGreenBg('expected') + '\n\n ';

Expand Down Expand Up @@ -204,7 +237,12 @@ internals.Reporter.prototype.end = function (notebook) {

if (test.err.data) {
const isObject = typeof test.err.data === 'object' && !Array.isArray(test.err.data);
let errorData = internals.stringify(test.err.data, isObject);
let errorData = internals.stringify(test.err.data);

if (Array.isArray(test.err.data)) {
errorData = errorData.replace(/\n\s*/g, ' ');
}

if (isObject) {
errorData = errorData.split('\n').slice(1, -1).join('\n');
}
Expand Down

0 comments on commit d39ee55

Please sign in to comment.