Skip to content

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

fix(test runner): make sure auto worker fixtures run before auto test fixtures (#27131)

Dmitry Gozman3bcf9687e650
diff --git a/packages/playwright/src/worker/fixtureRunner.ts b/packages/playwright/src/worker/fixtureRunner.ts
index 46456588cb..2e5e0d09c4 100644
--- a/packages/playwright/src/worker/fixtureRunner.ts
+++ b/packages/playwright/src/worker/fixtureRunner.ts
@@ -235,6 +235,7 @@ export class FixtureRunner {
async resolveParametersForFunction(fn: Function, testInfo: TestInfoImpl, autoFixtures: 'worker' | 'test' | 'all-hooks-only'): Promise<object | null> {
// Install automatic fixtures.
+ const auto: FixtureRegistration[] = [];
for (const registration of this.pool!.registrations.values()) {
if (registration.auto === false)
continue;
@@ -243,11 +244,14 @@ export class FixtureRunner {
shouldRun = registration.scope === 'worker' || registration.auto === 'all-hooks-included';
else if (autoFixtures === 'worker')
shouldRun = registration.scope === 'worker';
- if (shouldRun) {
- const fixture = await this.setupFixtureForRegistration(registration, testInfo);
- if (fixture.failed)
- return null;
- }
+ if (shouldRun)
+ auto.push(registration);
+ }
+ auto.sort((r1, r2) => (r1.scope === 'worker' ? 0 : 1) - (r2.scope === 'worker' ? 0 : 1));
+ for (const registration of auto) {
+ const fixture = await this.setupFixtureForRegistration(registration, testInfo);
+ if (fixture.failed)
+ return null;
}
// Install used fixtures.
diff --git a/tests/playwright-test/fixtures.spec.ts b/tests/playwright-test/fixtures.spec.ts
index 15a8ef5c75..a130f8dfcb 100644
--- a/tests/playwright-test/fixtures.spec.ts
+++ b/tests/playwright-test/fixtures.spec.ts
@@ -783,3 +783,36 @@ test('worker teardown errors reflected in timed-out tests', async ({ runInlineTe
expect(result.output).toContain('Test timeout of 1000ms exceeded.');
expect(result.output).toContain('Rejecting!');
});
+
+test('automatic worker fixtures should start before automatic test fixtures', async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ 'a.test.ts': `
+ import { test as base, expect } from '@playwright/test';
+ const test = base.extend({
+ autoTest: [async ({}, use) => {
+ console.log('\\n%%TEST FIXTURE 1');
+ await use();
+ console.log('\\n%%TEST FIXTURE 2');
+ }, { scope: 'test', auto: true }],
+
+ autoWorker: [async ({}, use) => {
+ console.log('\\n%%WORKER FIXTURE 1');
+ await use();
+ console.log('\\n%%WORKER FIXTURE 2');
+ }, { scope: 'worker', auto: true }],
+ });
+
+ test('test', async () => {
+ console.log('\\n%%TEST');
+ });
+ `
+ });
+ expect(result.exitCode).toBe(0);
+ expect(result.outputLines).toEqual([
+ 'WORKER FIXTURE 1',
+ 'TEST FIXTURE 1',
+ 'TEST',
+ 'TEST FIXTURE 2',
+ 'WORKER FIXTURE 2',
+ ]);
+});