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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

toMatchObject seems to ignore Map and Set #11250

Open
fabiosantoscode opened this issue Mar 29, 2021 · 6 comments
Open

toMatchObject seems to ignore Map and Set #11250

fabiosantoscode opened this issue Mar 29, 2021 · 6 comments

Comments

@fabiosantoscode
Copy link

fabiosantoscode commented Mar 29, 2021

馃悰 Bug Report

I've noticed that .toMatchObject ignores any Map and Set objects, silently accepting different things.

To Reproduce

Steps to reproduce the behavior:

Attempt to match an object against a Map or Set, for example:

expect({ hello: 'world' }).toMatchObject(new Set()); // Passes, should fail
expect({ hello: [] }).toMatchObject({ hello: new Map() }); // Passes, should fail

Expected behavior

The test should fail, because Map and Set are always different from strings and arrays.

Link to repl or repo (highly encouraged)

https://replit.com/@fabiosantoscode/jest-playground#matchobject.test.js

envinfo

  System:
    OS: Linux 5.8 Ubuntu 20.04.2 LTS (Focal Fossa)
    CPU: (8) x64 Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
  Binaries:
    Node: 15.4.0 - ~/.nvm/versions/node/v15.4.0/bin/node
    Yarn: 1.22.10 - ~/.nvm/versions/node/v15.4.0/bin/yarn
    npm: 7.6.3 - ~/.nvm/versions/node/v15.4.0/bin/npm
  npmPackages:
    jest: 26.2.2 => 26.2.2 

(and also in the REPL, which has this envinfo)

  System:
    OS: Linux 5.4 Debian GNU/Linux 9 (stretch) 9 (stretch)
    CPU: (4) x64 Intel(R) Xeon(R) CPU @ 2.30GHz
  Binaries:
    Node: 12.21.0 - /usr/local/bin/node
    Yarn: 1.22.5 - /usr/local/bin/yarn
    npm: 6.14.11 - /usr/local/bin/npm
  npmPackages:
    jest: ^26.6.3 => 26.6.3 
@fabiosantoscode
Copy link
Author

May be related to #11112 -- RegExp objects also have the same effect as Map in this case.

@julienw
Copy link

julienw commented Feb 18, 2022

Just got into this as well. The fact that it silently accepts the value and passes the test is error-prone.

@fabiosantoscode
Copy link
Author

Fixed the REPL to make it clearer. I started experimenting with regexes shortly after I posted this issue, but then I didn't revert my changes :)

@github-actions
Copy link

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Feb 18, 2023
@fabiosantoscode
Copy link
Author

fabiosantoscode commented Feb 18, 2023

I've updated the reproduction REPL to use Jest 29.4.3, and can still reproduce this issue.

@szimek
Copy link

szimek commented Dec 19, 2023

I've just ran into this issue when trying to use expect.objectContains with a value that is a Map.

This issue describes the problem: #13968, but is unfortunately already closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants