Jest Array toContain does not work with objects - Improve documentation example
See original GitHub issueDo you want to request a feature or report a bug?
This is (I suspect) a bug report
What is the current behavior?
Jest expect(Array).toContain(Object) does not work in one very simple case
Here is a demo showing the (I suspect) bug: https://repl.it/@SandinoNunez/Testing-jest-toContain-Method
What is the expected behavior?
Jest expect(Array).toContain(Object) should work correctly
Please provide your exact Jest configuration
"jest": {
"transform": {
"^.+\\.js?$": "babel-jest"
},
"transformIgnorePatterns": [
"!node_modules/react-runtime"
],
"testMatch": [
"<rootDir>/lib/components/**/__tests__/**/*.js?(x)",
"<rootDir>/lib/**/__tests__/**/*.js?(x)",
"**/?(*.).js?(x)"
],
"setupTestFrameworkScriptFile": "./node_modules/jest-enzyme/lib/index.js",
"coverageThreshold": {
"collectCoverage": true,
"coverageFormats": [
"json",
"html"
],
"global": {
"branches": 80,
"functions": 90,
"lines": 80,
"statements": 80
},
"./lib/widget/widget.js": {
"branches": 90,
"functions": 90,
"lines": 90,
"statements": 90
},
"./lib/utils/*.js": {
"branches": 70,
"functions": 70,
"lines": 70,
"statements": 70
}
}
},
Jest version: 22.1.2
Issue Analytics
- State:
- Created 5 years ago
- Comments:6
Top Results From Across the Web
Jest Array/Object partial match with objectContaining and ...
It's possible to do partial matches on Arrays and Objects in Jest using expect.objectContaining and expect.arrayContaining .</
Read more >Jest - check that item does not contain object with some property
The matcher toContain checks if an item is in the array by using the strict equality check. That means that if you don't...
Read more >Expect - Jest
The expect function is used every time you want to test a value. ... matcher functions, documented below, to help you test different...
Read more >Expect · Jest
The expect function is used every time you want to test a value. ... of different matcher functions, documented below, to help you...
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
In case anyone comes across this same issue, use this instead of what has been suggested
.toMatchObject
Here is the reference
I figured out that using
toContainEqual
does exactly what I was looking for.Is confusing that the function accepts an object as value but it does not work as intended. The message makes the user believe that something is wrong but you see your object is contained in the array so can not figure out why the test is failing.
This is the output:
` expect(array).toContain(value)
`
I found the
toContainEqual
after looking at the source code to see why the object comparison was not working and maybe try to fix it.I think we can include an example usage here: https://facebook.github.io/jest/docs/en/using-matchers.html
Let me know what you think I can close this issue and open a PR for documentation update if you think that is necessary.