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: Symbol based properties in arrays causes toEqual to throw #6391

Merged
merged 6 commits into from
Jun 5, 2018

Conversation

mweststrate
Copy link
Contributor

Summary

toEqual matcher could not handle symbol based properties in array's, causing jest to throw:

Minimal example:

    const mySymbol = Symbol("test");
    const actual1 = [];
    actual1[mySymbol] = 3;
    const expected = [];
    expected[mySymbol] = 3;

    expect(actual1).toEqual(expected);

Throws:

 TypeError: allKeys[x].match is not a function

      at Object.test (packages/expect/src/__tests__/matchers.test.js:456:21)

The cause of this issue is this line which assumes that all property names of arrays are strings and hence .match can be called on them.

This assumption does not held for symbol based properties. If the property name is a symbol, it is never an index, so it should always be added to the extraKeys collection.

Test plan

I added a unit test demonstrating that deep equality on arrays behaves correctly now, even in the presence of symbolic properties.

@mweststrate mweststrate changed the title Fix: Symbol based properties in array caused Jest to throw Fix: Symbol based properties in arrays causes toEqual to throw Jun 4, 2018
@@ -222,7 +222,7 @@ function keys(obj, isArray, hasKey) {
}

for (var x = 0; x < allKeys.length; x++) {
if (!allKeys[x].match(/^[0-9]+$/)) {
if (typeof allKeys[x] === 'symbol' || !allKeys[x].match(/^[0-9]+$/)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just add a FlowFixMe comment above this, flow is wrong.

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed, used //$FlowFixMe. Hopefully that is correct, I am flow noob :)

Copy link
Member

@rickhanlonii rickhanlonii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great find

@codecov-io
Copy link

codecov-io commented Jun 4, 2018

Codecov Report

Merging #6391 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #6391   +/-   ##
=======================================
  Coverage   63.48%   63.48%           
=======================================
  Files         227      227           
  Lines        8697     8697           
  Branches        4        3    -1     
=======================================
  Hits         5521     5521           
  Misses       3175     3175           
  Partials        1        1

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e82a933...c049cb9. Read the comment docs.

@mweststrate
Copy link
Contributor Author

N.B.: #6392 is quite related, I am willing to PR that one as well, but wanted to verify that the solution direction is correct first

@mweststrate
Copy link
Contributor Author

Solved merge conflicts in last commit, no further changes

@cpojer cpojer merged commit 05bbcc5 into jestjs:master Jun 5, 2018
calebeby pushed a commit to Pigmice2733/scouting-frontend that referenced this pull request Jun 30, 2018
This Pull Request updates dependency [jest](https://github.com/facebook/jest) from `v23.1.0` to `v23.2.0`



<details>
<summary>Release Notes</summary>

### [`v23.2.0`](https://github.com/facebook/jest/blob/master/CHANGELOG.md#&#8203;2320)
[Compare Source](jestjs/jest@v23.1.0...v23.2.0)
##### Features

- `[jest-each]` Add support for keyPaths in test titles ([#&#8203;6457](`jestjs/jest#6457))
- `[jest-cli]` Add `jest --init` option that generates a basic configuration file with a short description for each option ([#&#8203;6442](`jestjs/jest#6442))
- `[jest.retryTimes]` Add `jest.retryTimes()` option that allows failed tests to be retried n-times when using jest-circus. ([#&#8203;6498](`jestjs/jest#6498))
##### Fixes

- `[jest-cli]` Add check to make sure one or more tests have run before notifying when using `--notify` ([#&#8203;6495](`jestjs/jest#6495))
- `[jest-cli]` Pass `globalConfig` as a parameter to `globalSetup` and `globalTeardown` functions ([#&#8203;6486](`jestjs/jest#6486))
- `[jest-config]` Add missing options to the `defaults` object ([#&#8203;6428](`jestjs/jest#6428))
- `[expect]` Using symbolic property names in arrays no longer causes the `toEqual` matcher to fail ([#&#8203;6391](`jestjs/jest#6391))
- `[expect]` `toEqual` no longer tries to compare non-enumerable symbolic properties, to be consistent with non-symbolic properties. ([#&#8203;6398](`jestjs/jest#6398))
- `[jest-util]` `console.timeEnd` now properly log elapsed time in milliseconds. ([#&#8203;6456](`jestjs/jest#6456))
- `[jest-mock]` Fix `MockNativeMethods` access in react-native `jest.mock()` ([#&#8203;6505](`jestjs/jest#6505))
##### Chore & Maintenance

- `[docs]` Add jest-each docs for 1 dimensional arrays ([#&#8203;6444](`jestjs/jest#6444))

---

</details>




---

This PR has been generated by [Renovate Bot](https://renovatebot.com).
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants