Skip to content

Commit

Permalink
Fix toHaveProperty() to use hasOwnProperty from Object (jestjs#3410)
Browse files Browse the repository at this point in the history
  • Loading branch information
thymikee authored and cpojer committed Apr 28, 2017
1 parent f4c2e5b commit 79d0d18
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2618,6 +2618,18 @@ With a value of:
"
`;

exports[`.toHaveProperty() {pass: true} expect({"property": 1}).toHaveProperty('property', 1) 1`] = `
"<dim>expect(<red>object</><dim>).not.toHaveProperty(<green>path</>, <green>value</><dim>)

Expected the object:
<red>{\\"property\\": 1}</>
Not to have a nested property:
<green>\\"property\\"</>
With a value of:
<green>1</>
"
`;

exports[`.toMatch() {pass: true} expect(Foo bar).toMatch(/^foo/i) 1`] = `
"<dim>expect(<red>received</><dim>).not.toMatch(<green>expected</><dim>)

Expand Down
1 change: 1 addition & 0 deletions packages/jest-matchers/src/__tests__/matchers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions packages/jest-matchers/src/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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]),
);
};
Expand Down Expand Up @@ -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,
});
Expand All @@ -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();
Expand Down
6 changes: 5 additions & 1 deletion packages/jest-matchers/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -93,4 +96,5 @@ const getObjectSubset = (object: Object, subset: Object) => {
module.exports = {
getObjectSubset,
getPath,
hasOwnProperty,
};

0 comments on commit 79d0d18

Please sign in to comment.