Skip to content

Commit

Permalink
toStrictEqual does not consider arrays with objects having undefine…
Browse files Browse the repository at this point in the history
…d values correctly (#7938)

<!-- Thanks for submitting a pull request! Please provide enough information so that others can review your pull request. The two fields below are mandatory. -->

<!-- Please remember to update CHANGELOG.md in the root of the project if you have not done so. -->

## Summary

Fixes #7937: `toStrictEqual` does not consider arrays with objects having undefined values correctly

## Test plan

Before that change, the following assertion was failing:

```js
expect([{a: undefined}]).not.toStrictEqual([{}]);
```

Now it passes as expected.
  • Loading branch information
dubzzz authored and thymikee committed Feb 20, 2019
1 parent be2703c commit 44bfa6e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- `[jest-cli]` Fix prototype pollution vulnerability in dependency ([#7904](https://github.com/facebook/jest/pull/7904))
- `[jest-cli]` Refactor `-o` and `--coverage` combined ([#7611](https://github.com/facebook/jest/pull/7611))
- `[expect]` Fix custom async matcher stack trace ([#7652](https://github.com/facebook/jest/pull/7652))
- `[expect]` Fix `toStrictEqual` not considering arrays with objects having undefined values correctly ([#7938](https://github.com/facebook/jest/pull/7938))
- `[jest-changed-files]` Improve default file selection for Mercurial repos ([#7880](https://github.com/facebook/jest/pull/7880))
- `[jest-validate]` Fix validating async functions ([#7894](https://github.com/facebook/jest/issues/7894))
- `[jest-circus]` Fix bug with test.only ([#7888](https://github.com/facebook/jest/pull/7888))
Expand Down
8 changes: 8 additions & 0 deletions packages/expect/src/__tests__/matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,14 @@ describe('.toStrictEqual()', () => {
}).not.toStrictEqual({b: 2});
});

it('does not ignore keys with undefined values inside an array', () => {
expect([{a: undefined}]).not.toStrictEqual([{}]);
});

it('does not ignore keys with undefined values deep inside an object', () => {
expect([{a: [{a: undefined}]}]).not.toStrictEqual([{a: [{}]}]);
});

it('passes when comparing same type', () => {
expect({
test: new TestClassA(1, 2),
Expand Down
4 changes: 3 additions & 1 deletion packages/expect/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,9 @@ export const sparseArrayEquality = (a: unknown, b: unknown) => {
// A sparse array [, , 1] will have keys ["2"] whereas [undefined, undefined, 1] will have keys ["0", "1", "2"]
const aKeys = Object.keys(a);
const bKeys = Object.keys(b);
return equals(a, b) && equals(aKeys, bKeys);
return (
equals(a, b, [iterableEquality, typeEquality], true) && equals(aKeys, bKeys)
);
};

export const partition = <T>(
Expand Down

0 comments on commit 44bfa6e

Please sign in to comment.