chore: add generic overloaded toStrictEqual

Refs: #32986
This commit is contained in:
Rui Figueira 2024-10-10 01:56:41 +01:00
parent 8ad47b3b17
commit 4b5435e8e2
3 changed files with 63 additions and 2 deletions

View file

@ -31,8 +31,8 @@ export type ReporterDescription = Readonly<
[string] | [string, any] [string] | [string, any]
>; >;
type PartialDeep<T> = { type DeepPartial<T> = {
[P in keyof T]?: PartialDeep<T[P]> [P in keyof T]?: DeepPartial<T[P]>
} }
type UseOptions<TestArgs, WorkerArgs> = Partial<WorkerArgs> & Partial<TestArgs>; type UseOptions<TestArgs, WorkerArgs> = Partial<WorkerArgs> & Partial<TestArgs>;
@ -6479,6 +6479,28 @@ interface GenericAssertions<R> {
* @param expected The expected object value to match against. * @param expected The expected object value to match against.
*/ */
toMatchObject(expected: Record<string, unknown> | Array<unknown>): R; toMatchObject(expected: Record<string, unknown> | Array<unknown>): R;
/**
* Compares contents of the value with contents of
* [`expected`](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-to-strict-equal-option-expected)
* **and** their types.
*
* Differences from
* [expect(value).toEqual(expected)](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-to-equal):
* - Keys with undefined properties are checked. For example, `{ a: undefined, b: 2 }` does not match `{ b: 2 }`.
* - Array sparseness is checked. For example, `[, 1]` does not match `[undefined, 1]`.
* - Object types are checked to be equal. For example, a class instance with fields `a` and `b` will not equal a
* literal object with fields `a` and `b`.
*
* **Usage**
*
* ```js
* const value = { prop: 1 };
* expect(value).toStrictEqual({ prop: 1 });
* ```
*
* @param expected Expected value.
*/
toStrictEqual<K>(expected: K): R;
/** /**
* Compares contents of the value with contents of * Compares contents of the value with contents of
* [`expected`](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-to-strict-equal-option-expected) * [`expected`](https://playwright.dev/docs/api/class-genericassertions#generic-assertions-to-strict-equal-option-expected)

View file

@ -360,3 +360,41 @@ test.fixme('should check types of overloaded toMatchObject with generic paramete
}); });
expect(result.exitCode).toBe(0); expect(result.exitCode).toBe(0);
}); });
test.fixme('should check types of overloaded toStrictEqual 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).toStrictEqual<typeof value>({ a: 1, b: 'foo', c: { d: 2, e: 'bar' } });
expect.soft([value]).toStrictEqual<Array<typeof value>>([{ a: 1, b: 'foo', c: { d: 2, e: 'bar' } }]);
expect.soft(new Date()).toStrictEqual<Date>(new Date());
// @ts-expect-error
expect.soft(value).toStrictEqual<typeof value>({ a: 1 });
// @ts-expect-error
expect.soft(value).toStrictEqual<typeof value>({ a: 'a' });
// @ts-expect-error
expect.soft(value).toStrictEqual<typeof value>({ c: { e: 2 } });
// @ts-expect-error
expect.soft([value]).toStrictEqual<typeof value>([{ a: 'a' }]);
// @ts-expect-error
expect.soft([value]).toStrictEqual<typeof value>([{ c: { e: 2 } }]);
});
`
});
expect(result.exitCode).toBe(0);
});

View file

@ -295,6 +295,7 @@ interface GenericAssertions<R> {
toMatch(expected: RegExp | string): R; toMatch(expected: RegExp | string): R;
toMatchObject<K extends Record<string, unknown>>(expected: DeepPartial<K> | Array<DeepPartial<K>>): R; toMatchObject<K extends Record<string, unknown>>(expected: DeepPartial<K> | Array<DeepPartial<K>>): R;
toMatchObject(expected: Record<string, unknown> | Array<unknown>): R; toMatchObject(expected: Record<string, unknown> | Array<unknown>): R;
toStrictEqual<K>(expected: K): R;
toStrictEqual(expected: unknown): R; toStrictEqual(expected: unknown): R;
toThrow(error?: unknown): R; toThrow(error?: unknown): R;
toThrowError(error?: unknown): R; toThrowError(error?: unknown): R;