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 = (