Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] improve type safety hooksConfig #19933

Open
sand4rt opened this issue Jan 7, 2023 · 3 comments
Open

[Feature] improve type safety hooksConfig #19933

sand4rt opened this issue Jan 7, 2023 · 3 comments

Comments

@sand4rt
Copy link
Collaborator

sand4rt commented Jan 7, 2023

The idea comes from @tannerlinsley: https://youtu.be/DXbfDKoMzWY?t=1467.

// ./playwright/index.tsx
import { beforeMount } from '@playwright/experimental-ct-solid/hooks';

// Override HooksConfig so there is no need to specify it as a `mount<HooksConfig>` generic
declare module '@playwright/experimental-ct-solid/hooks' { 
  interface RegisterHooksConfig {
    route: string;
  }
}

beforeMount(async ({ hooksConfig }) => {
  hooksConfig.DOES_NOT_EXSISTS; // throws an error without specifying the generic
});
// App.test.tsx
import { expect, test } from "@playwright/experimental-ct-solid";
import { App } from "./app";

test('type check', async ({ mount }) => {
  const component = await mount(<App />, { 
    hooksConfig: { route: '' } // is typesafe without specifying the `mount<HooksConfig>` generic
  });

  await expect(component.getByText('label')).toBeVisible();
});

related: #18616

@tannerlinsley
Copy link

Booyah!

@segevfiner
Copy link

I have hit another issue. HooksConfig is defined to extends JsonObject in the signature of mount which prevents using it with other more restricted types:

Type 'HooksConfig' does not satisfy the constraint 'JsonObject'.
  Index signature for type 'string' is missing in type 'HooksConfig'.ts(2344)

@segevfiner
Copy link

OK this might be due to older Playwright version though. Couldn't upgrade yet due to #23505

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants