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

handle objects with no constructor in hasOwnProperty #7334

Merged
merged 2 commits into from
Nov 7, 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
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