Skip to content

Commit 6cc43d8

Browse files
cherry-pick(#27117): fix: custom expect matchers on Locator/Page/APIResponse instance (#27125)
This PR cherry-picks the following commits: - 0d44405 Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 2a577a5 commit 6cc43d8

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

packages/playwright/types/test.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5072,12 +5072,12 @@ type FunctionAssertions = {
50725072
};
50735073

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

50775077
type SpecificMatchers<R, T> =
5078-
T extends Page ? PageAssertions & AllowedGenericMatchers<R> :
5079-
T extends Locator ? LocatorAssertions & AllowedGenericMatchers<R> :
5080-
T extends APIResponse ? APIResponseAssertions & AllowedGenericMatchers<R> :
5078+
T extends Page ? PageAssertions & AllowedGenericMatchers<R, T> :
5079+
T extends Locator ? LocatorAssertions & AllowedGenericMatchers<R, T> :
5080+
T extends APIResponse ? APIResponseAssertions & AllowedGenericMatchers<R, T> :
50815081
BaseMatchers<R, T> & (T extends Function ? FunctionAssertions : {});
50825082
type AllMatchers<R, T> = PageAssertions & LocatorAssertions & APIResponseAssertions & FunctionAssertions & BaseMatchers<R, T>;
50835083

tests/playwright-test/expect.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,21 @@ test('should work with custom PlaywrightTest namespace', async ({ runTSC }) => {
272272
}
273273
`,
274274
'a.spec.ts': `
275-
import { test, expect } from '@playwright/test';
275+
import { test, expect, type Page, type APIResponse } from '@playwright/test';
276276
test.expect.extend({
277277
toBeWithinRange() { },
278278
});
279279
280+
const page = {} as Page;
281+
const locator = page.locator('');
282+
const apiResponse = {} as APIResponse;
283+
test.expect(page).toBeEmpty();
284+
test.expect(page).not.toBeEmpty();
285+
test.expect(locator).toBeEmpty();
286+
test.expect(locator).not.toBeEmpty();
287+
test.expect(apiResponse).toBeEmpty();
288+
test.expect(apiResponse).not.toBeEmpty();
289+
280290
test.expect('').toBeEmpty();
281291
test.expect('hello').not.toBeEmpty();
282292
test.expect([]).toBeEmpty();

utils/generate_types/overrides-test.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,12 +330,12 @@ type FunctionAssertions = {
330330
};
331331

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

335335
type SpecificMatchers<R, T> =
336-
T extends Page ? PageAssertions & AllowedGenericMatchers<R> :
337-
T extends Locator ? LocatorAssertions & AllowedGenericMatchers<R> :
338-
T extends APIResponse ? APIResponseAssertions & AllowedGenericMatchers<R> :
336+
T extends Page ? PageAssertions & AllowedGenericMatchers<R, T> :
337+
T extends Locator ? LocatorAssertions & AllowedGenericMatchers<R, T> :
338+
T extends APIResponse ? APIResponseAssertions & AllowedGenericMatchers<R, T> :
339339
BaseMatchers<R, T> & (T extends Function ? FunctionAssertions : {});
340340
type AllMatchers<R, T> = PageAssertions & LocatorAssertions & APIResponseAssertions & FunctionAssertions & BaseMatchers<R, T>;
341341

0 commit comments

Comments
 (0)