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 not working to assert on the message of an error #5359

Closed
alem0lars opened this issue Jan 22, 2018 · 4 comments · Fixed by #5611
Closed

toMatchObject not working to assert on the message of an error #5359

alem0lars opened this issue Jan 22, 2018 · 4 comments · Fixed by #5611

Comments

@alem0lars
Copy link

alem0lars commented Jan 22, 2018

What is the current behavior?

While discussing on issue #3601, we found that the method toMatchObject isn't working to assert on the message of a Error.

In fact, the following expectation doesn't work:

it("foo", () => {
  expect(new Error("octopus")).toMatchObject({
    message: expect.stringMatching(/oct/)
  });
  return expect(Promise.reject(new Error("octopus"))).rejects.toMatchObject({
    message: expect.stringMatching(/oct/)
  });
});

What is the expected behavior?

The expectation described above should pass.

@SimenB
Copy link
Member

SimenB commented Jan 22, 2018

If using toMatchObject({message: 'octopus'}), it fails with a different error:

    Expected value to match object:
      {"message": "octopus"}
    Received:
      [Error: octopus]
    Difference:
    Compared values have no visual difference.

So I think this is a matter of toMatchObject to accepting Error as Object.

(we also should fix our message, as the diff is misleading with the current expect.stringMatching(/oct/) - it matches)

@SimenB
Copy link
Member

SimenB commented Jan 22, 2018

Ironically, git bisect blames #4339.

@kpsroka
Copy link
Contributor

kpsroka commented Feb 19, 2018

Hi,
I got to the bottom of the issue: the comparison of Error objects and regular objects doesn't work because of a check in subsetEquality, which extended to return undefined for Errors in #4339. When comparing Errors with ordinary Object, the subsetEquality returns undefined and the flow continues to check on Object.prototype.toString equality, from where it returns false.

What I suggest, and will send a PR soon to include this, is to remove the !isObjectWithKeys(object) part of the check in subsetEquality. This will essentially change the behavior of this method to return true to match not only own properties of the passed object, but also of its prototype's properties. Since toMatchObject's docs don't specify whether comparison depends only on an object's own properties.

cpojer pushed a commit that referenced this issue Feb 19, 2018
* fix: make Errors match with ordinary objects (#5359)

* chore: updating CHANGELOG.md
@github-actions
Copy link

This issue 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 a pull request may close this issue.

3 participants