chore: add generic overloaded toMatchObject

Refs: #32986
This commit is contained in:
Rui Figueira 2024-10-10 01:45:59 +01:00
parent bc30cc795e
commit 8ad47b3b17
3 changed files with 71 additions and 0 deletions

View file

@ -31,6 +31,10 @@ export type ReporterDescription = Readonly<
[string] | [string, any]
>;
type PartialDeep<T> = {
[P in keyof T]?: PartialDeep<T[P]>
}
type UseOptions<TestArgs, WorkerArgs> = Partial<WorkerArgs> & Partial<TestArgs>;
/**
@ -6423,6 +6427,32 @@ interface GenericAssertions<R> {
* @param expected Regular expression to match against.
*/
toMatch(expected: RegExp | string): R;
/**
* Compares contents of the value with contents of
* [`expected`](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-to-match-object-option-expected),
* performing "deep equality" check. Allows extra properties to be present in the value, unlike
* [expect(value).toEqual(expected)](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-to-equal),
* so you can check just a subset of object properties.
*
* When comparing arrays, the number of items must match, and each item is checked recursively.
*
* **Usage**
*
* ```js
* const value = {
* a: 1,
* b: 2,
* c: true,
* };
* expect(value).toMatchObject({ a: 1, c: true });
* expect(value).toMatchObject({ b: 2, c: true });
*
* expect([{ a: 1, b: 2 }]).toMatchObject([{ a: 1 }]);
* ```
*
* @param expected The expected object value to match against.
*/
toMatchObject<K extends Record<string, unknown>>(expected: DeepPartial<K> | Array<DeepPartial<K>>): R;
/**
* Compares contents of the value with contents of
* [`expected`](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-to-match-object-option-expected),

View file

@ -324,3 +324,39 @@ test('config should allow void/empty options', async ({ runTSC }) => {
});
expect(result.exitCode).toBe(0);
});
test.fixme('should check types of overloaded toMatchObject with generic parameter', async ({ runTSC }) => {
const result = await runTSC({
'playwright.config.ts': `
import { defineConfig } from '@playwright/test';
export default defineConfig({
});
`,
'a.spec.ts': `
import { test, expect } from '@playwright/test';
test('my test', async () => {
const value = { a: 1, b: 'foo', c: { d: 2, e: 'bar' } };
expect.soft(value).toMatchObject<typeof value>({});
expect.soft(value).toMatchObject<typeof value>({ a: 1 });
expect.soft(value).toMatchObject<typeof value>({ a: 1, c: { e: 'bar' } });
expect.soft([value]).toMatchObject<typeof value>([{ a: 1 }]);
expect.soft([value]).toMatchObject<typeof value>([{ a: 1, c: { e: 'bar' } }]);
// @ts-expect-error
expect.soft(value).toMatchObject<typeof value>({ a: 'a' });
// @ts-expect-error
expect.soft(value).toMatchObject<typeof value>({ c: { e: 2 } });
// @ts-expect-error
expect.soft([value]).toMatchObject<typeof value>([{ a: 'a' }]);
// @ts-expect-error
expect.soft([value]).toMatchObject<typeof value>([{ c: { e: 2 } }]);
});
`
});
expect(result.exitCode).toBe(0);
});

View file

@ -30,6 +30,10 @@ export type ReporterDescription = Readonly<
[string] | [string, any]
>;
type DeepPartial<T> = {
[P in keyof T]?: DeepPartial<T[P]>
}
type UseOptions<TestArgs, WorkerArgs> = Partial<WorkerArgs> & Partial<TestArgs>;
interface TestProject<TestArgs = {}, WorkerArgs = {}> {
@ -289,6 +293,7 @@ interface GenericAssertions<R> {
toHaveLength(expected: number): R;
toHaveProperty(keyPath: string | Array<string>, value?: unknown): R;
toMatch(expected: RegExp | string): R;
toMatchObject<K extends Record<string, unknown>>(expected: DeepPartial<K> | Array<DeepPartial<K>>): R;
toMatchObject(expected: Record<string, unknown> | Array<unknown>): R;
toStrictEqual(expected: unknown): R;
toThrow(error?: unknown): R;