Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: toStrictEqual considers array sparseness (resolves #7586) #7591

Merged
merged 4 commits into from
Jan 9, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

### Fixes

- `[expect]` `toStrictArray` considers sparseness of arrays. ([#7591](https://github.com/facebook/jest/pull/7591))
marcfallows marked this conversation as resolved.
Show resolved Hide resolved
- `[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))
Expand Down
10 changes: 10 additions & 0 deletions packages/expect/src/__tests__/matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
marcfallows marked this conversation as resolved.
Show resolved Hide resolved
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
});
marcfallows marked this conversation as resolved.
Show resolved Hide resolved
});

describe('.toEqual()', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/expect/src/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
getObjectSubset,
getPath,
iterableEquality,
sparseArrayEquality,
subsetEquality,
typeEquality,
isOneline,
Expand Down Expand Up @@ -666,7 +667,7 @@ const matchers: MatchersObject = {
const pass = equals(
received,
expected,
[iterableEquality, typeEquality],
[iterableEquality, typeEquality, sparseArrayEquality],
true,
);

Expand Down
11 changes: 11 additions & 0 deletions packages/expect/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <T>(
items: Array<T>,
predicate: T => boolean,
Expand Down