Skip to content

Commit

Permalink
handle objects with no constructor in hasOwnProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanoglesby08 committed Nov 6, 2018
1 parent 9812e1b commit fddc444
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
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 fddc444

Please sign in to comment.