2020-08-24 05:23:05 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
|
*
|
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
*
|
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
import { codeFrameColumns } from '@babel/code-frame';
|
|
|
|
|
|
import colors from 'colors/safe';
|
|
|
|
|
|
import fs from 'fs';
|
|
|
|
|
|
import milliseconds from 'ms';
|
|
|
|
|
|
import os from 'os';
|
|
|
|
|
|
import path from 'path';
|
|
|
|
|
|
import StackUtils from 'stack-utils';
|
|
|
|
|
|
import terminalLink from 'terminal-link';
|
|
|
|
|
|
import { Reporter } from '../reporter';
|
|
|
|
|
|
import { RunnerConfig } from '../runnerConfig';
|
2020-08-26 23:14:23 +02:00
|
|
|
|
import { Suite, Test, TestResult } from '../test';
|
2020-08-24 05:23:05 +02:00
|
|
|
|
|
2020-08-26 23:14:23 +02:00
|
|
|
|
const stackUtils = new StackUtils();
|
2020-08-24 05:23:05 +02:00
|
|
|
|
|
|
|
|
|
|
export class BaseReporter implements Reporter {
|
2020-08-26 18:38:19 +02:00
|
|
|
|
skipped: Test[] = [];
|
2020-08-27 01:32:47 +02:00
|
|
|
|
passed: Test[] = [];
|
|
|
|
|
|
flaky: Test[] = [];
|
|
|
|
|
|
failed: Test[] = [];
|
|
|
|
|
|
timedOut: Test[] = [];
|
2020-08-24 05:23:05 +02:00
|
|
|
|
duration = 0;
|
|
|
|
|
|
startTime: number;
|
|
|
|
|
|
config: RunnerConfig;
|
|
|
|
|
|
suite: Suite;
|
|
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
|
process.on('SIGINT', async () => {
|
|
|
|
|
|
this.epilogue();
|
|
|
|
|
|
process.exit(130);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
onBegin(config: RunnerConfig, suite: Suite) {
|
|
|
|
|
|
this.startTime = Date.now();
|
|
|
|
|
|
this.config = config;
|
|
|
|
|
|
this.suite = suite;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-08-26 23:14:23 +02:00
|
|
|
|
onTestBegin(test: Test) {
|
2020-08-24 05:23:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-08-26 18:38:19 +02:00
|
|
|
|
onTestStdOut(test: Test, chunk: string | Buffer) {
|
2020-08-25 20:00:05 +02:00
|
|
|
|
if (!this.config.quiet)
|
|
|
|
|
|
process.stdout.write(chunk);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-08-26 18:38:19 +02:00
|
|
|
|
onTestStdErr(test: Test, chunk: string | Buffer) {
|
2020-08-25 20:00:05 +02:00
|
|
|
|
if (!this.config.quiet)
|
|
|
|
|
|
process.stderr.write(chunk);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-08-26 23:14:23 +02:00
|
|
|
|
onTestEnd(test: Test, result: TestResult) {
|
|
|
|
|
|
switch (result.status) {
|
2020-08-27 01:32:47 +02:00
|
|
|
|
case 'skipped': {
|
|
|
|
|
|
this.skipped.push(test);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
case 'passed':
|
|
|
|
|
|
if (test.results.length === 1)
|
|
|
|
|
|
this.passed.push(test);
|
|
|
|
|
|
else
|
|
|
|
|
|
this.flaky.push(test);
|
|
|
|
|
|
return;
|
|
|
|
|
|
case 'failed':
|
|
|
|
|
|
// Fall through.
|
|
|
|
|
|
case 'timedOut': {
|
|
|
|
|
|
if (test.results.length === this.config.retries + 1) {
|
|
|
|
|
|
if (result.status === 'timedOut')
|
|
|
|
|
|
this.timedOut.push(test);
|
|
|
|
|
|
else
|
|
|
|
|
|
this.failed.push(test);
|
|
|
|
|
|
}
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2020-08-26 23:14:23 +02:00
|
|
|
|
}
|
2020-08-24 05:23:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
onEnd() {
|
|
|
|
|
|
this.duration = Date.now() - this.startTime;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
epilogue() {
|
|
|
|
|
|
console.log('');
|
|
|
|
|
|
|
2020-08-26 23:14:23 +02:00
|
|
|
|
console.log(colors.green(` ${this.passed.length} passed`) + colors.dim(` (${milliseconds(this.duration)})`));
|
2020-08-24 05:23:05 +02:00
|
|
|
|
|
2020-08-26 18:38:19 +02:00
|
|
|
|
if (this.skipped.length)
|
|
|
|
|
|
console.log(colors.yellow(` ${this.skipped.length} skipped`));
|
2020-08-24 05:23:05 +02:00
|
|
|
|
|
2020-08-26 23:14:23 +02:00
|
|
|
|
if (this.failed.length) {
|
|
|
|
|
|
console.log(colors.red(` ${this.failed.length} failed`));
|
2020-08-24 05:23:05 +02:00
|
|
|
|
console.log('');
|
2020-08-26 23:14:23 +02:00
|
|
|
|
this._printFailures(this.failed);
|
2020-08-24 19:24:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-08-27 01:32:47 +02:00
|
|
|
|
if (this.flaky.length) {
|
|
|
|
|
|
console.log(colors.red(` ${this.flaky.length} flaky`));
|
|
|
|
|
|
console.log('');
|
|
|
|
|
|
this._printFailures(this.flaky);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-08-26 23:14:23 +02:00
|
|
|
|
if (this.timedOut.length) {
|
|
|
|
|
|
console.log(colors.red(` ${this.timedOut.length} timed out`));
|
2020-08-24 19:24:40 +02:00
|
|
|
|
console.log('');
|
2020-08-26 23:14:23 +02:00
|
|
|
|
this._printFailures(this.timedOut);
|
2020-08-24 19:24:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-08-27 01:32:47 +02:00
|
|
|
|
private _printFailures(failures: Test[]) {
|
|
|
|
|
|
failures.forEach((test, index) => {
|
|
|
|
|
|
console.log(this.formatFailure(test, index + 1));
|
2020-08-24 19:24:40 +02:00
|
|
|
|
});
|
2020-08-24 05:23:05 +02:00
|
|
|
|
}
|
2020-08-25 20:00:05 +02:00
|
|
|
|
|
2020-08-27 01:32:47 +02:00
|
|
|
|
formatFailure(test: Test, index?: number): string {
|
2020-08-25 20:00:05 +02:00
|
|
|
|
const tokens: string[] = [];
|
2020-08-26 23:14:23 +02:00
|
|
|
|
const relativePath = path.relative(process.cwd(), test.file);
|
|
|
|
|
|
const header = ` ${index ? index + ')' : ''} ${terminalLink(relativePath, `file://${os.hostname()}${test.file}`)} › ${test.title}`;
|
2020-08-25 20:00:05 +02:00
|
|
|
|
tokens.push(colors.bold(colors.red(header)));
|
2020-08-27 01:32:47 +02:00
|
|
|
|
for (const result of test.results) {
|
|
|
|
|
|
if (result.status === 'passed')
|
|
|
|
|
|
continue;
|
|
|
|
|
|
if (result.status === 'timedOut') {
|
2020-08-25 20:00:05 +02:00
|
|
|
|
tokens.push('');
|
2020-08-27 01:32:47 +02:00
|
|
|
|
tokens.push(indent(colors.red(`Timeout of ${test.timeout}ms exceeded.`), ' '));
|
|
|
|
|
|
} else {
|
|
|
|
|
|
const stack = result.error.stack;
|
|
|
|
|
|
if (stack) {
|
|
|
|
|
|
tokens.push('');
|
|
|
|
|
|
const messageLocation = result.error.stack.indexOf(result.error.message);
|
|
|
|
|
|
const preamble = result.error.stack.substring(0, messageLocation + result.error.message.length);
|
|
|
|
|
|
tokens.push(indent(preamble, ' '));
|
|
|
|
|
|
const position = positionInFile(stack, test.file);
|
|
|
|
|
|
if (position) {
|
|
|
|
|
|
const source = fs.readFileSync(test.file, 'utf8');
|
|
|
|
|
|
tokens.push('');
|
|
|
|
|
|
tokens.push(indent(codeFrameColumns(source, {
|
|
|
|
|
|
start: position,
|
|
|
|
|
|
},
|
|
|
|
|
|
{ highlightCode: true}
|
|
|
|
|
|
), ' '));
|
|
|
|
|
|
}
|
|
|
|
|
|
tokens.push('');
|
|
|
|
|
|
tokens.push(indent(colors.dim(stack.substring(preamble.length + 1)), ' '));
|
|
|
|
|
|
} else {
|
2020-08-26 23:14:23 +02:00
|
|
|
|
tokens.push('');
|
2020-08-27 01:32:47 +02:00
|
|
|
|
tokens.push(indent(String(result.error), ' '));
|
2020-08-26 23:14:23 +02:00
|
|
|
|
}
|
2020-08-25 20:00:05 +02:00
|
|
|
|
}
|
2020-08-27 01:32:47 +02:00
|
|
|
|
break;
|
2020-08-25 20:00:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
tokens.push('');
|
|
|
|
|
|
return tokens.join('\n');
|
|
|
|
|
|
}
|
2020-08-24 05:23:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function indent(lines: string, tab: string) {
|
|
|
|
|
|
return lines.replace(/^/gm, tab);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function positionInFile(stack: string, file: string): { column: number; line: number; } {
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|