Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
225 changes: 115 additions & 110 deletions lib/internal/test_runner/reporter/utils.js
Original file line number Diff line number Diff line change
@@ -1,110 +1,115 @@
'use strict';
const {
ArrayPrototypeJoin,
RegExpPrototypeSymbolSplit,
SafeMap,
StringPrototypeRepeat,
hardenRegExp,
} = primordials;
const colors = require('internal/util/colors');
const { inspectWithNoCustomRetry } = require('internal/errors');
const indentMemo = new SafeMap();

const inspectOptions = {
__proto__: null,
colors: colors.shouldColorize(process.stdout),
breakLength: Infinity,
};

const reporterUnicodeSymbolMap = {
'__proto__': null,
'test:fail': '\u2716 ',
'test:pass': '\u2714 ',
'test:diagnostic': '\u2139 ',
'test:log': '\u2139 ',
'test:coverage': '\u2139 ',
'arrow:right': '\u25B6 ',
'hyphen:minus': '\uFE63 ',
'warning:alert': '\u26A0 ',
};

const reporterColorMap = {
'__proto__': null,
get 'test:fail'() {
return colors.red;
},
get 'test:pass'() {
return colors.green;
},
get 'test:diagnostic'() {
return colors.blue;
},
get 'test:log'() {
return colors.blue;
},
get 'info'() {
return colors.blue;
},
get 'warn'() {
return colors.yellow;
},
get 'error'() {
return colors.red;
},
};

function indent(nesting) {
let value = indentMemo.get(nesting);
if (value === undefined) {
value = StringPrototypeRepeat(' ', nesting);
indentMemo.set(nesting, value);
}
return value;
}

function formatError(error, indent) {
const err = error.code === 'ERR_TEST_FAILURE' ? error.cause : error;
const message = ArrayPrototypeJoin(
RegExpPrototypeSymbolSplit(
hardenRegExp(/\r?\n/),
inspectWithNoCustomRetry(err, inspectOptions),
), `\n${indent} `);
return `\n${indent} ${message}\n`;
}

function formatTestReport(type, data, showErrorDetails = true, prefix = '', indent = '') {
let color = reporterColorMap[type] ?? colors.white;
let symbol = reporterUnicodeSymbolMap[type] ?? ' ';
const { skip, todo, expectFailure } = data;
const duration_ms = data.details?.duration_ms ? ` ${colors.gray}(${data.details.duration_ms}ms)${colors.white}` : '';
const replayed = data.details?.passed_on_attempt !== undefined ?
` ${colors.gray}(passed on attempt ${data.details.passed_on_attempt})${colors.white}` :
'';
let title = `${data.name}${duration_ms}${replayed}`;

if (skip !== undefined) {
title += ` # ${typeof skip === 'string' && skip.length ? skip : 'SKIP'}`;
color = colors.gray;
symbol = reporterUnicodeSymbolMap['hyphen:minus'];
} else if (todo !== undefined) {
title += ` # ${typeof todo === 'string' && todo.length ? todo : 'TODO'}`;
if (type === 'test:fail') {
color = colors.yellow;
symbol = reporterUnicodeSymbolMap['warning:alert'];
}
} else if (expectFailure !== undefined) {
title += ` # EXPECTED FAILURE`;
}

const err = showErrorDetails && data.details?.error ? formatError(data.details.error, indent) : '';

return `${prefix}${indent}${color}${symbol}${title}${colors.white}${err}`;
}

module.exports = {
__proto__: null,
reporterUnicodeSymbolMap,
reporterColorMap,
formatTestReport,
indent,
};
'use strict';

Check failure on line 1 in lib/internal/test_runner/reporter/utils.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected linebreaks to be 'LF' but found 'CRLF'
const {

Check failure on line 2 in lib/internal/test_runner/reporter/utils.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected linebreaks to be 'LF' but found 'CRLF'
ArrayPrototypeJoin,

Check failure on line 3 in lib/internal/test_runner/reporter/utils.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected linebreaks to be 'LF' but found 'CRLF'
RegExpPrototypeSymbolSplit,

Check failure on line 4 in lib/internal/test_runner/reporter/utils.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected linebreaks to be 'LF' but found 'CRLF'
SafeMap,

Check failure on line 5 in lib/internal/test_runner/reporter/utils.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected linebreaks to be 'LF' but found 'CRLF'
StringPrototypeRepeat,

Check failure on line 6 in lib/internal/test_runner/reporter/utils.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected linebreaks to be 'LF' but found 'CRLF'
hardenRegExp,

Check failure on line 7 in lib/internal/test_runner/reporter/utils.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected linebreaks to be 'LF' but found 'CRLF'
} = primordials;

