From 4b3f69a50a47a81da412cf273da980dfb4d49431 Mon Sep 17 00:00:00 2001 From: Marc Fallows Date: Wed, 9 Jan 2019 09:55:12 +1100 Subject: [PATCH 1/4] fix: toStrictEqual considers array sparseness (resolves #7586) --- packages/expect/src/__tests__/matchers.test.js | 10 ++++++++++ packages/expect/src/matchers.js | 3 ++- packages/expect/src/utils.js | 11 +++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index 8d27231a6a0b..c88b439d784b 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -271,6 +271,16 @@ describe('.toStrictEqual()', () => { expect(c.constructor.name).toEqual(d.constructor.name); expect({test: c}).not.toStrictEqual({test: d}); }); + + it('does not pass when sparseness of arrays do not match', () => { + expect([, 1]).not.toStrictEqual([undefined, 1]); // eslint-disable-line no-sparse-arrays + expect([undefined, 1]).not.toStrictEqual([, 1]); // eslint-disable-line no-sparse-arrays + expect([, , , 1]).not.toStrictEqual([, 1]); // eslint-disable-line no-sparse-arrays + }); + + it('does not pass when equally sparse arrays have different values', () => { + expect([, 1]).not.toStrictEqual([, 2]); // eslint-disable-line no-sparse-arrays + }); }); describe('.toEqual()', () => { diff --git a/packages/expect/src/matchers.js b/packages/expect/src/matchers.js index 8549f544753d..eb2609442f43 100644 --- a/packages/expect/src/matchers.js +++ b/packages/expect/src/matchers.js @@ -30,6 +30,7 @@ import { getObjectSubset, getPath, iterableEquality, + sparseArrayEquality, subsetEquality, typeEquality, isOneline, @@ -666,7 +667,7 @@ const matchers: MatchersObject = { const pass = equals( received, expected, - [iterableEquality, typeEquality], + [iterableEquality, typeEquality, sparseArrayEquality], true, ); diff --git a/packages/expect/src/utils.js b/packages/expect/src/utils.js index 5a086749c479..f99a5fdde95f 100644 --- a/packages/expect/src/utils.js +++ b/packages/expect/src/utils.js @@ -243,6 +243,17 @@ export const typeEquality = (a: any, b: any) => { return false; }; +export const sparseArrayEquality = (a: any, b: any) => { + if (!Array.isArray(a) || !Array.isArray(b)) { + return undefined; + } + + // 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); +}; + export const partition = ( items: Array, predicate: T => boolean, From ff2f63d9010168e27a17928bdf88976de8934fa6 Mon Sep 17 00:00:00 2001 From: Marc Fallows Date: Wed, 9 Jan 2019 09:59:08 +1100 Subject: [PATCH 2/4] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a4a0a94d680f..63f6c7748348 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,7 @@ ### Fixes +- `[expect]` `toStrictArray` considers sparseness of arrays. ([#7591](https://github.com/facebook/jest/pull/7591)) - `[jest-cli]` Fix empty coverage data for untested files ([#7388](https://github.com/facebook/jest/pull/7388)) - `[jest-cli]` [**BREAKING**] Do not use `text-summary` coverage reporter by default if other reporters are configured ([#7058](https://github.com/facebook/jest/pull/7058)) - `[jest-mock]` [**BREAKING**] Fix bugs with mock/spy result tracking of recursive functions ([#6381](https://github.com/facebook/jest/pull/6381)) From fe65db71ee364b4516eb617a851601aace5393aa Mon Sep 17 00:00:00 2001 From: Marc Fallows Date: Wed, 9 Jan 2019 11:18:50 +1100 Subject: [PATCH 3/4] review feedback --- CHANGELOG.md | 2 +- packages/expect/src/__tests__/matchers.test.js | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 63f6c7748348..36eb4dbbea3b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,7 +49,7 @@ ### Fixes -- `[expect]` `toStrictArray` considers sparseness of arrays. ([#7591](https://github.com/facebook/jest/pull/7591)) +- `[expect]` `toStrictEqual` considers sparseness of arrays. ([#7591](https://github.com/facebook/jest/pull/7591)) - `[jest-cli]` Fix empty coverage data for untested files ([#7388](https://github.com/facebook/jest/pull/7388)) - `[jest-cli]` [**BREAKING**] Do not use `text-summary` coverage reporter by default if other reporters are configured ([#7058](https://github.com/facebook/jest/pull/7058)) - `[jest-mock]` [**BREAKING**] Fix bugs with mock/spy result tracking of recursive functions ([#6381](https://github.com/facebook/jest/pull/6381)) diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index c88b439d784b..15c1f9c62c56 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -272,15 +272,21 @@ describe('.toStrictEqual()', () => { expect({test: c}).not.toStrictEqual({test: d}); }); + /* eslint-disable no-sparse-arrays */ + it('passes for matching sparse arrays', () => { + expect([, 1]).toStrictEqual([, 1]); + }); + it('does not pass when sparseness of arrays do not match', () => { - expect([, 1]).not.toStrictEqual([undefined, 1]); // eslint-disable-line no-sparse-arrays - expect([undefined, 1]).not.toStrictEqual([, 1]); // eslint-disable-line no-sparse-arrays - expect([, , , 1]).not.toStrictEqual([, 1]); // eslint-disable-line no-sparse-arrays + expect([, 1]).not.toStrictEqual([undefined, 1]); + expect([undefined, 1]).not.toStrictEqual([, 1]); + expect([, , , 1]).not.toStrictEqual([, 1]); }); it('does not pass when equally sparse arrays have different values', () => { - expect([, 1]).not.toStrictEqual([, 2]); // eslint-disable-line no-sparse-arrays + expect([, 1]).not.toStrictEqual([, 2]); }); + /* eslint-enable */ }); describe('.toEqual()', () => { From 1c2d3516a938569f7d8d27e12e111c8cd3068855 Mon Sep 17 00:00:00 2001 From: Marc Fallows Date: Wed, 9 Jan 2019 21:13:50 +1100 Subject: [PATCH 4/4] line in docs --- docs/ExpectAPI.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/ExpectAPI.md b/docs/ExpectAPI.md index a0ac61bef2a6..b0f297bc0c51 100644 --- a/docs/ExpectAPI.md +++ b/docs/ExpectAPI.md @@ -1125,6 +1125,7 @@ Use `.toStrictEqual` to test that objects have the same types as well as structu Differences from `.toEqual`: - Keys with `undefined` properties are checked. e.g. `{a: undefined, b: 2}` does not match `{b: 2}` when using `.toStrictEqual`. +- Array sparseness is checked. e.g. `[, 1]` does not match `[undefined, 1]` when using `.toStrictEqual`. - Object types are checked to be equal. e.g. A class instance with fields `a` and `b` will not equal a literal object with fields `a` and `b`. ```js