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

Array keyPath for toHaveProperty #5220

Merged
merged 9 commits into from
Jan 5, 2018

Conversation

dmmulroy
Copy link
Contributor

@dmmulroy dmmulroy commented Jan 3, 2018

Summary

Add support to .toHaveProperty matcher to accept the keyPath argument as an array of properties/indices.

Discussion can be found at #3873

Test plan

Update the .toHaveProperty tests to test against array as the keyPath argument. Include test for testing that an object has a flattened property like: {'a.b.c.d': 1} and against array indices.

@dmmulroy dmmulroy changed the title Array key path to have property Array keyPath for toHaveProperty Jan 3, 2018
@codecov-io
Copy link

codecov-io commented Jan 3, 2018

Codecov Report

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

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #5220   +/-   ##
=======================================
  Coverage   60.91%   60.91%           
=======================================
  Files         202      202           
  Lines        6731     6731           
  Branches        3        3           
=======================================
  Hits         4100     4100           
  Misses       2630     2630           
  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 2b9e7b9...9788fd4. Read the comment docs.

Copy link
Collaborator

@thymikee thymikee left a comment

Choose a reason for hiding this comment

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

Nice, I'm surprised it just worked this way. Left some comments inlined.

CHANGELOG.md Outdated
@@ -13,10 +13,17 @@
([#5166](https://github.com/facebook/jest/pull/5166))
* `[jest-config]` Allow configuration objects inside `projects` array
([#5176](https://github.com/facebook/jest/pull/5176))
* `[jest-cli]` Add support to .toHaveProperty matcher to accept the keyPath
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be [expect]. Also, can you add backticks?

`[expect]` Add support to `.toHaveProperty` matcher to accept the keyPath

"
`;

exports[`.toHaveProperty() {pass: true} expect({"a": {"b": {"c": {"d": 1}}}}).toHaveProperty('a,b,c,d')' 1`] = `
Copy link
Collaborator

Choose a reason for hiding this comment

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

Btw, this is out of scope of this PR, but could you please remove this extra single-quote here?
Also, not super relevant, but this should say .toHaveProperty(['a','b','c','d']) instead .toHaveProperty('a,b,c,d')

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I fixed the changelog and remove the extra single-quote. How would you prefer for the snapshot output be changed (if at all for this PR)? I tried using the stringify util function but ended up with a different issue where the output looked like .toHaveProperty('["a","b","c","d"]')

Copy link
Collaborator

Choose a reason for hiding this comment

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

If I had to choose, I'd pick the stringified version (it's just a name of the test anyway)

Copy link
Contributor Author

@dmmulroy dmmulroy Jan 3, 2018

Choose a reason for hiding this comment

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

Taking a second look, it also had an adverse effect of adding additional double quotes to normal keypaths like so : exports['.toHaveProperty() {pass: false} expect("abc").toHaveProperty('"a.b.c"') 1']. This might be better off fixed in the pretty-format package in a separate PR to prevent really cluttering up the matcher tests with string parsing logic.

@kubijo
Copy link

kubijo commented Jul 30, 2018

Hi.

It seems that this fine feature lacks the update of flow type-def.
The type in jest_v23.x.x.js is still:

toHaveProperty(propPath: string, value?: any): void;

I'd post the update, but I'm at loss where's the correct place

@thymikee
Copy link
Collaborator

Flow-typed repo

@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.

6 participants