Check failure on line 8 in lib/internal/test_runner/reporter/utils.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected linebreaks to be 'LF' but found 'CRLF'
const colors = require('internal/util/colors');

Check failure on line 9 in lib/internal/test_runner/reporter/utils.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected linebreaks to be 'LF' but found 'CRLF'
const { inspectWithNoCustomRetry } = require('internal/errors');

Check failure on line 10 in lib/internal/test_runner/reporter/utils.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected linebreaks to be 'LF' but found 'CRLF'
const { getOptionValue } = require('internal/options');
const indentMemo = new SafeMap();

const testInspectDepth = getOptionValue('--test-inspect-depth');
const inspectOptions = {
__proto__: null,
colors: colors.shouldColorize(process.stdout),
breakLength: Infinity,
};
if (testInspectDepth >= 0) {
inspectOptions.depth = testInspectDepth;
}

const reporterUnicodeSymbolMap = {
'__proto__': null,
'test:fail': '\u2716 ',
'test:pass': '\u2714 ',
'test:diagnostic': '\u2139 ',
'test:log': '\u2139 ',
'test:coverage': '\u2139 ',
'arrow:right': '\u25B6 ',
'hyphen:minus': '\uFE63 ',
'warning:alert': '\u26A0 ',
};

const reporterColorMap = {
'__proto__': null,
get 'test:fail'() {
return colors.red;
},
get 'test:pass'() {
return colors.green;
},
get 'test:diagnostic'() {
return colors.blue;
},
get 'test:log'() {
return colors.blue;
},
get 'info'() {
return colors.blue;
},
get 'warn'() {
return colors.yellow;
},
get 'error'() {
return colors.red;
},
};

function indent(nesting) {
let value = indentMemo.get(nesting);
if (value === undefined) {
value = StringPrototypeRepeat(' ', nesting);
indentMemo.set(nesting, value);
}
return value;
}

function formatError(error, indent) {
const err = error.code === 'ERR_TEST_FAILURE' ? error.cause : error;
const message = ArrayPrototypeJoin(
RegExpPrototypeSymbolSplit(
hardenRegExp(/\r?\n/),
inspectWithNoCustomRetry(err, inspectOptions),
), `\n${indent} `);
return `\n${indent} ${message}\n`;
}

function formatTestReport(type, data, showErrorDetails = true, prefix = '', indent = '') {
let color = reporterColorMap[type] ?? colors.white;
let symbol = reporterUnicodeSymbolMap[type] ?? ' ';
const { skip, todo, expectFailure } = data;
const duration_ms = data.details?.duration_ms ? ` ${colors.gray}(${data.details.duration_ms}ms)${colors.white}` : '';
const replayed = data.details?.passed_on_attempt !== undefined ?
` ${colors.gray}(passed on attempt ${data.details.passed_on_attempt})${colors.white}` :
'';
let title = `${data.name}${duration_ms}${replayed}`;

if (skip !== undefined) {
title += ` # ${typeof skip === 'string' && skip.length ? skip : 'SKIP'}`;
color = colors.gray;
symbol = reporterUnicodeSymbolMap['hyphen:minus'];
} else if (todo !== undefined) {
title += ` # ${typeof todo === 'string' && todo.length ? todo : 'TODO'}`;
if (type === 'test:fail') {
color = colors.yellow;
symbol = reporterUnicodeSymbolMap['warning:alert'];
}
} else if (expectFailure !== undefined) {
title += ` # EXPECTED FAILURE`;
}

const err = showErrorDetails && data.details?.error ? formatError(data.details.error, indent) : '';

return `${prefix}${indent}${color}${symbol}${title}${colors.white}${err}`;
}

module.exports = {
__proto__: null,
reporterUnicodeSymbolMap,
reporterColorMap,
formatTestReport,
indent,
};
Loading
Loading