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]: Is there a reason not to expose MatcherReturnType? #30131

Closed
silversonicaxel opened this issue Mar 26, 2024 · 4 comments · Fixed by #30139
Closed

[Feature]: Is there a reason not to expose MatcherReturnType? #30131

silversonicaxel opened this issue Mar 26, 2024 · 4 comments · Fixed by #30139

Comments

@silversonicaxel
Copy link

silversonicaxel commented Mar 26, 2024

🚀 Feature Request

Can this type definition here

type MatcherReturnType = {
message: () => string;
pass: boolean;
name?: string;
expected?: unknown;
actual?: any;
log?: string[];
};
be exported?

type MatcherReturnType = {
  message: () => string;
  pass: boolean;
  name?: string;
  expected?: unknown;
  actual?: any;
  log?: string[];
};

Example

export type MatcherReturnType = {
  message: () => string;
  pass: boolean;
  name?: string;
  expected?: unknown;
  actual?: any;
  log?: string[];
};

Motivation

It can be useful in case of expect extensions. Currently we need to define this type internally.

@pavelfeldman
Copy link
Member

pavelfeldman commented Mar 26, 2024

Could you share your use case? We rather seek for the answer to "is there a reason to expose". I.e. by default we'd like to expose nothing, unless necessary.

@silversonicaxel
Copy link
Author

Imagine the example in this page https://playwright.dev/docs/test-assertions#add-custom-matchers-using-expectextend.

I can copy here a smaller version of this case example, to show the need of matcher return type, from Playwright exported type definition:

export const expect = baseExpect.extend({
  async toHaveAmount(locator: Locator, expected: number): Promise<MatcherReturnType> {
    let pass: boolean;
    try {
      await baseExpect(locator).toHaveAttribute('data-amount', String(expected));
      pass = true;
    } catch (e: any) {
      pass = false;
    }
    const message = () => 'Whatever error is ..';

    return {
      message,
      pass,
      name: 'toHaveAmount',
      expected,
    };
  },
});

It would be nice to import MatcherReturnType instead of defining it myself.

@silversonicaxel
Copy link
Author

Thanks

@HardworkingSlacker
Copy link

HardworkingSlacker commented Apr 19, 2024

If you need this type in an older version of Playwright, this construction should provide you with the correct type:

type MatcherReturnType = Awaited<Parameters<Expect["extend"]> extends [matchers: Record<string, (...args: any[]) => infer MRT>] ? MRT : any>;

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

Successfully merging a pull request may close this issue.

3 participants