question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Jest Array toContain does not work with objects - Improve documentation example

See original GitHub issue

Do 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:closed
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

13reactions
peoraycommented, Sep 28, 2018

In case anyone comes across this same issue, use this instead of what has been suggested .toMatchObject

Here is the reference

8reactions
sandinosasocommented, Apr 4, 2018

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)

Expected array:
  [{"attr1": "attr1 value", "attr2": true}]
To contain value:
  {"attr1": "attr1 value", "attr2": true}

`

I found the toContainEqualafter 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.

Read more comments on GitHub >

github_iconTop 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 >
API Reference | Vitest
A blazing fast unit test framework powered by Vite.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found