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

toContain gives unhelpful error message when value being checked is an object #6808

Closed
philraj opened this issue Aug 6, 2018 · 7 comments · Fixed by #6810
Closed

toContain gives unhelpful error message when value being checked is an object #6808

philraj opened this issue Aug 6, 2018 · 7 comments · Fixed by #6810

Comments

@philraj
Copy link

philraj commented Aug 6, 2018

🐛 Bug Report

expect(arr).toContain(val) gives an unhelpful error message when matching object values.

To Reproduce

Steps to reproduce the behavior:

  1. Create an array containing objects: const arr = [{ a: 1 }, { b: 2}];
  2. Try to expect(arr).toContain({ a: 1 })

Error message is displayed:

Expected array:
      [{"a": 1 }, {"b": 2}]
    To contain value:
      {"a": 1}

but mentions nothing about how identity is being used for equality checks, leaving you scratching your head since it plainly displays "the same" object literal in both places.

Expected behavior

It would ideally add a note about how it's checking object identity and not key/value equality.

Link to repl or repo (highly encouraged)

https://repl.it/repls/UsableLovingPhases

Run npx envinfo --preset jest

  System:
    OS: macOS High Sierra 10.13.6
    CPU: x64 Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz
  Binaries:
    Node: 9.0.0 - /usr/local/bin/node
    Yarn: 1.7.0 - /usr/local/bin/yarn
    npm: 6.1.0 - /usr/local/bin/npm
  npmPackages:
    @types/jest: ^21.1.8 => 21.1.10
    jest: ^21.2.1 => 21.2.1
@Gregjarvez
Copy link
Contributor

Gregjarvez commented Aug 6, 2018

Expected array:
      [{"a": 1}, {"b": 2}]
 To contain value by reference:
     {"a": 1}

if this wording is okay I will make a PR for this.

@thymikee
Copy link
Collaborator

thymikee commented Aug 7, 2018

Oh, I didn't see you're using v21. In v23 we've added this message:

screen shot 2018-08-07 at 07 48 10

However, you can see it's a bit off. @Gregjarvez feel free to add 2 extra newlines here: https://github.com/facebook/jest/blob/277c547edd13ef2b69049ec1b4e768a2333471e1/packages/expect/src/matchers.js#L316

@Gregjarvez
Copy link
Contributor

sure !!

@philraj
Copy link
Author

philraj commented Aug 7, 2018

Oh my mistake, was working on an existing project and didn't realize we weren't up to date. Thanks. :-)

@rahilvora
Copy link

can I work on this issue, if someone is not ?

@thymikee
Copy link
Collaborator

thymikee commented Aug 7, 2018

Too late! Already addressed:

screen shot 2018-08-07 at 23 02 51

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

4 participants