Skip to content

Commit

Permalink
fix: toMatchObject throws TypeError when a source property is null (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
WillBrock authored and thymikee committed May 27, 2018
1 parent 266e7dc commit d59017d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## master

### Fixes

* `[expect]` toMatchObject throws TypeError when a source property is null ([#6313](https://github.com/facebook/jest/pull/6313))

## 23.0.1

### Chore & Maintenance
Expand Down
30 changes: 29 additions & 1 deletion packages/expect/src/__tests__/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
'use strict';

const {stringify} = require('jest-matcher-utils');
const {emptyObject, getObjectSubset, getPath} = require('../utils');
const {
emptyObject,
getObjectSubset,
getPath,
subsetEquality,
} = require('../utils');

describe('getPath()', () => {
test('property exists', () => {
Expand Down Expand Up @@ -122,3 +127,26 @@ describe('emptyObject()', () => {
expect(emptyObject(34)).toBe(false);
});
});

describe('subsetEquality()', () => {
test('matching object returns true', () => {
expect(subsetEquality({foo: 'bar'}, {foo: 'bar'})).toBe(true);
});

test('object without keys is undefined', () => {
expect(subsetEquality('foo', 'bar')).toBe(undefined);
});

test('objects to not match', () => {
expect(subsetEquality({foo: 'bar'}, {foo: 'baz'})).toBe(false);
expect(subsetEquality('foo', {foo: 'baz'})).toBe(false);
});

test('null does not return errors', () => {
expect(subsetEquality(null, {foo: 'bar'})).not.toBeTruthy();
});

test('undefined does not return errors', () => {
expect(subsetEquality(undefined, {foo: 'bar'})).not.toBeTruthy();
});
});
1 change: 1 addition & 0 deletions packages/expect/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ export const subsetEquality = (object: Object, subset: Object) => {

return Object.keys(subset).every(
key =>
object != null &&
hasOwnProperty(object, key) &&
equals(object[key], subset[key], [iterableEquality, subsetEquality]),
);
Expand Down

0 comments on commit d59017d

Please sign in to comment.