Skip to content

Commit

Permalink
feat(testrunner): pretty error messages (#3469)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelEinbinder authored Aug 14, 2020
1 parent 2f5a0a6 commit c1de95f
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 7 deletions.
37 changes: 37 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"ws": "^6.1.0"
},
"devDependencies": {
"@babel/code-frame": "^7.10.4",
"@babel/core": "^7.10.3",
"@babel/preset-env": "^7.10.3",
"@babel/preset-typescript": "^7.10.1",
Expand All @@ -59,6 +60,7 @@
"@types/progress": "^2.0.3",
"@types/proxy-from-env": "^1.0.0",
"@types/rimraf": "^2.0.2",
"@types/stack-utils": "^1.0.1",
"@types/ws": "^6.0.1",
"@typescript-eslint/eslint-plugin": "^2.6.1",
"@typescript-eslint/parser": "^2.6.1",
Expand All @@ -79,6 +81,8 @@
"pixelmatch": "^4.0.2",
"socksv5": "0.0.6",
"source-map-support": "^0.5.19",
"stack-utils": "^2.0.2",
"terminal-link": "^2.1.1",
"text-diff": "^1.0.1",
"ts-loader": "^6.1.2",
"typescript": "^3.8.3",
Expand Down
77 changes: 75 additions & 2 deletions test/runner/dotReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@
const Base = require('mocha/lib/reporters/base');
const constants = require('mocha/lib/runner').constants;
const colors = require('colors/safe');

const milliseconds = require('ms');
const { codeFrameColumns } = require('@babel/code-frame');
const path = require('path');
const fs = require('fs');
const os = require('os');
const terminalLink = require('terminal-link');
const StackUtils = require('stack-utils');
const stackUtils = new StackUtils();
class DotReporter extends Base {
constructor(runner, options) {
super(runner, options);

process.on('SIGINT', async () => {
Base.list(this.failures);
this.epilogue();
process.exit(130);
});

Expand All @@ -47,6 +54,72 @@ class DotReporter extends Base {
this.epilogue();
});
}

epilogue() {
console.log('');

console.log(colors.green(` ${this.stats.passes || 0} passing`) + colors.dim(` (${milliseconds(this.stats.duration)})`));

if (this.stats.pending)
console.log(colors.yellow(` ${this.stats.pending} skipped`));

if (this.stats.failures) {
console.log(colors.red(` ${this.stats.failures} failing`));
console.log('');
this.failures.forEach((failure, index) => {
const relativePath = path.relative(process.cwd(), failure.file);
const header = ` ${index +1}. ${terminalLink(relativePath, `file://${os.hostname()}${failure.file}`)}${failure.title}`;
console.log(colors.bold.red(header));
const stack = failure.err.stack;
if (stack) {
console.log('');
const messageLocation = failure.err.stack.indexOf(failure.err.message);
const preamble = failure.err.stack.substring(0, messageLocation + failure.err.message.length);
console.log(indent(preamble, ' '));
const position = positionInFile(stack, failure.file);
if (position) {
const source = fs.readFileSync(failure.file, 'utf8');
console.log('');
console.log(indent(codeFrameColumns(source, {
start: position,
},
{ highlightCode: true}
), ' '));
}
console.log('');
console.log(indent(colors.dim(stack.substring(preamble.length + 1)), ' '));
} else {
console.log('');
console.log(indent(String(failure.err), ' '));
}
console.log('');
});
}
}
}

/**
* @param {string} lines
* @param {string} tab
*/
function indent(lines, tab) {
return lines.replace(/^/gm, tab);
}

/**
* @param {string} stack
* @param {string} file
* @return {{column: number, line: number}}
*/
function positionInFile(stack, file) {
for (const line of stack.split('\n')) {
const parsed = stackUtils.parseLine(line);
if (!parsed)
continue;
if (path.resolve(process.cwd(), parsed.file) === file)
return {column: parsed.column, line: parsed.line};
}
return null;
}

module.exports = DotReporter;
15 changes: 10 additions & 5 deletions test/runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,14 @@ class Runner extends EventEmitter {
this.stats.passes += params.stats.passes;
this.stats.pending += params.stats.pending;
this.stats.tests += params.stats.tests;
if (params.error)
this._restartWorker(worker);
else
this._workerAvailable(worker);
if (this._runCompleteCallback && !this._pendingJobs)
this._runCompleteCallback();
else {
if (params.error)
this._restartWorker(worker);
else
this._workerAvailable(worker);
}
});
}

Expand Down Expand Up @@ -180,7 +182,10 @@ class Worker extends EventEmitter {

this.process = child_process.fork(path.join(__dirname, 'worker.js'), {
detached: false,
env: process.env,
env: {
FORCE_COLOR: process.stdout.isTTY ? 1 : 0,
...process.env
},
stdio: ['ignore', 'pipe', 'pipe', 'ipc']
});
this.process.on('exit', () => this.emit('exit'));
Expand Down

0 comments on commit c1de95f

Please sign in to comment.