fix: custom expect matchers on Locator/Page/APIResponse instance (#27117)

Fixes https://github.com/microsoft/playwright/issues/27113
This commit is contained in:
Max Schmitt 2023-09-15 18:05:44 +02:00 коммит произвёл GitHub
Родитель ebf6a08290
Коммит 0d44405762
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 19 добавлений и 9 удалений

8
packages/playwright/types/test.d.ts поставляемый
Просмотреть файл

@ -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>;

Просмотреть файл

@ -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();

8
utils/generate_types/overrides-test.d.ts поставляемый
Просмотреть файл

@ -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>;