chore: nicer stack highlight (#3259)

Highlight part of the stack that points to where the actual
test failure happened.
This commit is contained in:
Andrey Lushnikov 2018-09-17 23:22:53 +01:00 committed by GitHub
parent a4abb4a628
commit a1a211d9e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 15 deletions

View File

@ -189,5 +189,5 @@ if (process.env.CI && testRunner.hasFocusedTestsOrSuites()) {
process.exit(1);
}
new Reporter(testRunner);
new Reporter(testRunner, utils.projectRoot());
testRunner.run();

View File

@ -20,8 +20,9 @@ const YELLOW_COLOR = '\x1b[33m';
const RESET_COLOR = '\x1b[0m';
class Reporter {
constructor(runner) {
constructor(runner, testFolder = null) {
this._runner = runner;
this._testFolder = testFolder;
runner.on('started', this._onStarted.bind(this));
runner.on('terminated', this._onTerminated.bind(this));
runner.on('finished', this._onFinished.bind(this));
@ -90,19 +91,8 @@ class Reporter {
console.log(' Message:');
console.log(` ${RED_COLOR}${test.error.message || test.error}${RESET_COLOR}`);
console.log(' Stack:');
if (test.error.stack) {
const stack = test.error.stack.split('\n').map(line => ' ' + line);
let i = 0;
while (i < stack.length && !stack[i].includes(__dirname))
++i;
while (i < stack.length && stack[i].includes(__dirname))
++i;
if (i < stack.length) {
const indent = stack[i].match(/^\s*/)[0];
stack[i] = stack[i].substring(0, indent.length - 3) + YELLOW_COLOR + '⇨ ' + RESET_COLOR + stack[i].substring(indent.length - 1);
}
console.log(stack.join('\n'));
}
if (test.error.stack)
console.log(this._beautifyStack(test.error.stack));
}
if (test.output) {
console.log(' Output:');
@ -130,6 +120,25 @@ class Reporter {
console.log(`Finished in ${YELLOW_COLOR}${seconds}${RESET_COLOR} seconds`);
}
_beautifyStack(stack) {
if (!this._testFolder)
return stack;
const lines = stack.split('\n').map(line => ' ' + line);
// Find last stack line that include testrunner code.
let index = 0;
while (index < lines.length && !lines[index].includes(__dirname))
++index;
while (index < lines.length && lines[index].includes(__dirname))
++index;
if (index >= lines.length)
return stack;
const line = lines[index];
const fromIndex = line.lastIndexOf(this._testFolder) + this._testFolder.length;
const toIndex = line.lastIndexOf(')');
lines[index] = line.substring(0, fromIndex) + YELLOW_COLOR + line.substring(fromIndex, toIndex) + RESET_COLOR + line.substring(toIndex);
return lines.join('\n');
}
_onTestStarted(test, workerId) {
this._workersState.set(workerId, {test, isRunning: true});
}