From d9dfdb37e87d8739e186d74099a9a40bb3f2f82d Mon Sep 17 00:00:00 2001 From: Nicolas DUBIEN Date: Tue, 19 Feb 2019 23:37:08 +0100 Subject: [PATCH] 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 = (