2021-06-07 02:09:53 +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 fs from 'fs';
|
|
|
|
|
import path from 'path';
|
2021-10-14 11:55:08 +02:00
|
|
|
import { FullConfig, TestCase, Suite, TestResult, TestError, TestStep, FullResult, TestStatus, Location, Reporter } from '../../types/testReporter';
|
2022-01-05 01:00:55 +01:00
|
|
|
import { prepareErrorStack } from './base';
|
2021-07-09 02:16:36 +02:00
|
|
|
|
|
|
|
|
export interface JSONReport {
|
2022-04-01 21:36:05 +02:00
|
|
|
config: Omit<FullConfig, 'projects'> & {
|
2021-07-09 02:16:36 +02:00
|
|
|
projects: {
|
|
|
|
|
outputDir: string,
|
|
|
|
|
repeatEach: number,
|
|
|
|
|
retries: number,
|
|
|
|
|
metadata: any,
|
|
|
|
|
name: string,
|
|
|
|
|
testDir: string,
|
|
|
|
|
testIgnore: string[],
|
|
|
|
|
testMatch: string[],
|
|
|
|
|
timeout: number,
|
|
|
|
|
}[],
|
|
|
|
|
};
|
|
|
|
|
suites: JSONReportSuite[];
|
|
|
|
|
errors: TestError[];
|
|
|
|
|
}
|
|
|
|
|
export interface JSONReportSuite {
|
2021-06-07 02:09:53 +02:00
|
|
|
title: string;
|
|
|
|
|
file: string;
|
|
|
|
|
column: number;
|
|
|
|
|
line: number;
|
2021-07-09 02:16:36 +02:00
|
|
|
specs: JSONReportSpec[];
|
|
|
|
|
suites?: JSONReportSuite[];
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
2021-07-09 02:16:36 +02:00
|
|
|
export interface JSONReportSpec {
|
2021-09-15 21:30:22 +02:00
|
|
|
tags: string[],
|
2021-07-09 02:16:36 +02:00
|
|
|
title: string;
|
|
|
|
|
ok: boolean;
|
|
|
|
|
tests: JSONReportTest[];
|
|
|
|
|
file: string;
|
|
|
|
|
line: number;
|
|
|
|
|
column: number;
|
|
|
|
|
}
|
|
|
|
|
export interface JSONReportTest {
|
|
|
|
|
timeout: number;
|
|
|
|
|
annotations: { type: string, description?: string }[],
|
|
|
|
|
expectedStatus: TestStatus;
|
|
|
|
|
projectName: string;
|
|
|
|
|
results: JSONReportTestResult[];
|
|
|
|
|
status: 'skipped' | 'expected' | 'unexpected' | 'flaky';
|
|
|
|
|
}
|
|
|
|
|
export interface JSONReportTestResult {
|
|
|
|
|
workerIndex: number;
|
|
|
|
|
status: TestStatus | undefined;
|
|
|
|
|
duration: number;
|
|
|
|
|
error: TestError | undefined;
|
2021-09-30 23:18:36 +02:00
|
|
|
stdout: JSONReportSTDIOEntry[];
|
|
|
|
|
stderr: JSONReportSTDIOEntry[];
|
2021-07-09 02:16:36 +02:00
|
|
|
retry: number;
|
2021-09-07 22:35:30 +02:00
|
|
|
steps?: JSONReportTestStep[];
|
2021-09-30 23:18:36 +02:00
|
|
|
attachments: {
|
|
|
|
|
name: string;
|
|
|
|
|
path?: string;
|
|
|
|
|
body?: string;
|
|
|
|
|
contentType: string;
|
|
|
|
|
}[];
|
2022-01-05 01:00:55 +01:00
|
|
|
errorLocation?: Location;
|
2021-07-09 02:16:36 +02:00
|
|
|
}
|
2021-09-07 22:35:30 +02:00
|
|
|
export interface JSONReportTestStep {
|
|
|
|
|
title: string;
|
|
|
|
|
duration: number;
|
|
|
|
|
error: TestError | undefined;
|
|
|
|
|
steps?: JSONReportTestStep[];
|
|
|
|
|
}
|
2021-07-09 02:16:36 +02:00
|
|
|
export type JSONReportSTDIOEntry = { text: string } | { buffer: string };
|
2021-06-07 02:09:53 +02:00
|
|
|
|
2021-08-05 22:36:47 +02:00
|
|
|
export function toPosixPath(aPath: string): string {
|
2021-06-07 02:09:53 +02:00
|
|
|
return aPath.split(path.sep).join(path.posix.sep);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-16 21:40:33 +02:00
|
|
|
class JSONReporter implements Reporter {
|
2021-06-07 02:09:53 +02:00
|
|
|
config!: FullConfig;
|
|
|
|
|
suite!: Suite;
|
|
|
|
|
private _errors: TestError[] = [];
|
|
|
|
|
private _outputFile: string | undefined;
|
|
|
|
|
|
|
|
|
|
constructor(options: { outputFile?: string } = {}) {
|
2021-11-03 16:25:16 +01:00
|
|
|
this._outputFile = options.outputFile || process.env[`PLAYWRIGHT_JSON_OUTPUT_NAME`];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printsToStdio() {
|
|
|
|
|
return !this._outputFile;
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onBegin(config: FullConfig, suite: Suite) {
|
|
|
|
|
this.config = config;
|
|
|
|
|
this.suite = suite;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onError(error: TestError): void {
|
|
|
|
|
this._errors.push(error);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-29 19:55:46 +02:00
|
|
|
async onEnd(result: FullResult) {
|
2021-06-07 02:09:53 +02:00
|
|
|
outputReport(this._serializeReport(), this._outputFile);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-09 02:16:36 +02:00
|
|
|
private _serializeReport(): JSONReport {
|
2021-06-07 02:09:53 +02:00
|
|
|
return {
|
|
|
|
|
config: {
|
|
|
|
|
...this.config,
|
|
|
|
|
rootDir: toPosixPath(this.config.rootDir),
|
|
|
|
|
projects: this.config.projects.map(project => {
|
|
|
|
|
return {
|
|
|
|
|
outputDir: toPosixPath(project.outputDir),
|
|
|
|
|
repeatEach: project.repeatEach,
|
|
|
|
|
retries: project.retries,
|
|
|
|
|
metadata: project.metadata,
|
|
|
|
|
name: project.name,
|
|
|
|
|
testDir: toPosixPath(project.testDir),
|
|
|
|
|
testIgnore: serializePatterns(project.testIgnore),
|
|
|
|
|
testMatch: serializePatterns(project.testMatch),
|
|
|
|
|
timeout: project.timeout,
|
|
|
|
|
};
|
|
|
|
|
})
|
|
|
|
|
},
|
2021-07-16 07:02:10 +02:00
|
|
|
suites: this._mergeSuites(this.suite.suites),
|
2021-06-07 02:09:53 +02:00
|
|
|
errors: this._errors
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-16 07:02:10 +02:00
|
|
|
private _mergeSuites(suites: Suite[]): JSONReportSuite[] {
|
|
|
|
|
const fileSuites = new Map<string, JSONReportSuite>();
|
|
|
|
|
const result: JSONReportSuite[] = [];
|
2021-07-17 00:23:50 +02:00
|
|
|
for (const projectSuite of suites) {
|
|
|
|
|
for (const fileSuite of projectSuite.suites) {
|
2021-07-19 02:40:59 +02:00
|
|
|
const file = fileSuite.location!.file;
|
|
|
|
|
if (!fileSuites.has(file)) {
|
2021-07-17 00:23:50 +02:00
|
|
|
const serialized = this._serializeSuite(fileSuite);
|
|
|
|
|
if (serialized) {
|
2021-07-19 02:40:59 +02:00
|
|
|
fileSuites.set(file, serialized);
|
2021-07-17 00:23:50 +02:00
|
|
|
result.push(serialized);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2021-07-19 02:40:59 +02:00
|
|
|
this._mergeTestsFromSuite(fileSuites.get(file)!, fileSuite);
|
2021-07-16 07:02:10 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-19 02:40:59 +02:00
|
|
|
private _relativeLocation(location: Location | undefined): Location {
|
|
|
|
|
if (!location)
|
|
|
|
|
return { file: '', line: 0, column: 0 };
|
2021-07-16 21:40:33 +02:00
|
|
|
return {
|
|
|
|
|
file: toPosixPath(path.relative(this.config.rootDir, location.file)),
|
|
|
|
|
line: location.line,
|
|
|
|
|
column: location.column,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-19 02:40:59 +02:00
|
|
|
private _locationMatches(s: JSONReportSuite | JSONReportSpec, location: Location | undefined) {
|
2021-07-16 21:40:33 +02:00
|
|
|
const relative = this._relativeLocation(location);
|
|
|
|
|
return s.file === relative.file && s.line === relative.line && s.column === relative.column;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-16 07:02:10 +02:00
|
|
|
private _mergeTestsFromSuite(to: JSONReportSuite, from: Suite) {
|
|
|
|
|
for (const fromSuite of from.suites) {
|
2021-07-16 21:40:33 +02:00
|
|
|
const toSuite = (to.suites || []).find(s => s.title === fromSuite.title && this._locationMatches(s, from.location));
|
2021-07-16 07:02:10 +02:00
|
|
|
if (toSuite) {
|
|
|
|
|
this._mergeTestsFromSuite(toSuite, fromSuite);
|
|
|
|
|
} else {
|
|
|
|
|
const serialized = this._serializeSuite(fromSuite);
|
|
|
|
|
if (serialized) {
|
|
|
|
|
if (!to.suites)
|
|
|
|
|
to.suites = [];
|
|
|
|
|
to.suites.push(serialized);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (const test of from.tests) {
|
2021-07-16 21:40:33 +02:00
|
|
|
const toSpec = to.specs.find(s => s.title === test.title && s.file === toPosixPath(path.relative(this.config.rootDir, test.location.file)) && s.line === test.location.line && s.column === test.location.column);
|
2021-07-16 07:02:10 +02:00
|
|
|
if (toSpec)
|
|
|
|
|
toSpec.tests.push(this._serializeTest(test));
|
|
|
|
|
else
|
|
|
|
|
to.specs.push(this._serializeTestSpec(test));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-09 02:16:36 +02:00
|
|
|
private _serializeSuite(suite: Suite): null | JSONReportSuite {
|
2021-07-16 21:40:33 +02:00
|
|
|
if (!suite.allTests().length)
|
2021-06-07 02:09:53 +02:00
|
|
|
return null;
|
2021-07-09 02:16:36 +02:00
|
|
|
const suites = suite.suites.map(suite => this._serializeSuite(suite)).filter(s => s) as JSONReportSuite[];
|
2021-06-07 02:09:53 +02:00
|
|
|
return {
|
|
|
|
|
title: suite.title,
|
2021-07-16 21:40:33 +02:00
|
|
|
...this._relativeLocation(suite.location),
|
2021-07-16 07:02:10 +02:00
|
|
|
specs: suite.tests.map(test => this._serializeTestSpec(test)),
|
2021-06-07 02:09:53 +02:00
|
|
|
suites: suites.length ? suites : undefined,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-19 23:54:18 +02:00
|
|
|
private _serializeTestSpec(test: TestCase): JSONReportSpec {
|
2021-06-07 02:09:53 +02:00
|
|
|
return {
|
2021-07-16 07:02:10 +02:00
|
|
|
title: test.title,
|
|
|
|
|
ok: test.ok(),
|
2021-09-15 21:30:22 +02:00
|
|
|
tags: (test.title.match(/@[\S]+/g) || []).map(t => t.substring(1)),
|
2021-07-16 07:02:10 +02:00
|
|
|
tests: [ this._serializeTest(test) ],
|
2021-07-16 21:40:33 +02:00
|
|
|
...this._relativeLocation(test.location),
|
2021-06-07 02:09:53 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-19 23:54:18 +02:00
|
|
|
private _serializeTest(test: TestCase): JSONReportTest {
|
2021-06-07 02:09:53 +02:00
|
|
|
return {
|
|
|
|
|
timeout: test.timeout,
|
|
|
|
|
annotations: test.annotations,
|
|
|
|
|
expectedStatus: test.expectedStatus,
|
2021-07-17 00:23:50 +02:00
|
|
|
projectName: test.titlePath()[1],
|
2022-01-07 20:06:47 +01:00
|
|
|
results: test.results.map(r => this._serializeTestResult(r, test)),
|
2021-07-19 02:40:59 +02:00
|
|
|
status: test.outcome(),
|
2021-06-07 02:09:53 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-07 20:06:47 +01:00
|
|
|
private _serializeTestResult(result: TestResult, test: TestCase): JSONReportTestResult {
|
2021-09-07 22:35:30 +02:00
|
|
|
const steps = result.steps.filter(s => s.category === 'test.step');
|
2021-09-30 23:18:36 +02:00
|
|
|
const jsonResult: JSONReportTestResult = {
|
2021-06-07 02:09:53 +02:00
|
|
|
workerIndex: result.workerIndex,
|
|
|
|
|
status: result.status,
|
|
|
|
|
duration: result.duration,
|
|
|
|
|
error: result.error,
|
|
|
|
|
stdout: result.stdout.map(s => stdioEntry(s)),
|
|
|
|
|
stderr: result.stderr.map(s => stdioEntry(s)),
|
|
|
|
|
retry: result.retry,
|
2021-09-07 22:35:30 +02:00
|
|
|
steps: steps.length ? steps.map(s => this._serializeTestStep(s)) : undefined,
|
2021-07-16 22:48:37 +02:00
|
|
|
attachments: result.attachments.map(a => ({
|
|
|
|
|
name: a.name,
|
|
|
|
|
contentType: a.contentType,
|
|
|
|
|
path: a.path,
|
|
|
|
|
body: a.body?.toString('base64')
|
|
|
|
|
})),
|
2021-06-07 02:09:53 +02:00
|
|
|
};
|
2022-01-05 01:00:55 +01:00
|
|
|
if (result.error?.stack)
|
2022-01-07 20:06:47 +01:00
|
|
|
jsonResult.errorLocation = prepareErrorStack(result.error.stack, test.location.file).location;
|
2021-09-30 23:18:36 +02:00
|
|
|
return jsonResult;
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
2021-09-07 22:35:30 +02:00
|
|
|
|
|
|
|
|
private _serializeTestStep(step: TestStep): JSONReportTestStep {
|
|
|
|
|
const steps = step.steps.filter(s => s.category === 'test.step');
|
|
|
|
|
return {
|
|
|
|
|
title: step.title,
|
|
|
|
|
duration: step.duration,
|
|
|
|
|
error: step.error,
|
|
|
|
|
steps: steps.length ? steps.map(s => this._serializeTestStep(s)) : undefined,
|
|
|
|
|
};
|
|
|
|
|
}
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-09 02:16:36 +02:00
|
|
|
function outputReport(report: JSONReport, outputFile: string | undefined) {
|
2021-06-07 02:09:53 +02:00
|
|
|
const reportString = JSON.stringify(report, undefined, 2);
|
|
|
|
|
if (outputFile) {
|
|
|
|
|
fs.mkdirSync(path.dirname(outputFile), { recursive: true });
|
|
|
|
|
fs.writeFileSync(outputFile, reportString);
|
|
|
|
|
} else {
|
|
|
|
|
console.log(reportString);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function stdioEntry(s: string | Buffer): any {
|
|
|
|
|
if (typeof s === 'string')
|
|
|
|
|
return { text: s };
|
|
|
|
|
return { buffer: s.toString('base64') };
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-05 22:36:47 +02:00
|
|
|
export function serializePatterns(patterns: string | RegExp | (string | RegExp)[]): string[] {
|
2021-06-07 02:09:53 +02:00
|
|
|
if (!Array.isArray(patterns))
|
|
|
|
|
patterns = [patterns];
|
|
|
|
|
return patterns.map(s => s.toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default JSONReporter;
|