Skip to content

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

feat(test runner): improve fixture typings for function fixtures (#9138)

Dmitry Gozmand22dd4a4e7a5
diff --git a/tests/playwright-test/types.spec.ts b/tests/playwright-test/types.spec.ts
index 234eb9b23b..91329580c4 100644
--- a/tests/playwright-test/types.spec.ts
+++ b/tests/playwright-test/types.spec.ts
@@ -82,6 +82,18 @@ test('should check types of fixtures', async ({runTSC}) => {
// @ts-expect-error
}, { scope: 'test' } ],
});
+
+ type AssertNotAny<S> = {notRealProperty: number} extends S ? false : true;
+ type AssertType<T, S> = S extends T ? AssertNotAny<S> : false;
+ const funcTest = pwt.test.extend<{ foo: (x: number, y: string) => Promise<string> }>({
+ foo: async ({}, use) => {
+ await use(async (x, y) => {
+ const assertionX: AssertType<number, typeof x> = true;
+ const assertionY: AssertType<string, typeof y> = true;
+ return y;
+ });
+ },
+ })
`,
'playwright.config.ts': `
import { MyOptions } from './helper';
diff --git a/types/test.d.ts b/types/test.d.ts
index 8375c4f8e3..708087b0d4 100644
--- a/types/test.d.ts
+++ b/types/test.d.ts
@@ -2282,8 +2282,8 @@ export interface TestType<TestArgs extends KeyValue, WorkerArgs extends KeyValue
type KeyValue = { [key: string]: any };
export type TestFixture<R, Args extends KeyValue> = (args: Args, use: (r: R) => Promise<void>, testInfo: TestInfo) => any;
export type WorkerFixture<R, Args extends KeyValue> = (args: Args, use: (r: R) => Promise<void>, workerInfo: WorkerInfo) => any;
-type TestFixtureValue<R, Args> = R | TestFixture<R, Args>;
-type WorkerFixtureValue<R, Args> = R | WorkerFixture<R, Args>;
+type TestFixtureValue<R, Args> = Exclude<R, Function> | TestFixture<R, Args>;
+type WorkerFixtureValue<R, Args> = Exclude<R, Function> | WorkerFixture<R, Args>;
export type Fixtures<T extends KeyValue = {}, W extends KeyValue = {}, PT extends KeyValue = {}, PW extends KeyValue = {}> = {
[K in keyof PW]?: WorkerFixtureValue<PW[K], W & PW> | [WorkerFixtureValue<PW[K], W & PW>, { scope: 'worker' }];
} & {
diff --git a/utils/generate_types/overrides-test.d.ts b/utils/generate_types/overrides-test.d.ts
index 0dc3004bd1..48f9c0074c 100644
--- a/utils/generate_types/overrides-test.d.ts
+++ b/utils/generate_types/overrides-test.d.ts
@@ -265,8 +265,8 @@ export interface TestType<TestArgs extends KeyValue, WorkerArgs extends KeyValue
type KeyValue = { [key: string]: any };
export type TestFixture<R, Args extends KeyValue> = (args: Args, use: (r: R) => Promise<void>, testInfo: TestInfo) => any;
export type WorkerFixture<R, Args extends KeyValue> = (args: Args, use: (r: R) => Promise<void>, workerInfo: WorkerInfo) => any;
-type TestFixtureValue<R, Args> = R | TestFixture<R, Args>;
-type WorkerFixtureValue<R, Args> = R | WorkerFixture<R, Args>;
+type TestFixtureValue<R, Args> = Exclude<R, Function> | TestFixture<R, Args>;
+type WorkerFixtureValue<R, Args> = Exclude<R, Function> | WorkerFixture<R, Args>;
export type Fixtures<T extends KeyValue = {}, W extends KeyValue = {}, PT extends KeyValue = {}, PW extends KeyValue = {}> = {
[K in keyof PW]?: WorkerFixtureValue<PW[K], W & PW> | [WorkerFixtureValue<PW[K], W & PW>, { scope: 'worker' }];
} & {