2021-06-06 17:09:53 -07:00
|
|
|
/**
|
|
|
|
* Copyright Microsoft Corporation. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2021-07-15 22:02:10 -07:00
|
|
|
import type { FixturePool } from './fixtures';
|
2022-04-06 13:57:14 -08:00
|
|
|
import type * as reporterTypes from '../types/testReporter';
|
2021-06-06 17:09:53 -07:00
|
|
|
import type { TestTypeImpl } from './testType';
|
2023-01-20 18:24:15 -08:00
|
|
|
import { rootTestType } from './testType';
|
2022-04-06 13:57:14 -08:00
|
|
|
import type { Annotation, FixturesWithLocation, FullProject, FullProjectInternal, Location } from './types';
|
2021-06-06 17:09:53 -07:00
|
|
|
|
|
|
|
class Base {
|
|
|
|
title: string;
|
|
|
|
_only = false;
|
2021-06-21 11:25:15 -07:00
|
|
|
_requireFile: string = '';
|
2021-06-06 17:09:53 -07:00
|
|
|
|
|
|
|
constructor(title: string) {
|
|
|
|
this.title = title;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-02 15:49:05 -07:00
|
|
|
export type Modifier = {
|
|
|
|
type: 'slow' | 'fixme' | 'skip' | 'fail',
|
|
|
|
fn: Function,
|
|
|
|
location: Location,
|
|
|
|
description: string | undefined
|
|
|
|
};
|
|
|
|
|
2021-06-06 17:09:53 -07:00
|
|
|
export class Suite extends Base implements reporterTypes.Suite {
|
2021-07-18 17:40:59 -07:00
|
|
|
location?: Location;
|
2021-10-19 08:38:04 -07:00
|
|
|
parent?: Suite;
|
2021-08-10 16:32:32 -07:00
|
|
|
_use: FixturesWithLocation[] = [];
|
2021-07-19 14:54:18 -07:00
|
|
|
_entries: (Suite | TestCase)[] = [];
|
2022-03-08 16:35:14 -08:00
|
|
|
_hooks: { type: 'beforeEach' | 'afterEach' | 'beforeAll' | 'afterAll', fn: Function, location: Location }[] = [];
|
2021-06-29 13:33:13 -07:00
|
|
|
_timeout: number | undefined;
|
2022-10-27 15:53:27 -07:00
|
|
|
_retries: number | undefined;
|
2023-01-24 12:49:47 -08:00
|
|
|
_staticAnnotations: Annotation[] = [];
|
2021-07-02 15:49:05 -07:00
|
|
|
_modifiers: Modifier[] = [];
|
2021-09-02 15:42:07 -07:00
|
|
|
_parallelMode: 'default' | 'serial' | 'parallel' = 'default';
|
2022-04-04 16:07:04 -07:00
|
|
|
_projectConfig: FullProjectInternal | undefined;
|
2022-07-27 20:17:19 -07:00
|
|
|
_fileId: string | undefined;
|
|
|
|
readonly _type: 'root' | 'project' | 'file' | 'describe';
|
|
|
|
|
|
|
|
constructor(title: string, type: 'root' | 'project' | 'file' | 'describe') {
|
|
|
|
super(title);
|
|
|
|
this._type = type;
|
|
|
|
}
|
2021-06-06 17:09:53 -07:00
|
|
|
|
2023-01-20 18:24:15 -08:00
|
|
|
get suites(): Suite[] {
|
|
|
|
return this._entries.filter(entry => entry instanceof Suite) as Suite[];
|
|
|
|
}
|
|
|
|
|
|
|
|
get tests(): TestCase[] {
|
|
|
|
return this._entries.filter(entry => entry instanceof TestCase) as TestCase[];
|
|
|
|
}
|
|
|
|
|
2021-07-19 14:54:18 -07:00
|
|
|
_addTest(test: TestCase) {
|
2021-07-15 22:02:10 -07:00
|
|
|
test.parent = this;
|
|
|
|
this._entries.push(test);
|
2021-06-06 17:09:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
_addSuite(suite: Suite) {
|
|
|
|
suite.parent = this;
|
|
|
|
this._entries.push(suite);
|
|
|
|
}
|
|
|
|
|
2021-07-19 14:54:18 -07:00
|
|
|
allTests(): TestCase[] {
|
|
|
|
const result: TestCase[] = [];
|
2021-07-16 12:40:33 -07:00
|
|
|
const visit = (suite: Suite) => {
|
|
|
|
for (const entry of suite._entries) {
|
|
|
|
if (entry instanceof Suite)
|
|
|
|
visit(entry);
|
|
|
|
else
|
|
|
|
result.push(entry);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
visit(this);
|
2021-06-06 17:09:53 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-10-19 08:38:04 -07:00
|
|
|
titlePath(): string[] {
|
|
|
|
const titlePath = this.parent ? this.parent.titlePath() : [];
|
2022-07-06 13:54:11 -07:00
|
|
|
// Ignore anonymous describe blocks.
|
2022-07-27 20:17:19 -07:00
|
|
|
if (this.title || this._type !== 'describe')
|
2022-07-06 13:54:11 -07:00
|
|
|
titlePath.push(this.title);
|
2021-10-19 08:38:04 -07:00
|
|
|
return titlePath;
|
|
|
|
}
|
|
|
|
|
2021-07-19 14:54:18 -07:00
|
|
|
_getOnlyItems(): (TestCase | Suite)[] {
|
|
|
|
const items: (TestCase | Suite)[] = [];
|
2021-06-06 17:09:53 -07:00
|
|
|
if (this._only)
|
2021-06-28 22:13:35 +02:00
|
|
|
items.push(this);
|
|
|
|
for (const suite of this.suites)
|
|
|
|
items.push(...suite._getOnlyItems());
|
2021-07-15 22:02:10 -07:00
|
|
|
items.push(...this.tests.filter(test => test._only));
|
2021-06-28 22:13:35 +02:00
|
|
|
return items;
|
2021-06-06 17:09:53 -07:00
|
|
|
}
|
|
|
|
|
2022-11-22 16:22:48 -08:00
|
|
|
_deepClone(): Suite {
|
|
|
|
const suite = this._clone();
|
|
|
|
for (const entry of this._entries) {
|
|
|
|
if (entry instanceof Suite)
|
|
|
|
suite._addSuite(entry._deepClone());
|
|
|
|
else
|
|
|
|
suite._addTest(entry._clone());
|
|
|
|
}
|
|
|
|
return suite;
|
|
|
|
}
|
|
|
|
|
2023-01-20 18:24:15 -08:00
|
|
|
_deepSerialize(): any {
|
|
|
|
const suite = this._serialize();
|
|
|
|
suite.entries = [];
|
|
|
|
for (const entry of this._entries) {
|
|
|
|
if (entry instanceof Suite)
|
|
|
|
suite.entries.push(entry._deepSerialize());
|
|
|
|
else
|
|
|
|
suite.entries.push(entry._serialize());
|
|
|
|
}
|
|
|
|
return suite;
|
|
|
|
}
|
|
|
|
|
|
|
|
static _deepParse(data: any): Suite {
|
|
|
|
const suite = Suite._parse(data);
|
|
|
|
for (const entry of data.entries) {
|
|
|
|
if (entry.kind === 'suite')
|
|
|
|
suite._addSuite(Suite._deepParse(entry));
|
|
|
|
else
|
|
|
|
suite._addTest(TestCase._parse(entry));
|
|
|
|
}
|
|
|
|
return suite;
|
|
|
|
}
|
|
|
|
|
2023-01-19 15:56:57 -08:00
|
|
|
forEachTest(visitor: (test: TestCase, suite: Suite) => void) {
|
|
|
|
for (const entry of this._entries) {
|
|
|
|
if (entry instanceof Suite)
|
|
|
|
entry.forEachTest(visitor);
|
|
|
|
else
|
|
|
|
visitor(entry, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-20 18:24:15 -08:00
|
|
|
_serialize(): any {
|
|
|
|
return {
|
|
|
|
kind: 'suite',
|
|
|
|
title: this.title,
|
|
|
|
type: this._type,
|
|
|
|
location: this.location,
|
|
|
|
only: this._only,
|
|
|
|
requireFile: this._requireFile,
|
|
|
|
timeout: this._timeout,
|
|
|
|
retries: this._retries,
|
2023-01-24 12:49:47 -08:00
|
|
|
staticAnnotations: this._staticAnnotations.slice(),
|
2023-01-20 18:24:15 -08:00
|
|
|
modifiers: this._modifiers.slice(),
|
|
|
|
parallelMode: this._parallelMode,
|
|
|
|
hooks: this._hooks.map(h => ({ type: h.type, location: h.location })),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static _parse(data: any): Suite {
|
|
|
|
const suite = new Suite(data.title, data.type);
|
|
|
|
suite.location = data.location;
|
|
|
|
suite._only = data.only;
|
|
|
|
suite._requireFile = data.requireFile;
|
|
|
|
suite._timeout = data.timeout;
|
|
|
|
suite._retries = data.retries;
|
2023-01-24 12:49:47 -08:00
|
|
|
suite._staticAnnotations = data.staticAnnotations;
|
2023-01-20 18:24:15 -08:00
|
|
|
suite._modifiers = data.modifiers;
|
|
|
|
suite._parallelMode = data.parallelMode;
|
|
|
|
suite._hooks = data.hooks.map((h: any) => ({ type: h.type, location: h.location, fn: () => { } }));
|
|
|
|
return suite;
|
|
|
|
}
|
|
|
|
|
2021-07-15 22:02:10 -07:00
|
|
|
_clone(): Suite {
|
2023-01-20 18:24:15 -08:00
|
|
|
const data = this._serialize();
|
|
|
|
const suite = Suite._parse(data);
|
2021-08-10 16:32:32 -07:00
|
|
|
suite._use = this._use.slice();
|
2022-03-08 16:35:14 -08:00
|
|
|
suite._hooks = this._hooks.slice();
|
2021-10-19 08:38:04 -07:00
|
|
|
suite._projectConfig = this._projectConfig;
|
2021-07-15 22:02:10 -07:00
|
|
|
return suite;
|
|
|
|
}
|
2021-10-19 08:38:04 -07:00
|
|
|
|
|
|
|
project(): FullProject | undefined {
|
|
|
|
return this._projectConfig || this.parent?.project();
|
|
|
|
}
|
2021-06-06 17:09:53 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 14:54:18 -07:00
|
|
|
export class TestCase extends Base implements reporterTypes.TestCase {
|
2021-07-15 22:02:10 -07:00
|
|
|
fn: Function;
|
2021-06-06 17:09:53 -07:00
|
|
|
results: reporterTypes.TestResult[] = [];
|
2021-07-18 17:40:59 -07:00
|
|
|
location: Location;
|
2021-10-19 08:38:04 -07:00
|
|
|
parent!: Suite;
|
2021-06-06 17:09:53 -07:00
|
|
|
|
|
|
|
expectedStatus: reporterTypes.TestStatus = 'passed';
|
|
|
|
timeout = 0;
|
2022-03-08 16:35:14 -08:00
|
|
|
annotations: Annotation[] = [];
|
2021-06-06 17:09:53 -07:00
|
|
|
retries = 0;
|
2022-01-03 17:29:54 -08:00
|
|
|
repeatEachIndex = 0;
|
2021-06-06 17:09:53 -07:00
|
|
|
|
2021-07-15 22:02:10 -07:00
|
|
|
_testType: TestTypeImpl;
|
2022-07-27 20:17:19 -07:00
|
|
|
id = '';
|
2021-07-15 22:02:10 -07:00
|
|
|
_pool: FixturePool | undefined;
|
2023-01-20 08:36:31 -08:00
|
|
|
_poolDigest = '';
|
|
|
|
_workerHash = '';
|
2022-07-27 20:17:19 -07:00
|
|
|
_projectId = '';
|
2023-01-24 12:49:47 -08:00
|
|
|
// Annotations known statically before running the test, e.g. `test.skip()` or `test.describe.skip()`.
|
|
|
|
_staticAnnotations: Annotation[] = [];
|
2021-06-06 17:09:53 -07:00
|
|
|
|
2022-03-08 19:06:25 -08:00
|
|
|
constructor(title: string, fn: Function, testType: TestTypeImpl, location: Location) {
|
2021-07-15 22:02:10 -07:00
|
|
|
super(title);
|
|
|
|
this.fn = fn;
|
|
|
|
this._testType = testType;
|
2021-07-18 17:40:59 -07:00
|
|
|
this.location = location;
|
2021-06-06 17:09:53 -07:00
|
|
|
}
|
|
|
|
|
2021-10-19 08:38:04 -07:00
|
|
|
titlePath(): string[] {
|
|
|
|
const titlePath = this.parent ? this.parent.titlePath() : [];
|
|
|
|
titlePath.push(this.title);
|
|
|
|
return titlePath;
|
|
|
|
}
|
|
|
|
|
2021-07-18 17:40:59 -07:00
|
|
|
outcome(): 'skipped' | 'expected' | 'unexpected' | 'flaky' {
|
2022-08-02 12:55:43 -07:00
|
|
|
const nonSkipped = this.results.filter(result => result.status !== 'skipped' && result.status !== 'interrupted');
|
2021-08-10 21:26:45 -07:00
|
|
|
if (!nonSkipped.length)
|
2021-06-06 17:09:53 -07:00
|
|
|
return 'skipped';
|
2021-08-10 21:26:45 -07:00
|
|
|
if (nonSkipped.every(result => result.status === this.expectedStatus))
|
2021-07-28 15:43:37 -07:00
|
|
|
return 'expected';
|
2021-08-10 21:26:45 -07:00
|
|
|
if (nonSkipped.some(result => result.status === this.expectedStatus))
|
2021-06-06 17:09:53 -07:00
|
|
|
return 'flaky';
|
|
|
|
return 'unexpected';
|
|
|
|
}
|
|
|
|
|
|
|
|
ok(): boolean {
|
2021-07-18 17:40:59 -07:00
|
|
|
const status = this.outcome();
|
2021-06-06 17:09:53 -07:00
|
|
|
return status === 'expected' || status === 'flaky' || status === 'skipped';
|
|
|
|
}
|
|
|
|
|
2023-01-20 18:24:15 -08:00
|
|
|
_serialize(): any {
|
|
|
|
return {
|
|
|
|
kind: 'test',
|
|
|
|
title: this.title,
|
|
|
|
location: this.location,
|
|
|
|
only: this._only,
|
|
|
|
requireFile: this._requireFile,
|
|
|
|
poolDigest: this._poolDigest,
|
|
|
|
expectedStatus: this.expectedStatus,
|
2023-01-24 12:49:47 -08:00
|
|
|
staticAnnotations: this._staticAnnotations.slice(),
|
2023-01-20 18:24:15 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static _parse(data: any): TestCase {
|
|
|
|
const test = new TestCase(data.title, () => {}, rootTestType, data.location);
|
|
|
|
test._only = data.only;
|
|
|
|
test._requireFile = data.requireFile;
|
|
|
|
test._poolDigest = data.poolDigest;
|
|
|
|
test.expectedStatus = data.expectedStatus;
|
2023-01-24 12:49:47 -08:00
|
|
|
test._staticAnnotations = data.staticAnnotations;
|
2023-01-20 18:24:15 -08:00
|
|
|
return test;
|
|
|
|
}
|
|
|
|
|
2021-07-19 14:54:18 -07:00
|
|
|
_clone(): TestCase {
|
2023-01-20 18:24:15 -08:00
|
|
|
const data = this._serialize();
|
|
|
|
const test = TestCase._parse(data);
|
|
|
|
test._testType = this._testType;
|
|
|
|
test.fn = this.fn;
|
2021-07-15 22:02:10 -07:00
|
|
|
return test;
|
|
|
|
}
|
|
|
|
|
2021-06-06 17:09:53 -07:00
|
|
|
_appendTestResult(): reporterTypes.TestResult {
|
|
|
|
const result: reporterTypes.TestResult = {
|
|
|
|
retry: this.results.length,
|
2022-12-20 02:37:04 +04:00
|
|
|
parallelIndex: -1,
|
2022-04-08 15:23:23 -07:00
|
|
|
workerIndex: -1,
|
2021-06-06 17:09:53 -07:00
|
|
|
duration: 0,
|
2021-07-18 17:40:59 -07:00
|
|
|
startTime: new Date(),
|
2021-06-06 17:09:53 -07:00
|
|
|
stdout: [],
|
|
|
|
stderr: [],
|
2021-07-16 13:48:37 -07:00
|
|
|
attachments: [],
|
2021-07-28 15:43:37 -07:00
|
|
|
status: 'skipped',
|
2022-02-02 19:33:51 -07:00
|
|
|
steps: [],
|
|
|
|
errors: [],
|
2021-06-06 17:09:53 -07:00
|
|
|
};
|
|
|
|
this.results.push(result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|