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

[toMatchObject] Check different Error objects (#3864) #3866

Closed

Conversation

nathanforce
Copy link

@nathanforce nathanforce commented Jun 20, 2017

Summary

Fixes #3859. Similar to PR #3864 but fixes the issue further up the chain.

Test Plan

yarn jest after adding to matchers-test.js

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

@thymikee
Copy link
Collaborator

Makes sense, thanks for your contribution! Let's wait for CI to pass.

@codecov-io
Copy link

Codecov Report

Merging #3866 into master will increase coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3866      +/-   ##
==========================================
+ Coverage   57.63%   57.65%   +0.01%     
==========================================
  Files         194      194              
  Lines        6782     6782              
  Branches        6        6              
==========================================
+ Hits         3909     3910       +1     
+ Misses       2870     2869       -1     
  Partials        3        3
Impacted Files Coverage Δ
packages/jest-matchers/src/jasmine-utils.js 79.81% <100%> (+0.91%) ⬆️

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 09efdba...287b9f3. Read the comment docs.

@thymikee
Copy link
Collaborator

@thymikee toMatchObject should compare Error objects too? Or it should just fail without comparing when we encounter Error objects?

@anilreddykatta let's discuss it here. I'm good with Errors being compared by message.

@cpojer
Copy link
Member

cpojer commented Jun 27, 2017

Please rebase.

@cpojer
Copy link
Member

cpojer commented Jun 28, 2017

@nathanforce can you rebase this PR so I can merge it? :)

@jseminck
Copy link
Contributor

jseminck commented Jul 7, 2017

I also investigated this issue and my fix was slightly different. I updated the subsetEquality matcher so that the bug with the customMatchers got fixed.

This way we don't have to rely on the ordering of the code of Error objects vs customMatchers checking.

It turns out that running every() on an empty array produces true:

const test = [].every(key => key === 1);
console.log(test)
VM178:1 true

And an error is an object with an empty array for Object.keys():

const error = new Error('a');
console.log(Object.keys(error))
VM272:1 []

Therefore I updated the code for this matcher:

const isObjectWithKeys = a =>
  a !== null &&
  typeof a === 'object' &&
  Object.keys(a).length && // <------ new code 
  !(a instanceof Array) &&
  !(a instanceof Date);
const subsetEquality = (object, subset) => {
  if (!isObjectWithKeys(object) || !isObjectWithKeys(subset)) {
    return undefined;
  }

  return Object.keys(subset).every(
    key =>
      hasOwnProperty(object, key) &&
      equals(object[key], subset[key], [iterableEquality, subsetEquality]),
  );
};

Additionally, it seems like this code could be moved to the ./utils.js file, were also iterableEquality lives.

WDYT?

@nathanforce
Copy link
Author

@jseminck I see your point. Do you have those changes already made?

@jseminck
Copy link
Contributor

jseminck commented Jul 14, 2017

I have them in a local branch. But maybe it can be a separate PR after this is merged (just as a part of some code cleanup)?

@nathanforce
Copy link
Author

@jseminck I am closing this in favor of your changes. Could you open a PR against the issue?

@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 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

toMatchObject pass on comparing different Error objects
6 participants