Skip to content

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

Revert "chore: turn modifiers into hooks early (#39508)" (#39542)

Dmitry Gozmanfecb58c2bcf3
diff --git a/packages/playwright/src/common/fixtures.ts b/packages/playwright/src/common/fixtures.ts
index dad97674a3..30b144e663 100644
--- a/packages/playwright/src/common/fixtures.ts
+++ b/packages/playwright/src/common/fixtures.ts
@@ -228,16 +228,12 @@ export class FixturePool {
return hash.digest('hex');
}
- validateFunction(fn: Function, prefix: string, location: Location): 'worker' | 'test' {
- let scope: 'worker' | 'test' = 'worker';
+ validateFunction(fn: Function, prefix: string, location: Location) {
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 2571747db9..f975efe92f 100644
--- a/packages/playwright/src/common/poolBuilder.ts
+++ b/packages/playwright/src/common/poolBuilder.ts
@@ -14,9 +14,8 @@
* limitations under the License.
*/
-import { FixturePool, inheritFixtureNames } from './fixtures';
+import { FixturePool } from './fixtures';
import { formatLocation } from '../util';
-import { currentTestInfo } from './globals';
import type { FullProjectInternal } from './config';
import type { LoadError } from './fixtures';
@@ -42,32 +41,8 @@ export class PoolBuilder {
this._project = project;
}
- 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 => {
+ buildPools(suite: Suite, testErrors?: TestError[]) {
+ suite.forEachTest(test => {
const pool = this._buildPoolForTest(test, testErrors);
if (this._type === 'loader')
test._poolDigest = pool.digest;
@@ -78,18 +53,22 @@ export class PoolBuilder {
private _buildPoolForTest(test: TestCase, testErrors?: TestError[]): FixturePool {
let pool = this._buildTestTypePool(test._testType, testErrors);
- 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);
+ 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.validateFunction(test.fn, 'Test', test.location);
return pool;
}
diff --git a/packages/playwright/src/common/test.ts b/packages/playwright/src/common/test.ts
index e4b904e908..fa8d8c2003 100644
--- a/packages/playwright/src/common/test.ts
+++ b/packages/playwright/src/common/test.ts
@@ -35,19 +35,11 @@ class Base {
}
}
-type Modifier = {
+export type Modifier = {
type: 'slow' | 'fixme' | 'skip' | 'fail',
fn: Function,
location: Location,
- description: string | undefined,
- testType: TestTypeImpl,
-};
-
-export type SuiteHook = {
- type: 'beforeEach' | 'afterEach' | 'beforeAll' | 'afterAll';
- fn: Function;
- title: string;
- location: Location;
+ description: string | undefined
};
export class Suite extends Base {
@@ -55,14 +47,13 @@ export class Suite extends Base {
parent?: Suite;
_use: FixturesWithLocation[] = [];
_entries: (Suite | TestCase)[] = [];
- _hooks: SuiteHook[] = [];
+ _hooks: { type: 'beforeEach' | 'afterEach' | 'beforeAll' | 'afterAll', fn: Function, title: string, location: Location }[] = [];
_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;
@@ -214,14 +205,6 @@ 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',
@@ -234,9 +217,9 @@ export class Suite extends Base {
retries: this._retries,
staticAnnotations: this._staticAnnotations.slice(),
tags: this._tags.slice(),
- modifiers: this._modifiers.map(m => ({ ...m, fn: undefined, testType: undefined })),
+ modifiers: this._modifiers.slice(),
parallelMode: this._parallelMode,
- hooks: this._hooks.map(h => ({ ...h, fn: undefined })),
+ hooks: this._hooks.map(h => ({ type: h.type, location: h.location, title: h.title })),
fileId: this._fileId,
};
}
@@ -250,9 +233,9 @@ export class Suite extends Base {
suite._retries = data.retries;
suite._staticAnnotations = data.staticAnnotations;
suite._tags = data.tags;
- suite._modifiers = data.modifiers.map((m: any) => ({ ...m, fn: () => { }, testType: rootTestType }));
+ suite._modifiers = data.modifiers;
suite._parallelMode = data.parallelMode;
- suite._hooks = data.hooks.map((h: any) => ({ ...h, fn: () => { } }));
+ suite._hooks = data.hooks.map((h: any) => ({ type: h.type, location: h.location, title: h.title, fn: () => { } }));
suite._fileId = data.fileId;
return suite;
}
@@ -262,7 +245,6 @@ 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 7440c473e5..e8736b6677 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], testType: this });
+ suite._modifiers.push({ type, fn: modifierArgs[0], location, description: modifierArgs[1] });
} 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 7f8e0eacf7..c00b9fe626 100644
--- a/packages/playwright/src/worker/fixtureRunner.ts
+++ b/packages/playwright/src/worker/fixtureRunner.ts
@@ -289,6 +289,16 @@ 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 8d18fe098d..c003342d15 100644
--- a/packages/playwright/src/worker/timeoutManager.ts
+++ b/packages/playwright/src/worker/timeoutManager.ts
@@ -188,6 +188,12 @@ 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 8a7f5a960c..bdd46abaf0 100644
--- a/packages/playwright/src/worker/workerMain.ts
+++ b/packages/playwright/src/worker/workerMain.ts
@@ -24,12 +24,14 @@ 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';
@@ -525,6 +527,30 @@ 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;
@@ -536,7 +562,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 suite._hooks.filter(hook => hook.type === type)) {
+ for (const hook of this._collectHooksAndModifiers(suite, type, testInfo)) {
try {
await testInfo._runAsStep({ title: hook.title, category: 'hook', location: hook.location }, async () => {
// Separate time slot for each beforeAll/afterAll hook.
@@ -583,7 +609,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 => suite._hooks.filter(hook => hook.type === type)).flat();
+ const hooks = suites.map(suite => this._collectHooksAndModifiers(suite, type, testInfo)).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 b6ff2f35d6..e73e483b27 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('"beforeAll" hook timeout of 2000ms exceeded.');
+ expect(result.output).toContain('"skip" modifier timeout of 2000ms exceeded.');
expect(result.output).toContain('3 | test.skip(async () => new Promise(() => {}));');
});