Skip to content

Commit

Permalink
Add that toMatchObject can match arrays (#3994)
Browse files Browse the repository at this point in the history
  • Loading branch information
dandv authored and cpojer committed Jul 10, 2017
1 parent 487ab1d commit 9a1ffbb
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion docs/en/ExpectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,11 @@ describe('grapefruits are healthy', () => {

### `.toMatchObject(object)`

Use `.toMatchObject` to check that a JavaScript object matches a subset of the properties of an object. You can match properties against values or against matchers.
Use `.toMatchObject` to check that a JavaScript object matches a subset of the properties of an object. It will match received objects with properties that are **not** in the expected object.

You can also pass an array of objects, in which case the method will return true only if each object in the received array matches (in the `toMatchObject` sense described above) the corresponding object in the expected array. This is useful if you want to check that two arrays match in their number of elements, as opposed to `arrayContaining`, which allows for extra elements in the received array.

You can match properties against values or against matchers.

```js
const houseForSale = {
Expand All @@ -732,6 +736,40 @@ test('the house has my desired features', () => {
});
```

```js
describe('toMatchObject applied to arrays arrays', () => {
test('the number of elements must match exactly', () => {
expect([
{ foo: 'bar' },
{ baz: 1 }
]).toMatchObject([
{ foo: 'bar' },
{ baz: 1 }
]);
});

// .arrayContaining "matches a received array which contains elements that are *not* in the expected array"
test('.toMatchObject does not allow extra elements', () => {
expect([
{ foo: 'bar' },
{ baz: 1 }
]).toMatchObject([
{ foo: 'bar' }
]);
});

test('.toMatchObject is called for each elements, so extra object properties are okay', () => {
expect([
{ foo: 'bar' },
{ baz: 1, extra: 'quux' }
]).toMatchObject([
{ foo: 'bar' },
{ baz: 1 }
]);
});
});
```

### `.toHaveProperty(keyPath, value)`

Use `.toHaveProperty` to check if property at provided reference `keyPath` exists for an object.
Expand Down

0 comments on commit 9a1ffbb

Please sign in to comment.