Skip to content

Commit

Permalink
cherry-pick(#27117): fix: custom expect matchers on Locator/Page/APIR…
Browse files Browse the repository at this point in the history
…esponse instance (#27125)

This PR cherry-picks the following commits:

- 0d44405

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
playwrightmachine and github-actions[bot] authored Sep 15, 2023
1 parent 2a577a5 commit 6cc43d8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
8 changes: 4 additions & 4 deletions packages/playwright/types/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5072,12 +5072,12 @@ type FunctionAssertions = {
};

type BaseMatchers<R, T> = GenericAssertions<R> & PlaywrightTest.Matchers<R, T> & SnapshotAssertions;
type AllowedGenericMatchers<R> = Pick<GenericAssertions<R>, 'toBe' | 'toBeDefined' | 'toBeFalsy' | 'toBeNull' | 'toBeTruthy' | 'toBeUndefined'>;
type AllowedGenericMatchers<R, T> = PlaywrightTest.Matchers<R, T> & Pick<GenericAssertions<R>, 'toBe' | 'toBeDefined' | 'toBeFalsy' | 'toBeNull' | 'toBeTruthy' | 'toBeUndefined'>;

type SpecificMatchers<R, T> =
T extends Page ? PageAssertions & AllowedGenericMatchers<R> :
T extends Locator ? LocatorAssertions & AllowedGenericMatchers<R> :
T extends APIResponse ? APIResponseAssertions & AllowedGenericMatchers<R> :
T extends Page ? PageAssertions & AllowedGenericMatchers<R, T> :
T extends Locator ? LocatorAssertions & AllowedGenericMatchers<R, T> :
T extends APIResponse ? APIResponseAssertions & AllowedGenericMatchers<R, T> :
BaseMatchers<R, T> & (T extends Function ? FunctionAssertions : {});
type AllMatchers<R, T> = PageAssertions & LocatorAssertions & APIResponseAssertions & FunctionAssertions & BaseMatchers<R, T>;

Expand Down
12 changes: 11 additions & 1 deletion tests/playwright-test/expect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,21 @@ test('should work with custom PlaywrightTest namespace', async ({ runTSC }) => {
}
`,
'a.spec.ts': `
import { test, expect } from '@playwright/test';
import { test, expect, type Page, type APIResponse } from '@playwright/test';
test.expect.extend({
toBeWithinRange() { },
});
const page = {} as Page;
const locator = page.locator('');
const apiResponse = {} as APIResponse;
test.expect(page).toBeEmpty();
test.expect(page).not.toBeEmpty();
test.expect(locator).toBeEmpty();
test.expect(locator).not.toBeEmpty();
test.expect(apiResponse).toBeEmpty();
test.expect(apiResponse).not.toBeEmpty();
test.expect('').toBeEmpty();
test.expect('hello').not.toBeEmpty();
test.expect([]).toBeEmpty();
Expand Down
8 changes: 4 additions & 4 deletions utils/generate_types/overrides-test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,12 @@ type FunctionAssertions = {
};

type BaseMatchers<R, T> = GenericAssertions<R> & PlaywrightTest.Matchers<R, T> & SnapshotAssertions;
type AllowedGenericMatchers<R> = Pick<GenericAssertions<R>, 'toBe' | 'toBeDefined' | 'toBeFalsy' | 'toBeNull' | 'toBeTruthy' | 'toBeUndefined'>;
type AllowedGenericMatchers<R, T> = PlaywrightTest.Matchers<R, T> & Pick<GenericAssertions<R>, 'toBe' | 'toBeDefined' | 'toBeFalsy' | 'toBeNull' | 'toBeTruthy' | 'toBeUndefined'>;

type SpecificMatchers<R, T> =
T extends Page ? PageAssertions & AllowedGenericMatchers<R> :
T extends Locator ? LocatorAssertions & AllowedGenericMatchers<R> :
T extends APIResponse ? APIResponseAssertions & AllowedGenericMatchers<R> :
T extends Page ? PageAssertions & AllowedGenericMatchers<R, T> :
T extends Locator ? LocatorAssertions & AllowedGenericMatchers<R, T> :
T extends APIResponse ? APIResponseAssertions & AllowedGenericMatchers<R, T> :
BaseMatchers<R, T> & (T extends Function ? FunctionAssertions : {});
type AllMatchers<R, T> = PageAssertions & LocatorAssertions & APIResponseAssertions & FunctionAssertions & BaseMatchers<R, T>;

Expand Down

0 comments on commit 6cc43d8

Please sign in to comment.