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

Fixed toHaveProperty returning false positives when looking for undefined property #8923

Conversation

SixTfour
Copy link
Contributor

@SixTfour SixTfour commented Sep 6, 2019

Summary

Fix false positives when looking for undefined prop. {a:{}}.hasProperty('b', undefined) now returns false.

More info can be found here.

Fixes #8786

Test plan

Used commented test on line 1380 in packages/expect/src/__tests__/matchers.test.js.

// Input:
[{a: {}}, 'a.b', undefined], // wait until Jest 25
  ].forEach(([obj, keyPath, value]) => {
    test(`{pass: false} expect(${stringify(
      obj,
    )}).toHaveProperty('${keyPath}', ${stringify(value)})`, () => {
      expect(() =>
        jestExpect(obj).toHaveProperty(keyPath, value),
      ).toThrowErrorMatchingSnapshot();
      jestExpect(obj).not.toHaveProperty(keyPath, value);
    });
  });

// Output:
exports[`.toHaveProperty() {pass: false} expect({"a": {}}).toHaveProperty('a.b', undefined) 1`] = `
"<dim>expect(</><red>object</><dim>).toHaveProperty(</><green>path</><dim>, </><green>value</><dim>)</>

Expected the object:
  <red>{\\"a\\": {}}</>
To have a nested property:
  <green>\\"a.b\\"</>
With a value of:
  <green>undefined</>
Received:
  <red>object</>.a: <red>{}</>"
`;

@facebook-github-bot
Copy link
Contributor

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

@codecov-io
Copy link

codecov-io commented Sep 6, 2019

Codecov Report

Merging #8923 into master will decrease coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #8923      +/-   ##
==========================================
- Coverage   64.29%   64.28%   -0.01%     
==========================================
  Files         276      276              
  Lines       11707    11702       -5     
  Branches     2864     2863       -1     
==========================================
- Hits         7527     7523       -4     
  Misses       3549     3549              
+ Partials      631      630       -1
Impacted Files Coverage Δ
packages/expect/src/matchers.ts 97.16% <100%> (+0.29%) ⬆️
packages/expect/src/utils.ts 94.87% <100%> (ø) ⬆️

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 116303b...b18316a. Read the comment docs.

@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@sklaus64
Copy link

sklaus64 commented Sep 6, 2019

Output now reflects new output format:

"<dim>expect(</><red>received</><dim>).</>toHaveProperty<dim>(</><green>path</><dim>, </><green>value</><dim>)</>

Expected path: <green>\\"a.b\\"</>
Received path: <red>\\"a\\"</>

Expected value: <green>undefined</>
Received value: <red>{}</>"

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

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

Thank you! I'll leave this for @pedrottimark as he's the master of expect, but it LGTM 🙂

CHANGELOG.md Outdated Show resolved Hide resolved
packages/expect/src/__tests__/matchers.test.js Outdated Show resolved Hide resolved
@pedrottimark
Copy link
Contributor

Thank you. I need to take a more careful look at the new endPropIsDefined property, to make sure that we will be able to convert getPath function from recursive to iterative as a future chore.

@SixTfour
Copy link
Contributor Author

SixTfour commented Sep 9, 2019

Whoops. Forgot to take out that comment in matchers.test.js. Just fixed both of those issues. Thanks for taking a look at it @SimenB and @pedrottimark!

CHANGELOG.md Outdated Show resolved Hide resolved
@pedrottimark
Copy link
Contributor

@SimenB Is this an example of breaking change that we are postponing until Jest 26 milestone to maximize the number of people who can get the perf improvements in Jest 25?

@SimenB
Copy link
Member

SimenB commented Nov 9, 2019

@pedrottimark yea, I think so

@SimenB SimenB added this to the Jest 26 milestone May 2, 2020
@SimenB
Copy link
Member

SimenB commented May 2, 2020

@pedrottimark is this good to go you think?

@cpojer cpojer modified the milestones: Jest 26, High priority future May 2, 2020
@SimenB
Copy link
Member

SimenB commented Dec 6, 2020

@SixTfour would you be up for rebasing this? 🙂

@SimenB SimenB modified the milestones: High priority future, Jest 27 Dec 6, 2020
@cpojer
Copy link
Member

cpojer commented May 15, 2021

@SixTfour sorry for the long wait here. Would you mind rebasing this PR one more time?

@jeysal jeysal modified the milestones: Jest 27, High priority future May 20, 2021
@SimenB SimenB modified the milestones: High priority future, Jest 28 Feb 23, 2022
Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

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

thanks!

sorry about the delay 😅

@SimenB SimenB merged commit 0c7ec75 into jestjs:main Feb 23, 2022
@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 Mar 26, 2022
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.

toHaveProperty should not allow a missing property when looking for an undefined one
8 participants