Jest: Added Generic for "toMatchObject" function (#35033)

* Add generic to toMatchObject

* Cover feedback from @wsmd
This commit is contained in:
Jeroen Claassens (Favna)
2019-04-28 23:17:18 -07:00
committed by Wesley Wigham
parent 6d0dc421d7
commit 53230a10b3
2 changed files with 21 additions and 1 deletions
+19 -1
View File
@@ -18,6 +18,7 @@
// Sebastian Sebald <https://github.com/sebald>
// Andy <https://github.com/andys8>
// Antoine Brault <https://github.com/antoinebrault>
// Jeroen Claassens <https://github.com/favna>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.0
@@ -757,8 +758,25 @@ declare namespace jest {
toMatch(expected: string | RegExp): R;
/**
* Used to check that a JavaScript object matches a subset of the properties of an object
*
* Optionally, you can provide an object to use as Generic type for the expected value.
* This ensures that the matching object matches the structure of the provided object-like type.
*
* @example
*
* type House = {
* bath: boolean;
* bedrooms: number;
* kitchen: {
* amenities: string[];
* area: number;
* wallColor: string;
* }
* };
*
* expect(desiredHouse).toMatchObject<House>(...standardHouse, kitchen: {area: 20}) // wherein standardHouse is some base object of type House
*/
toMatchObject(expected: {} | any[]): R;
toMatchObject<E extends {} | any[]>(expected: E): R;
/**
* This ensures that a value matches the most recent snapshot with property matchers.
* Check out [the Snapshot Testing guide](http://facebook.github.io/jest/docs/snapshot-testing.html) for more information.
+2
View File
@@ -830,6 +830,8 @@ describe("", () => {
expect({ abc: "def" }).toMatchObject({ abc: "def" });
expect({}).toMatchObject([{}, {}]);
expect({ abc: "def" }).toMatchObject([{ abc: "def" }, { invalid: "property" }]);
expect({abc: "def"}).toMatchObject<{abc: string}>({abc: "def"});
expect([{abc: 'def'}, {abc: 'def'}]).toMatchObject<[{abc: string}, {abc: string}]>([{abc: 'def'}, {abc: 'def'}]);
expect({}).toMatchSnapshot();
expect({}).toMatchSnapshot("snapshotName");