Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Fixes

- `[@jest/expect-utils]` `toMatchObject` diffs should include `Symbol` properties ([#13810](https://github.com/facebook/jest/pull/13810))

### Chore & Maintenance

### Performance
Expand Down
15 changes: 13 additions & 2 deletions packages/expect-utils/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ const hasPropertyInObject = (object: object, key: string | symbol): boolean => {
);
};

// Retrieves an object's keys for evaluation by getObjectSubset. This evaluates
// the prototype chain for string keys but not for symbols. (Otherwise, it
// could find values such as a Set or Map's Symbol.toStringTag, with unexpected
// results.)
//
// Compare with subsetEquality's use of Reflect.ownKeys.
const getObjectKeys = (object: object) => [
...Object.keys(object),
...Object.getOwnPropertySymbols(object),
];

export const getPath = (
object: Record<string, any>,
propertyPath: string | Array<string>,
Expand Down Expand Up @@ -131,7 +142,7 @@ export const getObjectSubset = (
const trimmed: any = {};
seenReferences.set(object, trimmed);

Object.keys(object)
getObjectKeys(object)
.filter(key => hasPropertyInObject(subset, key))
.forEach(key => {
trimmed[key] = seenReferences.has(object[key])
Expand All @@ -144,7 +155,7 @@ export const getObjectSubset = (
);
});

if (Object.keys(trimmed).length > 0) {
if (getObjectKeys(trimmed).length > 0) {
return trimmed;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4140,13 +4140,13 @@ exports[`toMatchObject() {pass: false} expect({"a": "a", "c": "d"}).toMatchObjec
exports[`toMatchObject() {pass: false} expect({"a": "b", "c": "d", Symbol(jest): "jest"}).toMatchObject({"a": "c", Symbol(jest): Any<String>}) 1`] = `
<d>expect(</><r>received</><d>).</>toMatchObject<d>(</><g>expected</><d>)</>

<g>- Expected - 2</>
<g>- Expected - 1</>
<r>+ Received + 1</>

<d> Object {</>
<g>- "a": "c",</>
<g>- Symbol(jest): Any<String>,</>
<r>+ "a": "b",</>
<d> Symbol(jest): Any<String>,</>
<d> }</>
`;

Expand Down