From 6cc43d81c567f57a58ac3240fdf6797e66a2e119 Mon Sep 17 00:00:00 2001 From: Playwright Service <89237858+playwrightmachine@users.noreply.github.com> Date: Fri, 15 Sep 2023 10:47:28 -0700 Subject: [PATCH] cherry-pick(#27117): fix: custom expect matchers on Locator/Page/APIResponse instance (#27125) This PR cherry-picks the following commits: - 0d444057624b9970e0b5d4bdb302cd8efae39edb Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> --- packages/playwright/types/test.d.ts | 8 ++++---- tests/playwright-test/expect.spec.ts | 12 +++++++++++- utils/generate_types/overrides-test.d.ts | 8 ++++---- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/packages/playwright/types/test.d.ts b/packages/playwright/types/test.d.ts index 6562459a41b91..94d3117400a00 100644 --- a/packages/playwright/types/test.d.ts +++ b/packages/playwright/types/test.d.ts @@ -5072,12 +5072,12 @@ type FunctionAssertions = { }; type BaseMatchers = GenericAssertions & PlaywrightTest.Matchers & SnapshotAssertions; -type AllowedGenericMatchers = Pick, 'toBe' | 'toBeDefined' | 'toBeFalsy' | 'toBeNull' | 'toBeTruthy' | 'toBeUndefined'>; +type AllowedGenericMatchers = PlaywrightTest.Matchers & Pick, 'toBe' | 'toBeDefined' | 'toBeFalsy' | 'toBeNull' | 'toBeTruthy' | 'toBeUndefined'>; type SpecificMatchers = - T extends Page ? PageAssertions & AllowedGenericMatchers : - T extends Locator ? LocatorAssertions & AllowedGenericMatchers : - T extends APIResponse ? APIResponseAssertions & AllowedGenericMatchers : + T extends Page ? PageAssertions & AllowedGenericMatchers : + T extends Locator ? LocatorAssertions & AllowedGenericMatchers : + T extends APIResponse ? APIResponseAssertions & AllowedGenericMatchers : BaseMatchers & (T extends Function ? FunctionAssertions : {}); type AllMatchers = PageAssertions & LocatorAssertions & APIResponseAssertions & FunctionAssertions & BaseMatchers; diff --git a/tests/playwright-test/expect.spec.ts b/tests/playwright-test/expect.spec.ts index 2afffb1ac240a..04f69286354fd 100644 --- a/tests/playwright-test/expect.spec.ts +++ b/tests/playwright-test/expect.spec.ts @@ -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(); diff --git a/utils/generate_types/overrides-test.d.ts b/utils/generate_types/overrides-test.d.ts index 3a1f578fa133e..dc08d533a55b2 100644 --- a/utils/generate_types/overrides-test.d.ts +++ b/utils/generate_types/overrides-test.d.ts @@ -330,12 +330,12 @@ type FunctionAssertions = { }; type BaseMatchers = GenericAssertions & PlaywrightTest.Matchers & SnapshotAssertions; -type AllowedGenericMatchers = Pick, 'toBe' | 'toBeDefined' | 'toBeFalsy' | 'toBeNull' | 'toBeTruthy' | 'toBeUndefined'>; +type AllowedGenericMatchers = PlaywrightTest.Matchers & Pick, 'toBe' | 'toBeDefined' | 'toBeFalsy' | 'toBeNull' | 'toBeTruthy' | 'toBeUndefined'>; type SpecificMatchers = - T extends Page ? PageAssertions & AllowedGenericMatchers : - T extends Locator ? LocatorAssertions & AllowedGenericMatchers : - T extends APIResponse ? APIResponseAssertions & AllowedGenericMatchers : + T extends Page ? PageAssertions & AllowedGenericMatchers : + T extends Locator ? LocatorAssertions & AllowedGenericMatchers : + T extends APIResponse ? APIResponseAssertions & AllowedGenericMatchers : BaseMatchers & (T extends Function ? FunctionAssertions : {}); type AllMatchers = PageAssertions & LocatorAssertions & APIResponseAssertions & FunctionAssertions & BaseMatchers;