diff --git a/packages/jest-matchers/src/__tests__/__snapshots__/matchers-test.js.snap b/packages/jest-matchers/src/__tests__/__snapshots__/matchers-test.js.snap index a60f271c816d..e94f9243c1d8 100644 --- a/packages/jest-matchers/src/__tests__/__snapshots__/matchers-test.js.snap +++ b/packages/jest-matchers/src/__tests__/__snapshots__/matchers-test.js.snap @@ -2618,6 +2618,18 @@ With a value of: " `; +exports[`.toHaveProperty() {pass: true} expect({"property": 1}).toHaveProperty('property', 1) 1`] = ` +"expect(object).not.toHaveProperty(path, value) + +Expected the object: + {\\"property\\": 1} +Not to have a nested property: + \\"property\\" +With a value of: + 1 +" +`; + exports[`.toMatch() {pass: true} expect(Foo bar).toMatch(/^foo/i) 1`] = ` "expect(received).not.toMatch(expected) diff --git a/packages/jest-matchers/src/__tests__/matchers-test.js b/packages/jest-matchers/src/__tests__/matchers-test.js index 6dab5abaae8a..1a315ff46cb0 100644 --- a/packages/jest-matchers/src/__tests__/matchers-test.js +++ b/packages/jest-matchers/src/__tests__/matchers-test.js @@ -727,6 +727,7 @@ describe('.toHaveProperty()', () => { [{a: 0}, 'a', 0], [{a: {b: undefined}}, 'a.b', undefined], [{a: {b: {c: 5}}}, 'a.b', {c: 5}], + [Object.assign(Object.create(null), {property: 1}), 'property', 1], ].forEach(([obj, keyPath, value]) => { test(`{pass: true} expect(${stringify(obj)}).toHaveProperty('${keyPath}', ${stringify(value)})`, () => { jestExpect(obj).toHaveProperty(keyPath, value); diff --git a/packages/jest-matchers/src/matchers.js b/packages/jest-matchers/src/matchers.js index 65d057d13367..a16aa6e7155d 100644 --- a/packages/jest-matchers/src/matchers.js +++ b/packages/jest-matchers/src/matchers.js @@ -15,7 +15,7 @@ import type {MatchersObject} from 'types/Matchers'; const diff = require('jest-diff'); const {escapeStrForRegex} = require('jest-regex-util'); -const {getObjectSubset, getPath} = require('./utils'); +const {getObjectSubset, getPath, hasOwnProperty} = require('./utils'); const { EXPECTED_COLOR, RECEIVED_COLOR, @@ -77,7 +77,7 @@ const subsetEquality = (object, subset) => { } return Object.keys(subset).every( key => - object.hasOwnProperty(key) && + hasOwnProperty(object, key) && equals(object[key], subset[key], [iterableEquality, subsetEquality]), ); }; @@ -543,7 +543,7 @@ const matchers: MatchersObject = { let diffString; - if (valuePassed && result.hasOwnProperty('value')) { + if (valuePassed && hasOwnProperty(result, 'value')) { diffString = diff(value, result.value, { expand: this.expand, }); @@ -553,7 +553,7 @@ const matchers: MatchersObject = { ? equals(result.value, value, [iterableEquality]) : hasEndProp; - if (result.hasOwnProperty('value')) { + if (hasOwnProperty(result, 'value')) { // we don't diff numbers. So instead we'll show the object that contains the resulting value. // And to get that object we need to go up a level. result.traversedPath.pop(); diff --git a/packages/jest-matchers/src/utils.js b/packages/jest-matchers/src/utils.js index 520177a4cfac..9f75cbc48f6e 100644 --- a/packages/jest-matchers/src/utils.js +++ b/packages/jest-matchers/src/utils.js @@ -17,6 +17,9 @@ type GetPath = { value?: any, }; +const hasOwnProperty = (object: Object, value: string) => + Object.prototype.hasOwnProperty.call(object, value); + const getPath = ( object: Object, propertyPath: string | Array, @@ -44,7 +47,7 @@ const getPath = ( result.lastTraversedObject || (result.lastTraversedObject = object); result.traversedPath.unshift(prop); if (propertyPath.length === 1) { - result.hasEndProp = object.hasOwnProperty(prop); + result.hasEndProp = hasOwnProperty(object, prop); if (!result.hasEndProp) { delete result.value; result.traversedPath.shift(); @@ -93,4 +96,5 @@ const getObjectSubset = (object: Object, subset: Object) => { module.exports = { getObjectSubset, getPath, + hasOwnProperty, };