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

toStrictEqual does not consider arrays with objects having undefined values correctly #7938

Merged
merged 4 commits into from Feb 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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]` 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))
Expand Down
8 changes: 8 additions & 0 deletions packages/expect/src/__tests__/matchers.test.js
Expand Up @@ -281,6 +281,14 @@ describe('.toStrictEqual()', () => {
}).not.toStrictEqual({b: 2});
});

it('does not ignore keys with undefined values inside an array', () => {
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),
Expand Down
4 changes: 3 additions & 1 deletion packages/expect/src/utils.ts
Expand Up @@ -256,7 +256,9 @@ 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 = <T>(
Expand Down