From d9dfdb37e87d8739e186d74099a9a40bb3f2f82d Mon Sep 17 00:00:00 2001 From: Nicolas DUBIEN Date: Tue, 19 Feb 2019 23:37:08 +0100 Subject: [PATCH 1/4] Suggestion of fix for #7937 --- packages/expect/src/__tests__/matchers.test.js | 4 ++++ packages/expect/src/utils.ts | 5 ++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index bff61b7edc12..ad70c847e934 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -281,6 +281,10 @@ describe('.toStrictEqual()', () => { }).not.toStrictEqual({b: 2}); }); + it('does not ignore keys with undefined values inside an array', () => { + expect([{a: undefined}]).not.toStrictEqual([{}]); + }); + 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..6bfbd888d459 100644 --- a/packages/expect/src/utils.ts +++ b/packages/expect/src/utils.ts @@ -256,7 +256,10 @@ 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 = ( From 976ad4def511672d55494482b6fce5dcb6c6853f Mon Sep 17 00:00:00 2001 From: Nicolas DUBIEN Date: Tue, 19 Feb 2019 23:56:09 +0100 Subject: [PATCH 2/4] Update CHANGELOG --- CHANGELOG.md | 1 + packages/expect/src/utils.ts | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 570249abec1d..7e5c8509be1c 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]` `toStrictEqual` does not consider 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/utils.ts b/packages/expect/src/utils.ts index 6bfbd888d459..23d1fe1af797 100644 --- a/packages/expect/src/utils.ts +++ b/packages/expect/src/utils.ts @@ -257,8 +257,7 @@ export const sparseArrayEquality = (a: unknown, b: unknown) => { const aKeys = Object.keys(a); const bKeys = Object.keys(b); return ( - equals(a, b, [iterableEquality, typeEquality], true) && - equals(aKeys, bKeys) + equals(a, b, [iterableEquality, typeEquality], true) && equals(aKeys, bKeys) ); }; From d2f52287137d797fb97b4e2035965a482cb97924 Mon Sep 17 00:00:00 2001 From: Nicolas DUBIEN Date: Wed, 20 Feb 2019 13:46:20 +0100 Subject: [PATCH 3/4] Update CHANGELOG.md following review --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e5c8509be1c..bdf022c9ad6e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +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]` `toStrictEqual` does not consider arrays with objects having undefined values correctly ([#7938](https://github.com/facebook/jest/pull/7938)) +- `[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)) From 04ebef5e7396c9ee2173d2acf52ff3ddea5b1fae Mon Sep 17 00:00:00 2001 From: Nicolas DUBIEN Date: Wed, 20 Feb 2019 18:02:52 +0100 Subject: [PATCH 4/4] Add an additonal test for deeper keys --- packages/expect/src/__tests__/matchers.test.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index ad70c847e934..b57c29447d9b 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -285,6 +285,10 @@ describe('.toStrictEqual()', () => { 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),