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';
|
2021-06-06 17:09:53 -07:00
|
|
|
import * as reporterTypes from './reporter';
|
|
|
|
import type { TestTypeImpl } from './testType';
|
2021-07-02 15:49:05 -07:00
|
|
|
import { Annotations, Location } from './types';
|
2021-06-06 17:09:53 -07:00
|
|
|
|
|
|
|
class Base {
|
|
|
|
title: string;
|
|
|
|
file: string = '';
|
|
|
|
line: number = 0;
|
|
|
|
column: number = 0;
|
|
|
|
parent?: Suite;
|
|
|
|
|
2021-07-15 22:02:10 -07:00
|
|
|
_fullTitle: string = '';
|
2021-06-06 17:09:53 -07:00
|
|
|
_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-15 22:02:10 -07:00
|
|
|
_buildFullTitle(parentFullTitle: string) {
|
|
|
|
if (this.title)
|
|
|
|
this._fullTitle = (parentFullTitle ? parentFullTitle + ' ' : '') + this.title;
|
|
|
|
else
|
|
|
|
this._fullTitle = parentFullTitle;
|
2021-06-06 17:09:53 -07:00
|
|
|
}
|
2021-06-28 22:13:35 +02:00
|
|
|
|
|
|
|
fullTitle(): string {
|
2021-07-15 22:02:10 -07:00
|
|
|
return this._fullTitle;
|
2021-06-06 17:09:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
suites: Suite[] = [];
|
2021-07-15 22:02:10 -07:00
|
|
|
tests: Test[] = [];
|
2021-06-06 17:09:53 -07:00
|
|
|
_fixtureOverrides: any = {};
|
2021-07-15 22:02:10 -07:00
|
|
|
_entries: (Suite | Test)[] = [];
|
2021-06-06 17:09:53 -07:00
|
|
|
_hooks: {
|
|
|
|
type: 'beforeEach' | 'afterEach' | 'beforeAll' | 'afterAll',
|
|
|
|
fn: Function,
|
|
|
|
location: Location,
|
2021-06-28 22:13:35 +02:00
|
|
|
}[] = [];
|
2021-06-29 13:33:13 -07:00
|
|
|
_timeout: number | undefined;
|
2021-07-02 15:49:05 -07:00
|
|
|
_annotations: Annotations = [];
|
|
|
|
_modifiers: Modifier[] = [];
|
2021-07-15 22:02:10 -07:00
|
|
|
_repeatEachIndex = 0;
|
|
|
|
_projectIndex = 0;
|
2021-06-06 17:09:53 -07:00
|
|
|
|
2021-07-15 22:02:10 -07:00
|
|
|
_addTest(test: Test) {
|
|
|
|
test.parent = this;
|
|
|
|
test.suite = this;
|
|
|
|
this.tests.push(test);
|
|
|
|
this._entries.push(test);
|
2021-06-06 17:09:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
_addSuite(suite: Suite) {
|
|
|
|
suite.parent = this;
|
|
|
|
this.suites.push(suite);
|
|
|
|
this._entries.push(suite);
|
|
|
|
}
|
|
|
|
|
|
|
|
findTest(fn: (test: Test) => boolean | void): boolean {
|
|
|
|
for (const entry of this._entries) {
|
|
|
|
if (entry instanceof Suite) {
|
|
|
|
if (entry.findTest(fn))
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
if (fn(entry))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
totalTestCount(): number {
|
|
|
|
let total = 0;
|
|
|
|
for (const suite of this.suites)
|
|
|
|
total += suite.totalTestCount();
|
2021-07-15 22:02:10 -07:00
|
|
|
total += this.tests.length;
|
2021-06-06 17:09:53 -07:00
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2021-07-15 22:02:10 -07:00
|
|
|
_allTests(): Test[] {
|
|
|
|
const result: Test[] = [];
|
|
|
|
this.findTest(test => { result.push(test); });
|
2021-06-06 17:09:53 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-07-15 22:02:10 -07:00
|
|
|
_getOnlyItems(): (Test | Suite)[] {
|
|
|
|
const items: (Test | 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
|
|
|
}
|
|
|
|
|
|
|
|
_buildFixtureOverrides(): any {
|
|
|
|
return this.parent ? { ...this.parent._buildFixtureOverrides(), ...this._fixtureOverrides } : this._fixtureOverrides;
|
|
|
|
}
|
2021-07-15 22:02:10 -07:00
|
|
|
|
|
|
|
_clone(): Suite {
|
|
|
|
const suite = new Suite(this.title);
|
|
|
|
suite._only = this._only;
|
|
|
|
suite.file = this.file;
|
|
|
|
suite.line = this.line;
|
|
|
|
suite.column = this.column;
|
|
|
|
suite._requireFile = this._requireFile;
|
|
|
|
suite._fixtureOverrides = this._fixtureOverrides;
|
|
|
|
suite._hooks = this._hooks.slice();
|
|
|
|
suite._timeout = this._timeout;
|
|
|
|
suite._annotations = this._annotations.slice();
|
|
|
|
suite._modifiers = this._modifiers.slice();
|
|
|
|
return suite;
|
|
|
|
}
|
2021-06-06 17:09:53 -07:00
|
|
|
}
|
|
|
|
|
2021-07-15 22:02:10 -07:00
|
|
|
export class Test extends Base implements reporterTypes.Test {
|
|
|
|
suite!: Suite;
|
|
|
|
fn: Function;
|
2021-06-06 17:09:53 -07:00
|
|
|
results: reporterTypes.TestResult[] = [];
|
|
|
|
|
|
|
|
skipped = false;
|
|
|
|
expectedStatus: reporterTypes.TestStatus = 'passed';
|
|
|
|
timeout = 0;
|
2021-07-02 15:49:05 -07:00
|
|
|
annotations: Annotations = [];
|
2021-06-06 17:09:53 -07:00
|
|
|
projectName = '';
|
|
|
|
retries = 0;
|
|
|
|
|
2021-07-15 22:02:10 -07:00
|
|
|
_ordinalInFile: number;
|
|
|
|
_testType: TestTypeImpl;
|
2021-06-06 17:09:53 -07:00
|
|
|
_id = '';
|
|
|
|
_workerHash = '';
|
2021-07-15 22:02:10 -07:00
|
|
|
_pool: FixturePool | undefined;
|
2021-06-06 17:09:53 -07:00
|
|
|
|
2021-07-15 22:02:10 -07:00
|
|
|
constructor(title: string, fn: Function, ordinalInFile: number, testType: TestTypeImpl) {
|
|
|
|
super(title);
|
|
|
|
this.fn = fn;
|
|
|
|
this._ordinalInFile = ordinalInFile;
|
|
|
|
this._testType = testType;
|
2021-06-06 17:09:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
status(): 'skipped' | 'expected' | 'unexpected' | 'flaky' {
|
|
|
|
if (this.skipped)
|
|
|
|
return 'skipped';
|
|
|
|
// List mode bail out.
|
|
|
|
if (!this.results.length)
|
|
|
|
return 'skipped';
|
|
|
|
if (this.results.length === 1 && this.expectedStatus === this.results[0].status)
|
|
|
|
return 'expected';
|
|
|
|
let hasPassedResults = false;
|
|
|
|
for (const result of this.results) {
|
|
|
|
// Missing status is Ok when running in shards mode.
|
|
|
|
if (!result.status)
|
|
|
|
return 'skipped';
|
|
|
|
if (result.status === this.expectedStatus)
|
|
|
|
hasPassedResults = true;
|
|
|
|
}
|
|
|
|
if (hasPassedResults)
|
|
|
|
return 'flaky';
|
|
|
|
return 'unexpected';
|
|
|
|
}
|
|
|
|
|
|
|
|
ok(): boolean {
|
|
|
|
const status = this.status();
|
|
|
|
return status === 'expected' || status === 'flaky' || status === 'skipped';
|
|
|
|
}
|
|
|
|
|
2021-07-15 22:02:10 -07:00
|
|
|
_clone(): Test {
|
|
|
|
const test = new Test(this.title, this.fn, this._ordinalInFile, this._testType);
|
|
|
|
test._only = this._only;
|
|
|
|
test.file = this.file;
|
|
|
|
test.line = this.line;
|
|
|
|
test.column = this.column;
|
|
|
|
test._requireFile = this._requireFile;
|
|
|
|
return test;
|
|
|
|
}
|
|
|
|
|
2021-06-06 17:09:53 -07:00
|
|
|
fullTitle(): string {
|
2021-07-15 22:02:10 -07:00
|
|
|
return (this.projectName ? `[${this.projectName}] ` : '') + this._fullTitle;
|
2021-06-06 17:09:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
_appendTestResult(): reporterTypes.TestResult {
|
|
|
|
const result: reporterTypes.TestResult = {
|
|
|
|
retry: this.results.length,
|
|
|
|
workerIndex: 0,
|
|
|
|
duration: 0,
|
|
|
|
stdout: [],
|
|
|
|
stderr: [],
|
2021-07-08 17:16:36 -07:00
|
|
|
data: {},
|
2021-06-06 17:09:53 -07:00
|
|
|
};
|
|
|
|
this.results.push(result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|