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

feat(extend): custom asymmetric matchers #32740

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/playwright/types/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6634,6 +6634,11 @@ type PollMatchers<R, T, ExtendedMatchers> = {
not: PollMatchers<R, T, ExtendedMatchers>;
} & BaseMatchers<R, T> & ToUserMatcherObject<ExtendedMatchers, T>;

type ExtendedAsymmetricMatchers<M = {}> = {
[K in keyof M]: M[K] extends (this: ExpectMatcherState, received: any, ...args: infer P) => any ? (...args: P) => AsymmetricMatcher
: never
}

export type Expect<ExtendedMatchers = {}> = {
<T = unknown>(actual: T, messageOrOptions?: string | { message?: string }): MakeMatchers<void, T, ExtendedMatchers>;
soft: <T = unknown>(actual: T, messageOrOptions?: string | { message?: string }) => MakeMatchers<void, T, ExtendedMatchers>;
Expand All @@ -6645,8 +6650,8 @@ export type Expect<ExtendedMatchers = {}> = {
soft?: boolean,
}) => Expect<ExtendedMatchers>;
getState(): unknown;
not: Omit<AsymmetricMatchers, 'any' | 'anything'>;
} & AsymmetricMatchers;
not: Omit<AsymmetricMatchers & ExtendedAsymmetricMatchers<ExtendedMatchers>, 'any' | 'anything'>;
} & AsymmetricMatchers & ExtendedAsymmetricMatchers<ExtendedMatchers>;

// --- BEGINGLOBAL ---
declare global {
Expand Down
28 changes: 28 additions & 0 deletions tests/playwright-test/expect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -875,12 +875,21 @@ test('should support mergeExpects (TSC)', async ({ runTSC }) => {
const expect1 = baseExpect.extend({
async toBeAGoodPage(page: Page, x: number) {
return { pass: true, message: () => '' };
},
matchFoo(received: unknown, expected: string) {
return { pass: true, message: () => '' };
},
toBeFoo(received: unknown) {
return { pass: true, message: () => '' };
}
});

const expect2 = baseExpect.extend({
async toBeABadPage(page: Page, y: string) {
return { pass: true, message: () => '' };
},
matchBar(received: unknown, expected: string) {
return { pass: true, message: () => '' };
}
});

Expand All @@ -889,12 +898,19 @@ test('should support mergeExpects (TSC)', async ({ runTSC }) => {
test('custom matchers', async ({ page }) => {
await expect(page).toBeAGoodPage(123);
await expect(page).toBeABadPage('123');
await expect('foo').toEqual(expect.matchFoo('123'));
await expect('bar').not.toEqual(expect.not.matchBar('123'));
await expect({ name: 'Foo' }).toMatchObject({ name: expect.toBeFoo() });
// @ts-expect-error
await expect(page).toBeAMediocrePage();
// @ts-expect-error
await expect(page).toBeABadPage(123);
// @ts-expect-error
await expect(page).toBeAGoodPage('123');
// @ts-expect-error
expect('foo').toEqual(expect.matchFoo(123));
// @ts-expect-error
expect('bar').not.toEqual(expect.not.matchBar(123));
});
`
});
Expand All @@ -910,12 +926,21 @@ test('should support mergeExpects', async ({ runInlineTest }) => {
const expect1 = baseExpect.extend({
async toBeAGoodPage(page: Page, x: number) {
return { pass: true, message: () => '' };
},
matchFoo(received: unknown, expected: string) {
return { pass: true, message: () => '' };
},
toBeFoo(received: unknown) {
return { pass: true, message: () => '' };
}
});

const expect2 = baseExpect.extend({
async toBeABadPage(page: Page, y: string) {
return { pass: true, message: () => '' };
},
matchBar(received: unknown, expected: string) {
return { pass: true, message: () => '' };
}
});

Expand All @@ -924,6 +949,9 @@ test('should support mergeExpects', async ({ runInlineTest }) => {
test('custom matchers', async ({ page }) => {
await expect(page).toBeAGoodPage(123);
await expect(page).toBeABadPage('123');
await expect('foo').toEqual(expect.matchFoo('123'));
await expect('bar').not.toEqual(expect.not.matchBar('123'));
await expect({ name: 'Foo' }).toMatchObject({ name: expect.toBeFoo() });
});
`
}, { workers: 1 });
Expand Down
9 changes: 7 additions & 2 deletions utils/generate_types/overrides-test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,11 @@ type PollMatchers<R, T, ExtendedMatchers> = {
not: PollMatchers<R, T, ExtendedMatchers>;
} & BaseMatchers<R, T> & ToUserMatcherObject<ExtendedMatchers, T>;

type ExtendedAsymmetricMatchers<M = {}> = {
[K in keyof M]: M[K] extends (this: ExpectMatcherState, received: any, ...args: infer P) => any ? (...args: P) => AsymmetricMatcher
: never
}

export type Expect<ExtendedMatchers = {}> = {
<T = unknown>(actual: T, messageOrOptions?: string | { message?: string }): MakeMatchers<void, T, ExtendedMatchers>;
soft: <T = unknown>(actual: T, messageOrOptions?: string | { message?: string }) => MakeMatchers<void, T, ExtendedMatchers>;
Expand All @@ -422,8 +427,8 @@ export type Expect<ExtendedMatchers = {}> = {
soft?: boolean,
}) => Expect<ExtendedMatchers>;
getState(): unknown;
not: Omit<AsymmetricMatchers, 'any' | 'anything'>;
} & AsymmetricMatchers;
not: Omit<AsymmetricMatchers & ExtendedAsymmetricMatchers<ExtendedMatchers>, 'any' | 'anything'>;
} & AsymmetricMatchers & ExtendedAsymmetricMatchers<ExtendedMatchers>;

// --- BEGINGLOBAL ---
declare global {
Expand Down