Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore: turn modifiers into hooks early (#39508)

Dmitry Gozmanc06842628d83
diff --git a/packages/playwright/src/common/fixtures.ts b/packages/playwright/src/common/fixtures.ts
index 30b144e663..dad97674a3 100644
--- a/packages/playwright/src/common/fixtures.ts
+++ b/packages/playwright/src/common/fixtures.ts
@@ -228,12 +228,16 @@ export class FixturePool {
return hash.digest('hex');
}
- validateFunction(fn: Function, prefix: string, location: Location) {
+ validateFunction(fn: Function, prefix: string, location: Location): 'worker' | 'test' {
+ let scope: 'worker' | 'test' = 'worker';
for (const name of fixtureParameterNames(fn, location, e => this._onLoadError(e))) {
const registration = this._registrations.get(name);
if (!registration)
this._addLoadError(`${prefix} has unknown parameter "${name}".`, location);
+ if (registration?.scope === 'test')
+ scope = 'test';
}
+ return scope;
}
resolve(name: string, forFixture?: FixtureRegistration): FixtureRegistration | undefined {
diff --git a/packages/playwright/src/common/poolBuilder.ts b/packages/playwright/src/common/poolBuilder.ts
index f975efe92f..2571747db9 100644
--- a/packages/playwright/src/common/poolBuilder.ts
+++ b/packages/playwright/src/common/poolBuilder.ts
@@ -14,8 +14,9 @@
* limitations under the License.
*/
-import { FixturePool } from './fixtures';
+import { FixturePool, inheritFixtureNames } from './fixtures';
import { formatLocation } from '../util';
+import { currentTestInfo } from './globals';
import type { FullProjectInternal } from './config';
import type { LoadError } from './fixtures';
@@ -41,8 +42,32 @@ export class PoolBuilder {
this._project = project;
}
- buildPools(suite: Suite, testErrors?: TestError[]) {
- suite.forEachTest(test => {
+ buildPools(topSuite: Suite, testErrors?: TestError[]) {
+ topSuite.forEachSuite(suite => {
+ const modifiers = suite._modifiers.slice();
+ suite._modifiers = [];
+
+ for (const modifier of modifiers.reverse()) {
+ let pool = this._buildTestTypePool(modifier.testType, testErrors);
+ pool = this._buildPoolForSuite(pool, suite, testErrors);
+ const scope = pool.validateFunction(modifier.fn, modifier.type + ' modifier', modifier.location);
+
+ const fn = async (fixtures: any) => {
+ const result = await modifier.fn(fixtures);
+ currentTestInfo()?._modifier(modifier.type, modifier.location, [!!result, modifier.description]);
+ };
+ inheritFixtureNames(modifier.fn, fn);
+
+ suite._hooks.unshift({
+ type: scope === 'worker' ? 'beforeAll' : 'beforeEach',
+ fn,
+ title: `${modifier.type} modifier`,
+ location: modifier.location,
+ });
+ }
+ });
+
+ topSuite.forEachTest(test => {
const pool = this._buildPoolForTest(test, testErrors);
if (this._type === 'loader')
test._poolDigest = pool.digest;
@@ -53,25 +78,21 @@ export class PoolBuilder {
private _buildPoolForTest(test: TestCase, testErrors?: TestError[]): FixturePool {
let pool = this._buildTestTypePool(test._testType, testErrors);
-
- const parents: Suite[] = [];
- for (let parent: Suite | undefined = test.parent; parent; parent = parent.parent)
- parents.push(parent);
- parents.reverse();
-
- for (const parent of parents) {
- if (parent._use.length)
- pool = new FixturePool(parent._use, e => this._handleLoadError(e, testErrors), pool, parent._type === 'describe');
- for (const hook of parent._hooks)
- pool.validateFunction(hook.fn, hook.type + ' hook', hook.location);
- for (const modifier of parent._modifiers)
- pool.validateFunction(modifier.fn, modifier.type + ' modifier', modifier.location);
- }
-
+ pool = this._buildPoolForSuite(pool, test.parent, testErrors);
pool.validateFunction(test.fn, 'Test', test.location);
return pool;
}
+ private _buildPoolForSuite(pool: FixturePool, suite: Suite, testErrors?: TestError[]): FixturePool {
+ if (suite.parent)
+ pool = this._buildPoolForSuite(pool, suite.parent, testErrors);
+ if (suite._use.length)
+ pool = new FixturePool(suite._use, e => this._handleLoadError(e, testErrors), pool, suite._type === 'describe');
+ for (const hook of suite._hooks)
+ pool.validateFunction(hook.fn, hook.type + ' hook', hook.location);
+ return pool;
+ }
+
private _buildTestTypePool(testType: TestTypeImpl, testErrors?: TestError[]): FixturePool {
if (!this._testTypePools.has(testType)) {
const optionOverrides = {
diff --git a/packages/playwright/src/common/test.ts b/packages/playwright/src/common/test.ts
index fa8d8c2003..e4b904e908 100644
--- a/packages/playwright/src/common/test.ts
+++ b/packages/playwright/src/common/test.ts
@@ -35,11 +35,19 @@ class Base {
}
}
-export type Modifier = {
+type Modifier = {
type: 'slow' | 'fixme' | 'skip' | 'fail',
fn: Function,
location: Location,
- description: string | undefined
+ description: string | undefined,
+ testType: TestTypeImpl,
+};
+
+export type SuiteHook = {
+ type: 'beforeEach' | 'afterEach' | 'beforeAll' | 'afterAll';
+ fn: Function;
+ title: string;
+ location: Location;
};
export class Suite extends Base {
@@ -47,13 +55,14 @@ export class Suite extends Base {
parent?: Suite;
_use: FixturesWithLocation[] = [];
_entries: (Suite | TestCase)[] = [];
- _hooks: { type: 'beforeEach' | 'afterEach' | 'beforeAll' | 'afterAll', fn: Function, title: string, location: Location }[] = [];
+ _hooks: SuiteHook[] = [];
_timeout: number | undefined;
_retries: number | undefined;
// Annotations known statically before running the test, e.g. `test.describe.skip()` or `test.describe({ annotation }, body)`.
_staticAnnotations: TestAnnotation[] = [];
// Explicitly declared tags that are not a part of the title.
_tags: string[] = [];
+ // Modifiers are converted into hooks after the fixture pool is built.
_modifiers: Modifier[] = [];
_parallelMode: 'none' | 'default' | 'serial' | 'parallel' = 'none';
_fullProject: FullProjectInternal | undefined;
@@ -205,6 +214,14 @@ export class Suite extends Base {
}
}
+ forEachSuite(visitor: (suite: Suite) => void) {
+ visitor(this);
+ for (const entry of this._entries) {
+ if (entry instanceof Suite)
+ entry.forEachSuite(visitor);
+ }
+ }
+
_serialize(): any {
return {
kind: 'suite',
@@ -217,9 +234,9 @@ export class Suite extends Base {
retries: this._retries,
staticAnnotations: this._staticAnnotations.slice(),
tags: this._tags.slice(),
- modifiers: this._modifiers.slice(),
+ modifiers: this._modifiers.map(m => ({ ...m, fn: undefined, testType: undefined })),
parallelMode: this._parallelMode,
- hooks: this._hooks.map(h => ({ type: h.type, location: h.location, title: h.title })),
+ hooks: this._hooks.map(h => ({ ...h, fn: undefined })),
fileId: this._fileId,
};
}
@@ -233,9 +250,9 @@ export class Suite extends Base {
suite._retries = data.retries;
suite._staticAnnotations = data.staticAnnotations;
suite._tags = data.tags;
- suite._modifiers = data.modifiers;
+ suite._modifiers = data.modifiers.map((m: any) => ({ ...m, fn: () => { }, testType: rootTestType }));
suite._parallelMode = data.parallelMode;
- suite._hooks = data.hooks.map((h: any) => ({ type: h.type, location: h.location, title: h.title, fn: () => { } }));
+ suite._hooks = data.hooks.map((h: any) => ({ ...h, fn: () => { } }));
suite._fileId = data.fileId;
return suite;
}
@@ -245,6 +262,7 @@ export class Suite extends Base {
const suite = Suite._parse(data);
suite._use = this._use.slice();
suite._hooks = this._hooks.slice();
+ suite._modifiers = this._modifiers.slice();
suite._fullProject = this._fullProject;
return suite;
}
diff --git a/packages/playwright/src/common/testType.ts b/packages/playwright/src/common/testType.ts
index e8736b6677..7440c473e5 100644
--- a/packages/playwright/src/common/testType.ts
+++ b/packages/playwright/src/common/testType.ts
@@ -223,7 +223,7 @@ export class TestTypeImpl {
}
if (typeof modifierArgs[0] === 'function') {
- suite._modifiers.push({ type, fn: modifierArgs[0], location, description: modifierArgs[1] });
+ suite._modifiers.push({ type, fn: modifierArgs[0], location, description: modifierArgs[1], testType: this });
} else {
if (modifierArgs.length >= 1 && !modifierArgs[0])
return;
diff --git a/packages/playwright/src/worker/fixtureRunner.ts b/packages/playwright/src/worker/fixtureRunner.ts
index c00b9fe626..7f8e0eacf7 100644
--- a/packages/playwright/src/worker/fixtureRunner.ts
+++ b/packages/playwright/src/worker/fixtureRunner.ts
@@ -289,16 +289,6 @@ export class FixtureRunner {
await fixture.setup(testInfo, runnable);
return fixture;
}
-
- dependsOnWorkerFixturesOnly(fn: Function, location: Location): boolean {
- const names = getRequiredFixtureNames(fn, location);
- for (const name of names) {
- const registration = this.pool!.resolve(name)!;
- if (registration.scope !== 'worker')
- return false;
- }
- return true;
- }
}
function getRequiredFixtureNames(fn: Function, location?: Location) {
diff --git a/packages/playwright/src/worker/timeoutManager.ts b/packages/playwright/src/worker/timeoutManager.ts
index c003342d15..8d18fe098d 100644
--- a/packages/playwright/src/worker/timeoutManager.ts
+++ b/packages/playwright/src/worker/timeoutManager.ts
@@ -188,12 +188,6 @@ export class TimeoutManager {
message = `Worker teardown timeout of ${timeout}ms exceeded.`;
break;
}
- case 'skip':
- case 'slow':
- case 'fixme':
- case 'fail':
- message = `"${runnable.type}" modifier timeout of ${timeout}ms exceeded.`;
- break;
}
const fixtureWithSlot = runnable.fixture?.slot ? runnable.fixture : undefined;
if (fixtureWithSlot)
diff --git a/packages/playwright/src/worker/workerMain.ts b/packages/playwright/src/worker/workerMain.ts
index bdd46abaf0..8a7f5a960c 100644
--- a/packages/playwright/src/worker/workerMain.ts
+++ b/packages/playwright/src/worker/workerMain.ts
@@ -24,14 +24,12 @@ import { debugTest, relativeFilePath } from '../util';
import { FixtureRunner } from './fixtureRunner';
import { TestSkipError, TestInfoImpl, emtpyTestInfoCallbacks } from './testInfo';
import { testInfoError } from './util';
-import { inheritFixtureNames } from '../common/fixtures';
import { PoolBuilder } from '../common/poolBuilder';
import { ProcessRunner } from '../common/process';
import { applyRepeatEachIndex, bindFileSuiteToProject, filterTestsRemoveEmptySuites } from '../common/suiteUtils';
import { loadTestFile } from '../common/testLoader';
import type { TimeSlot } from './timeoutManager';
-import type { Location } from '../../types/testReporter';
import type { FullConfigInternal, FullProjectInternal } from '../common/config';
import type * as ipc from '../common/ipc';
import type { Suite, TestCase } from '../common/test';
@@ -527,30 +525,6 @@ export class WorkerMain extends ProcessRunner {
await removeFolders([testInfo.outputDir]);
}
- private _collectHooksAndModifiers(suite: Suite, type: 'beforeAll' | 'beforeEach' | 'afterAll' | 'afterEach', testInfo: TestInfoImpl) {
- type Runnable = { type: 'beforeEach' | 'afterEach' | 'beforeAll' | 'afterAll' | 'fixme' | 'skip' | 'slow' | 'fail', fn: Function, title: string, location: Location };
- const runnables: Runnable[] = [];
- for (const modifier of suite._modifiers) {
- const modifierType = this._fixtureRunner.dependsOnWorkerFixturesOnly(modifier.fn, modifier.location) ? 'beforeAll' : 'beforeEach';
- if (modifierType !== type)
- continue;
- const fn = async (fixtures: any) => {
- const result = await modifier.fn(fixtures);
- testInfo._modifier(modifier.type, modifier.location, [!!result, modifier.description]);
- };
- inheritFixtureNames(modifier.fn, fn);
- runnables.push({
- title: `${modifier.type} modifier`,
- location: modifier.location,
- type: modifier.type,
- fn,
- });
- }
- // Modifiers first, then hooks.
- runnables.push(...suite._hooks.filter(hook => hook.type === type));
- return runnables;
- }
-
private async _runBeforeAllHooksForSuite(suite: Suite, testInfo: TestInfoImpl) {
if (this._activeSuites.has(suite))
return;
@@ -562,7 +536,7 @@ export class WorkerMain extends ProcessRunner {
private async _runAllHooksForSuite(suite: Suite, testInfo: TestInfoImpl, type: 'beforeAll' | 'afterAll', extraAnnotations?: TestAnnotation[]) {
// Always run all the hooks, and capture the first error.
let firstError: Error | undefined;
- for (const hook of this._collectHooksAndModifiers(suite, type, testInfo)) {
+ for (const hook of suite._hooks.filter(hook => hook.type === type)) {
try {
await testInfo._runAsStep({ title: hook.title, category: 'hook', location: hook.location }, async () => {
// Separate time slot for each beforeAll/afterAll hook.
@@ -609,7 +583,7 @@ export class WorkerMain extends ProcessRunner {
private async _runEachHooksForSuites(suites: Suite[], type: 'beforeEach' | 'afterEach', testInfo: TestInfoImpl, slot?: TimeSlot) {
// Always run all the hooks, unless one of the times out, and capture the first error.
let firstError: Error | undefined;
- const hooks = suites.map(suite => this._collectHooksAndModifiers(suite, type, testInfo)).flat();
+ const hooks = suites.map(suite => suite._hooks.filter(hook => hook.type === type)).flat();
for (const hook of hooks) {
const runnable = { type: hook.type, location: hook.location, slot };
if (testInfo._timeoutManager.isTimeExhaustedFor(runnable)) {
diff --git a/tests/playwright-test/test-modifiers.spec.ts b/tests/playwright-test/test-modifiers.spec.ts
index e73e483b27..b6ff2f35d6 100644
--- a/tests/playwright-test/test-modifiers.spec.ts
+++ b/tests/playwright-test/test-modifiers.spec.ts
@@ -283,13 +283,13 @@ test.describe('test modifier annotations', () => {
const result = await runInlineTest({
'a.test.ts': `
import { test, expect } from '@playwright/test';
-
+
test.describe.only("suite", () => {
test.skip('focused skip by suite', () => {});
test.fixme('focused fixme by suite', () => {});
test.fail.only('focused fail by suite', () => { expect(1).toBe(2); });
});
-
+
test.describe.skip('not focused', () => {
test('no marker', () => {});
});
@@ -527,7 +527,7 @@ test('modifier timeout should be reported', async ({ runInlineTest }) => {
}, { timeout: 2000 });
expect(result.exitCode).toBe(1);
expect(result.failed).toBe(1);
- expect(result.output).toContain('"skip" modifier timeout of 2000ms exceeded.');
+ expect(result.output).toContain('"beforeAll" hook timeout of 2000ms exceeded.');
expect(result.output).toContain('3 | test.skip(async () => new Promise(() => {}));');
});