Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toMatchObject throws TypeError when a source property is null #6313

Merged
merged 4 commits into from
May 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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