Skip to content

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

fix(fixtures): prevent a fixture named "then" from being awaited (#38948)

Joan Rieu1f039d773834
diff --git a/packages/playwright/src/worker/fixtureRunner.ts b/packages/playwright/src/worker/fixtureRunner.ts
index a8d43206cb..c00b9fe626 100644
--- a/packages/playwright/src/worker/fixtureRunner.ts
+++ b/packages/playwright/src/worker/fixtureRunner.ts
@@ -228,7 +228,7 @@ export class FixtureRunner {
throw firstError;
}
- async resolveParametersForFunction(fn: Function, testInfo: TestInfoImpl, autoFixtures: 'worker' | 'test' | 'all-hooks-only', runnable: RunnableDescription): Promise<object | null> {
+ async resolveParametersForFunction(fn: Function, testInfo: TestInfoImpl, autoFixtures: 'worker' | 'test' | 'all-hooks-only', runnable: RunnableDescription): Promise<{ result: object } | null> {
const collector = new Set<FixtureRegistration>();
// Collect automatic fixtures.
@@ -264,7 +264,8 @@ export class FixtureRunner {
return null;
params[name] = fixture.value;
}
- return params;
+ // Wrap in an object to avoid returning a thenable if a fixture is named 'then'.
+ return { result: params };
}
async resolveParametersAndRunFunction(fn: Function, testInfo: TestInfoImpl, autoFixtures: 'worker' | 'test' | 'all-hooks-only', runnable: RunnableDescription) {
@@ -273,7 +274,7 @@ export class FixtureRunner {
// Do not run the function when fixture setup has already failed.
return null;
}
- await testInfo._runWithTimeout(runnable, () => fn(params, testInfo));
+ await testInfo._runWithTimeout(runnable, () => fn(params.result, testInfo));
}
private async _setupFixtureForRegistration(registration: FixtureRegistration, testInfo: TestInfoImpl, runnable: RunnableDescription): Promise<Fixture> {
diff --git a/packages/playwright/src/worker/workerMain.ts b/packages/playwright/src/worker/workerMain.ts
index d1289a46e5..c2d1a93858 100644
--- a/packages/playwright/src/worker/workerMain.ts
+++ b/packages/playwright/src/worker/workerMain.ts
@@ -391,7 +391,10 @@ export class WorkerMain extends ProcessRunner {
await this._runEachHooksForSuites(suites, 'beforeEach', testInfo);
// Setup fixtures required by the test.
- testFunctionParams = await this._fixtureRunner.resolveParametersForFunction(test.fn, testInfo, 'test', { type: 'test' });
+ const params = await this._fixtureRunner.resolveParametersForFunction(test.fn, testInfo, 'test', { type: 'test' });
+ if (params !== null)
+ testFunctionParams = params.result;
+
});
if (testFunctionParams === null) {
diff --git a/tests/playwright-test/fixtures.spec.ts b/tests/playwright-test/fixtures.spec.ts
index 7bde432d0f..001f827852 100644
--- a/tests/playwright-test/fixtures.spec.ts
+++ b/tests/playwright-test/fixtures.spec.ts
@@ -834,3 +834,19 @@ test('should error if use is not called', async ({ runInlineTest }) => {
expect(result.failed).toBe(1);
expect(result.output).toContain(`use() was not called in fixture "fixture"`);
});
+
+test('should not treat fixtures as thenable promise', async ({ runInlineTest }) => {
+ const { results } = await runInlineTest({
+ 'a.test.ts': `
+ import { test as base, expect } from '@playwright/test';
+ const test = base.extend({
+ then: async ({}, use) => await use(() => 'test-function'),
+ });
+
+ test('should not treat fixtures as thenable promise', async ({ then }) => {
+ expect(typeof then).toBe('function');
+ });
+ `,
+ });
+ expect(results[0].status).toBe('passed');
+});