diff --git a/CHANGELOG.md b/CHANGELOG.md index 570249abec1d..bdf022c9ad6e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index bff61b7edc12..b57c29447d9b 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -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), diff --git a/packages/expect/src/utils.ts b/packages/expect/src/utils.ts index 42384bbb8bc2..23d1fe1af797 100644 --- a/packages/expect/src/utils.ts +++ b/packages/expect/src/utils.ts @@ -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 = (