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.

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:

  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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
thymikeecommented, Aug 7, 2018

Too late! Already addressed:

screen shot 2018-08-07 at 23 02 51
0reactions
github-actions[bot]commented, May 12, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

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