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-28 09:32:00 +02:00
|
|
|
|
asExpected: Test[] = [];
|
|
|
|
|
|
unexpected = new Set<Test>();
|
2020-08-27 01:32:47 +02:00
|
|
|
|
flaky: 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) {
|
2020-08-28 09:32:00 +02:00
|
|
|
|
if (result.status === 'skipped') {
|
|
|
|
|
|
this.skipped.push(test);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (result.status === result.expectedStatus) {
|
|
|
|
|
|
if (test.results.length === 1) {
|
|
|
|
|
|
// as expected from the first attempt
|
|
|
|
|
|
this.asExpected.push(test);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// as expected after unexpected -> flaky.
|
|
|
|
|
|
this.flaky.push(test);
|
2020-08-27 01:32:47 +02:00
|
|
|
|
}
|
2020-08-28 09:32:00 +02:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (result.status === 'passed' || result.status === 'timedOut' || test.results.length === this.config.retries + 1) {
|
|
|
|
|
|
// We made as many retries as we could, still failing.
|
|
|
|
|
|
this.unexpected.add(test);
|
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-28 09:32:00 +02:00
|
|
|
|
console.log(colors.green(` ${this.asExpected.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-28 09:32:00 +02:00
|
|
|
|
const filteredUnexpected = [...this.unexpected].filter(t => !t._hasResultWithStatus('timedOut'));
|
|
|
|
|
|
if (filteredUnexpected.length) {
|
|
|
|
|
|
console.log(colors.red(` ${filteredUnexpected.length} failed`));
|
2020-08-24 05:23:05 +02:00
|
|
|
|
console.log('');
|
2020-08-28 09:32:00 +02:00
|
|
|
|
this._printFailures(filteredUnexpected);
|
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-28 09:32:00 +02:00
|
|
|
|
const timedOut = [...this.unexpected].filter(t => t._hasResultWithStatus('timedOut'));
|
|
|
|
|
|
if (timedOut.length) {
|
|
|
|
|
|
console.log(colors.red(` ${timedOut.length} timed out`));
|
2020-08-24 19:24:40 +02:00
|
|
|
|
console.log('');
|
2020-08-28 09:32:00 +02:00
|
|
|
|
this._printFailures(timedOut);
|
2020-08-24 19:24:40 +02:00
|
|
|
|
}
|
2020-08-28 09:32:00 +02:00
|
|
|
|
console.log('');
|
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);
|
2020-08-28 09:32:00 +02:00
|
|
|
|
const passedUnexpectedlySuffix = test.results[0].status === 'passed' ? ' -- passed unexpectedly' : '';
|
|
|
|
|
|
const header = ` ${index ? index + ')' : ''} ${terminalLink(relativePath, `file://${os.hostname()}${test.file}`)} › ${test.title}${passedUnexpectedlySuffix}`;
|
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-29 00:45:09 +02:00
|
|
|
|
tokens.push(indent(colors.red(`Timeout of ${test._timeout}ms exceeded.`), ' '));
|
2020-08-27 01:32:47 +02:00
|
|
|
|
} 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;
|
|
|
|
|
|
}
|