Skip to content

Commit

Permalink
handle objects with no constructor in hasOwnProperty (#7334)
Browse files Browse the repository at this point in the history
* handle objects with no constructor in hasOwnProperty

Closes #6730

* update changelog
  • Loading branch information
ryanoglesby08 authored and SimenB committed Nov 7, 2018
1 parent 9812e1b commit 4859600
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
- `[jest-message-util]` Improve parsing of error messages for unusually formatted stack traces ([#7319](https://github.com/facebook/jest/pull/7319))
- `[jest-runtime]` Ensure error message text is not lost on errors with code frames ([#7319](https://github.com/facebook/jest/pull/7319))
- `[jest-haste-map]` Fix to resolve path that is start with words same as rootDir ([#7324](https://github.com/facebook/jest/pull/7324))
- `[expect]` Fix toMatchObject matcher when used with `Object.create(null)` ([#7334](https://github.com/facebook/jest/pull/7334))

### Chore & Maintenance

Expand Down
26 changes: 26 additions & 0 deletions packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3688,6 +3688,23 @@ Difference:
<dim> }</>"
`;

exports[`toMatchObject() {pass: false} expect({"a": "b"}).toMatchObject({"c": "d"}) 1`] = `
"<dim>expect(</><red>received</><dim>).toMatchObject(</><green>expected</><dim>)</>

Expected value to match object:
<green>{\\"c\\": \\"d\\"}</>
Received:
<red>{\\"a\\": \\"b\\"}</>
Difference:
<green>- Expected</>
<red>+ Received</>

<dim> Object {</>
<green>- \\"c\\": \\"d\\",</>
<red>+ \\"a\\": \\"b\\",</>
<dim> }</>"
`;

exports[`toMatchObject() {pass: false} expect({"a": [{"a": "a", "b": "b"}]}).toMatchObject({"a": [{"a": "c"}]}) 1`] = `
"<dim>expect(</><red>received</><dim>).toMatchObject(</><green>expected</><dim>)</>

Expand Down Expand Up @@ -4026,6 +4043,15 @@ Received:
<red>{\\"a\\": \\"b\\", \\"t\\": {\\"x\\": {\\"r\\": \\"r\\"}, \\"z\\": \\"z\\"}}</>"
`;

exports[`toMatchObject() {pass: true} expect({"a": "b"}).toMatchObject({"a": "b"}) 1`] = `
"<dim>expect(</><red>received</><dim>).not.toMatchObject(</><green>expected</><dim>)</>

Expected value not to match object:
<green>{\\"a\\": \\"b\\"}</>
Received:
<red>{\\"a\\": \\"b\\"}</>"
`;

exports[`toMatchObject() {pass: true} expect({"a": [{"a": "a", "b": "b"}]}).toMatchObject({"a": [{"a": "a"}]}) 1`] = `
"<dim>expect(</><red>received</><dim>).not.toMatchObject(</><green>expected</><dim>)</>

Expand Down
2 changes: 2 additions & 0 deletions packages/expect/src/__tests__/matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,7 @@ describe('toMatchObject()', () => {
[new Error('foo'), new Error('foo')],
[new Error('bar'), {message: 'bar'}],
[new Foo(), {a: undefined, b: 'b'}],
[Object.assign(Object.create(null), {a: 'b'}), {a: 'b'}],
].forEach(([n1, n2]) => {
it(`{pass: true} expect(${stringify(n1)}).toMatchObject(${stringify(
n2,
Expand Down Expand Up @@ -1178,6 +1179,7 @@ describe('toMatchObject()', () => {
[[1, 2, 3], [2, 3, 1]],
[[1, 2, 3], [1, 2, 2]],
[new Error('foo'), new Error('bar')],
[Object.assign(Object.create(null), {a: 'b'}), {c: 'd'}],
].forEach(([n1, n2]) => {
it(`{pass: false} expect(${stringify(n1)}).toMatchObject(${stringify(
n2,
Expand Down
14 changes: 11 additions & 3 deletions packages/expect/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,17 @@ type GetPath = {
value?: any,
};

export const hasOwnProperty = (object: Object, value: string) =>
Object.prototype.hasOwnProperty.call(object, value) ||
Object.prototype.hasOwnProperty.call(object.constructor.prototype, value);
export const hasOwnProperty = (object: Object, value: string) => {
// Account for objects created using unconventional means such as
// `Object.create(null)`, in which case the `object.constructor` is undefined
// See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create#Custom_and_Null_objects
const objectConstructor = object.constructor || Object;

return (
Object.prototype.hasOwnProperty.call(object, value) ||
Object.prototype.hasOwnProperty.call(objectConstructor.prototype, value)
);
};

export const getPath = (
object: Object,
Expand Down

0 comments on commit 4859600

Please sign in to comment.