Skip to content

Commit

Permalink
exclude non-enumerable symbol in object matching
Browse files Browse the repository at this point in the history
  • Loading branch information
fa93hws committed Nov 2, 2023
1 parent 728f112 commit b72b21d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- [**BREAKING**] Changes `testPathPattern` configuration option to `testPathPatterns`, which now takes a list of patterns instead of the regex.
- [**BREAKING**] `--testPathPattern` is now `--testPathPatterns`
- `[jest-reporters, jest-runner]` Unhandled errors without stack get correctly logged to console ([#14619](https://github.com/facebook/jest/pull/14619))
- `[@jest/expect-utils]` [**BREAKING**] exclude non-enumerable in object matching ([#14670](https://github.com/jestjs/jest/pull/14670))

### Performance

Expand Down
45 changes: 45 additions & 0 deletions packages/expect-utils/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,51 @@ describe('subsetEquality()', () => {
).toBe(false);
});
});

describe('matching subsets with symbols', () => {
describe('same symbol', () => {
test('objects to not match with value diff', () => {
const symbol = Symbol('foo');
expect(subsetEquality({[symbol]: 1}, {[symbol]: 2})).toBe(false);
});

test('objects to match with non-enumerable symbols', () => {
const symbol = Symbol('foo');
const foo = {};
Object.defineProperty(foo, symbol, {
enumerable: false,
value: 1,
});
const bar = {};
Object.defineProperty(bar, symbol, {
enumerable: false,
value: 2,
});
expect(subsetEquality(foo, bar)).toBe(true);
});
});

describe('different symbol', () => {
test('objects to not match with same value', () => {
expect(subsetEquality({[Symbol('foo')]: 1}, {[Symbol('foo')]: 2})).toBe(
false,
);
});
test('objects to match with non-enumerable symbols', () => {
const foo = {};
Object.defineProperty(foo, Symbol('foo'), {
enumerable: false,
value: 1,
});
const bar = {};
Object.defineProperty(bar, Symbol('foo'), {
enumerable: false,
value: 2,
});
expect(subsetEquality(foo, bar)).toBe(true);
});
});
});
});

describe('iterableEquality', () => {
Expand Down
24 changes: 17 additions & 7 deletions packages/expect-utils/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,23 @@ 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.)
export const getObjectKeys = (object: object): Array<string | symbol> => [
...Object.keys(object),
...Object.getOwnPropertySymbols(object),
];
// the prototype chain for string keys but not for non-enumerable symbols.
// (Otherwise, it could find values such as a Set or Map's Symbol.toStringTag,
// with unexpected results.)
// export const getObjectKeys = (object: object): Array<string | symbol> => [
// ...Object.keys(object),
// ...Object.getOwnPropertySymbols(object).filter(
// s => Object.getOwnPropertyDescriptor(object, s)?.enumerable,
// ),
// ];
export const getObjectKeys = (object: object): Array<string | symbol> => {
return [
...Object.keys(object),
...Object.getOwnPropertySymbols(object).filter(
s => Object.getOwnPropertyDescriptor(object, s)?.enumerable,
),
];
};

export const getPath = (
object: Record<string, any>,
Expand Down

0 comments on commit b72b21d

Please sign in to comment.