toContain gives unhelpful error message when value being checked is an object
See original GitHub issue🐛 Bug Report
expect(arr).toContain(val)
gives an unhelpful error message when matching object values.
To Reproduce
Steps to reproduce the behavior:
- Create an array containing objects:
const arr = [{ a: 1 }, { b: 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
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Jest Expect - w3resource
toBe(expected) Expected value to be (using Object.is): "banana" Received: "apple"`. If an assertion fails, the error message will give as ...
Read more >Jest matcher to match any one of three values - Stack Overflow
In order to one among the expected value, you can reverse the comparison and test it using toContain method like
Read more >Expect · Jest
The expect function is used every time you want to test a value. ... If you mix them up, your tests will still...
Read more >ArgumentOutOfRangeException Class (System)
The exception that is thrown when the value of an argument is outside the allowable range of values as defined by the invoked...
Read more >Error.prototype.message - JavaScript - MDN Web Docs
prototype.message. The message data property of an Error instance is a human-readable description of the error. Value.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Too late! Already addressed:
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.