diff --git a/packages/playwright-ct-react/hooks.d.ts b/packages/playwright-ct-react/hooks.d.ts index 94e83bc9af..aa4680694a 100644 --- a/packages/playwright-ct-react/hooks.d.ts +++ b/packages/playwright-ct-react/hooks.d.ts @@ -18,5 +18,9 @@ type JsonPrimitive = string | number | boolean | null; type JsonValue = JsonPrimitive | JsonObject | JsonArray; type JsonArray = JsonValue[]; type JsonObject = { [Key in string]?: JsonValue }; -export declare function beforeMount(callback: (params: { hooksConfig: JsonObject }) => Promise): void; -export declare function afterMount(callback: (params: { hooksConfig: JsonObject }) => Promise): void; +export declare function beforeMount( + callback: (params: { hooksConfig: HooksConfig }) => Promise +): void; +export declare function afterMount( + callback: (params: { hooksConfig: HooksConfig }) => Promise +): void; diff --git a/packages/playwright-ct-react/index.d.ts b/packages/playwright-ct-react/index.d.ts index 4b30d4b4c2..8f06efc53e 100644 --- a/packages/playwright-ct-react/index.d.ts +++ b/packages/playwright-ct-react/index.d.ts @@ -27,11 +27,11 @@ import type { InlineConfig } from 'vite'; export type PlaywrightTestConfig = Omit & { use?: BasePlaywrightTestConfig['use'] & { - ctPort?: number, - ctTemplateDir?: string, - ctCacheDir?: string, - ctViteConfig?: InlineConfig - } + ctPort?: number; + ctTemplateDir?: string; + ctCacheDir?: string; + ctViteConfig?: InlineConfig; + }; }; type JsonPrimitive = string | number | boolean | null; @@ -39,8 +39,8 @@ type JsonValue = JsonPrimitive | JsonObject | JsonArray; type JsonArray = JsonValue[]; type JsonObject = { [Key in string]?: JsonValue }; -export interface MountOptions { - hooksConfig?: JsonObject; +export interface MountOptions { + hooksConfig?: HooksConfig; } interface MountResult extends Locator { @@ -49,11 +49,15 @@ interface MountResult extends Locator { } export interface ComponentFixtures { - mount(component: JSX.Element, options?: MountOptions): Promise; + mount( + component: JSX.Element, + options?: MountOptions + ): Promise; } export const test: TestType< PlaywrightTestArgs & PlaywrightTestOptions & ComponentFixtures, - PlaywrightWorkerArgs & PlaywrightWorkerOptions>; + PlaywrightWorkerArgs & PlaywrightWorkerOptions +>; export { expect, devices } from '@playwright/test'; diff --git a/packages/playwright-ct-solid/hooks.d.ts b/packages/playwright-ct-solid/hooks.d.ts index 94e83bc9af..aa4680694a 100644 --- a/packages/playwright-ct-solid/hooks.d.ts +++ b/packages/playwright-ct-solid/hooks.d.ts @@ -18,5 +18,9 @@ type JsonPrimitive = string | number | boolean | null; type JsonValue = JsonPrimitive | JsonObject | JsonArray; type JsonArray = JsonValue[]; type JsonObject = { [Key in string]?: JsonValue }; -export declare function beforeMount(callback: (params: { hooksConfig: JsonObject }) => Promise): void; -export declare function afterMount(callback: (params: { hooksConfig: JsonObject }) => Promise): void; +export declare function beforeMount( + callback: (params: { hooksConfig: HooksConfig }) => Promise +): void; +export declare function afterMount( + callback: (params: { hooksConfig: HooksConfig }) => Promise +): void; diff --git a/packages/playwright-ct-solid/index.d.ts b/packages/playwright-ct-solid/index.d.ts index a260249311..2b21976334 100644 --- a/packages/playwright-ct-solid/index.d.ts +++ b/packages/playwright-ct-solid/index.d.ts @@ -27,11 +27,11 @@ import type { InlineConfig } from 'vite'; export type PlaywrightTestConfig = Omit & { use?: BasePlaywrightTestConfig['use'] & { - ctPort?: number, - ctTemplateDir?: string, - ctCacheDir?: string, - ctViteConfig?: InlineConfig - } + ctPort?: number; + ctTemplateDir?: string; + ctCacheDir?: string; + ctViteConfig?: InlineConfig; + }; }; type JsonPrimitive = string | number | boolean | null; @@ -39,8 +39,8 @@ type JsonValue = JsonPrimitive | JsonObject | JsonArray; type JsonArray = JsonValue[]; type JsonObject = { [Key in string]?: JsonValue }; -export interface MountOptions { - hooksConfig?: JsonObject; +export interface MountOptions { + hooksConfig?: HooksConfig; } interface MountResult extends Locator { @@ -48,11 +48,15 @@ interface MountResult extends Locator { } export interface ComponentFixtures { - mount(component: JSX.Element, options?: MountOptions): Promise; + mount( + component: JSX.Element, + options?: MountOptions + ): Promise; } export const test: TestType< PlaywrightTestArgs & PlaywrightTestOptions & ComponentFixtures, - PlaywrightWorkerArgs & PlaywrightWorkerOptions>; + PlaywrightWorkerArgs & PlaywrightWorkerOptions +>; export { expect, devices } from '@playwright/test'; diff --git a/packages/playwright-ct-svelte/hooks.d.ts b/packages/playwright-ct-svelte/hooks.d.ts index e6a3172d4c..8c0fa0933f 100644 --- a/packages/playwright-ct-svelte/hooks.d.ts +++ b/packages/playwright-ct-svelte/hooks.d.ts @@ -14,11 +14,18 @@ * limitations under the License. */ -import type { SvelteComponent } from "svelte"; +import type { SvelteComponent } from 'svelte'; type JsonPrimitive = string | number | boolean | null; type JsonValue = JsonPrimitive | JsonObject | JsonArray; type JsonArray = JsonValue[]; type JsonObject = { [Key in string]?: JsonValue }; -export declare function beforeMount(callback: (params: { hooksConfig: JsonObject }) => Promise): void; -export declare function afterMount(callback: (params: { hooksConfig: JsonObject, svelteComponent: SvelteComponent }) => Promise): void; +export declare function beforeMount( + callback: (params: { hooksConfig: HooksConfig }) => Promise +): void; +export declare function afterMount( + callback: (params: { + hooksConfig: HooksConfig; + svelteComponent: SvelteComponent; + }) => Promise +): void; diff --git a/packages/playwright-ct-svelte/index.d.ts b/packages/playwright-ct-svelte/index.d.ts index 31705a0997..99610f379b 100644 --- a/packages/playwright-ct-svelte/index.d.ts +++ b/packages/playwright-ct-svelte/index.d.ts @@ -24,15 +24,15 @@ import type { Locator, } from '@playwright/test'; import type { InlineConfig } from 'vite'; -import type { SvelteComponent, ComponentProps } from 'svelte/types/runtime' +import type { SvelteComponent, ComponentProps } from 'svelte/types/runtime'; export type PlaywrightTestConfig = Omit & { use?: BasePlaywrightTestConfig['use'] & { - ctPort?: number, - ctTemplateDir?: string, - ctCacheDir?: string, - ctViteConfig?: InlineConfig - } + ctPort?: number; + ctTemplateDir?: string; + ctCacheDir?: string; + ctViteConfig?: InlineConfig; + }; }; type JsonPrimitive = string | number | boolean | null; @@ -42,27 +42,36 @@ type JsonObject = { [Key in string]?: JsonValue }; type Slot = string | string[]; -export interface MountOptions { +export interface MountOptions< + HooksConfig extends JsonObject, + Component extends SvelteComponent +> { props?: ComponentProps; slots?: Record & { default?: Slot }; on?: Record; - hooksConfig?: JsonObject; + hooksConfig?: HooksConfig; } interface MountResult extends Locator { unmount(): Promise; - update(options: Omit, 'hooksConfig'|'slots'>): Promise + update( + options: Omit, 'hooksConfig' | 'slots'> + ): Promise; } interface ComponentFixtures { - mount( + mount< + HooksConfig extends JsonObject, + Component extends SvelteComponent = SvelteComponent + >( component: new (...args: any[]) => Component, - options?: MountOptions + options?: MountOptions ): Promise>; } export const test: TestType< PlaywrightTestArgs & PlaywrightTestOptions & ComponentFixtures, - PlaywrightWorkerArgs & PlaywrightWorkerOptions>; + PlaywrightWorkerArgs & PlaywrightWorkerOptions +>; export { expect, devices } from '@playwright/test'; diff --git a/packages/playwright-ct-vue/hooks.d.ts b/packages/playwright-ct-vue/hooks.d.ts index 5879f5de70..6e2c3e1715 100644 --- a/packages/playwright-ct-vue/hooks.d.ts +++ b/packages/playwright-ct-vue/hooks.d.ts @@ -14,11 +14,19 @@ * limitations under the License. */ -import { App, ComponentPublicInstance } from 'vue'; +import type { App, ComponentPublicInstance } from 'vue'; type JsonPrimitive = string | number | boolean | null; type JsonValue = JsonPrimitive | JsonObject | JsonArray; type JsonArray = JsonValue[]; type JsonObject = { [Key in string]?: JsonValue }; -export declare function beforeMount(callback: (params: { app: App, hooksConfig: JsonObject }) => Promise): void; -export declare function afterMount(callback: (params: { app: App, hooksConfig: JsonObject, instance: ComponentPublicInstance }) => Promise): void; +export declare function beforeMount( + callback: (params: { app: App; hooksConfig: HooksConfig }) => Promise +): void; +export declare function afterMount( + callback: (params: { + app: App; + hooksConfig: HooksConfig; + instance: ComponentPublicInstance; + }) => Promise +): void; diff --git a/packages/playwright-ct-vue/index.d.ts b/packages/playwright-ct-vue/index.d.ts index 657e3b1131..af90311c09 100644 --- a/packages/playwright-ct-vue/index.d.ts +++ b/packages/playwright-ct-vue/index.d.ts @@ -27,11 +27,11 @@ import type { InlineConfig } from 'vite'; export type PlaywrightTestConfig = Omit & { use?: BasePlaywrightTestConfig['use'] & { - ctPort?: number, - ctTemplateDir?: string, - ctCacheDir?: string, - ctViteConfig?: InlineConfig - } + ctPort?: number; + ctTemplateDir?: string; + ctCacheDir?: string; + ctViteConfig?: InlineConfig; + }; }; type JsonPrimitive = string | number | boolean | null; @@ -41,31 +41,46 @@ type JsonObject = { [Key in string]?: JsonValue }; type Slot = string | string[]; -export interface MountOptions> { +export interface MountOptions< +HooksConfig extends JsonObject, +Props extends Record +> { props?: Props; slots?: Record & { default?: Slot }; on?: Record; - hooksConfig?: JsonObject; + hooksConfig?: HooksConfig; } -interface MountResult> extends Locator { +interface MountResult< + Props extends Record +> extends Locator { unmount(): Promise; - update(options: Omit, 'hooksConfig'>): Promise + update(options: Omit, 'hooksConfig'>): Promise; } interface MountResultJsx extends Locator { unmount(): Promise; - update(component: JSX.Element): Promise + update(component: JSX.Element): Promise; } export interface ComponentFixtures { mount(component: JSX.Element): Promise; - mount(component: any, options?: MountOptions): Promise; - mount(component: any, options: MountOptions & { props: Props }): Promise>; + mount( + component: any, + options?: MountOptions> + ): Promise>>; + mount< + HooksConfig extends JsonObject, + Props extends Record = Record + >( + component: any, + options: MountOptions & { props: Props } + ): Promise>; } export const test: TestType< PlaywrightTestArgs & PlaywrightTestOptions & ComponentFixtures, - PlaywrightWorkerArgs & PlaywrightWorkerOptions>; + PlaywrightWorkerArgs & PlaywrightWorkerOptions +>; export { expect, devices } from '@playwright/test'; diff --git a/packages/playwright-ct-vue2/hooks.d.ts b/packages/playwright-ct-vue2/hooks.d.ts index 300ad3990c..18147cd857 100644 --- a/packages/playwright-ct-vue2/hooks.d.ts +++ b/packages/playwright-ct-vue2/hooks.d.ts @@ -20,5 +20,18 @@ type JsonPrimitive = string | number | boolean | null; type JsonValue = JsonPrimitive | JsonObject | JsonArray; type JsonArray = JsonValue[]; type JsonObject = { [Key in string]?: JsonValue }; -export declare function beforeMount(callback: (params: { hooksConfig: JsonObject }) => Promise): void; -export declare function afterMount(callback: (params: { hooksConfig: JsonObject, instance: CombinedVueInstance> }) => Promise): void; +export declare function beforeMount( + callback: (params: { hooksConfig: HooksConfig }) => Promise +): void; +export declare function afterMount( + callback: (params: { + hooksConfig: HooksConfig; + instance: CombinedVueInstance< + Vue, + object, + object, + object, + Record + >; + }) => Promise +): void; diff --git a/packages/playwright-ct-vue2/index.d.ts b/packages/playwright-ct-vue2/index.d.ts index 657e3b1131..e732fa9191 100644 --- a/packages/playwright-ct-vue2/index.d.ts +++ b/packages/playwright-ct-vue2/index.d.ts @@ -27,11 +27,11 @@ import type { InlineConfig } from 'vite'; export type PlaywrightTestConfig = Omit & { use?: BasePlaywrightTestConfig['use'] & { - ctPort?: number, - ctTemplateDir?: string, - ctCacheDir?: string, - ctViteConfig?: InlineConfig - } + ctPort?: number; + ctTemplateDir?: string; + ctCacheDir?: string; + ctViteConfig?: InlineConfig; + }; }; type JsonPrimitive = string | number | boolean | null; @@ -41,31 +41,49 @@ type JsonObject = { [Key in string]?: JsonValue }; type Slot = string | string[]; -export interface MountOptions> { +export interface MountOptions< + HooksConfig extends JsonObject, + Props extends Record +> { props?: Props; slots?: Record & { default?: Slot }; on?: Record; hooksConfig?: JsonObject; } -interface MountResult> extends Locator { +interface MountResult< + HooksConfig extends JsonObject, + Props extends Record +> extends Locator { unmount(): Promise; - update(options: Omit, 'hooksConfig'>): Promise + update( + options: Omit, 'hooksConfig'> + ): Promise; } interface MountResultJsx extends Locator { unmount(): Promise; - update(component: JSX.Element): Promise + update(component: JSX.Element): Promise; } export interface ComponentFixtures { mount(component: JSX.Element): Promise; - mount(component: any, options?: MountOptions): Promise; - mount(component: any, options: MountOptions & { props: Props }): Promise>; + mount( + component: any, + options?: MountOptions + ): Promise>; + mount< + HooksConfig extends JsonObject, + Props extends Record = Record + >( + component: any, + options: MountOptions & { props: Props } + ): Promise>; } export const test: TestType< PlaywrightTestArgs & PlaywrightTestOptions & ComponentFixtures, - PlaywrightWorkerArgs & PlaywrightWorkerOptions>; + PlaywrightWorkerArgs & PlaywrightWorkerOptions +>; export { expect, devices } from '@playwright/test'; diff --git a/tests/components/ct-react-vite/playwright/index.ts b/tests/components/ct-react-vite/playwright/index.ts index 267e6fd64f..956fc9625f 100644 --- a/tests/components/ct-react-vite/playwright/index.ts +++ b/tests/components/ct-react-vite/playwright/index.ts @@ -2,10 +2,14 @@ import '../src/assets/index.css'; import { beforeMount, afterMount } from '@playwright/experimental-ct-react/hooks'; -beforeMount(async ({ hooksConfig }) => { +export type HooksConfig = { + route: string; +} + +beforeMount(async ({ hooksConfig }) => { console.log(`Before mount: ${JSON.stringify(hooksConfig)}`); }); -afterMount(async ({}) => { +afterMount(async () => { console.log(`After mount`); }); diff --git a/tests/components/ct-react-vite/src/tests.spec.tsx b/tests/components/ct-react-vite/src/tests.spec.tsx index 987dcf5ed1..a639055656 100644 --- a/tests/components/ct-react-vite/src/tests.spec.tsx +++ b/tests/components/ct-react-vite/src/tests.spec.tsx @@ -5,6 +5,7 @@ import MultipleChildren from './components/MultipleChildren'; import MultiRoot from './components/MultiRoot'; import Counter from './components/Counter'; import EmptyFragment from './components/EmptyFragment'; +import type { HooksConfig } from '../playwright'; test.use({ viewport: { width: 500, height: 500 } }); @@ -101,7 +102,7 @@ test('execute callback when a child node is clicked', async ({ mount }) => { test('run hooks', async ({ page, mount }) => { const messages: string[] = []; page.on('console', m => messages.push(m.text())); - await mount(