toMatchObject pass when comparing any object to a empty object ({})
See original GitHub issue🐛 Bug Report
it:
expect({a:[{}]}).toMatchObject({a:{}})
expect({ a: undefined }).toMatchObject({ a: {} });
expect({ a: null }).toMatchObject({});
expect({ a: "string" }).toMatchObject({});
expect({ a: 1 }).toMatchObject({});
should fail but pass
To Reproduce
expect({a:[{}]}).toMatchObject({a:{}})
Expected behavior
fail
Link to repl or repo (highly encouraged)
Please provide either a repl.it demo or a minimal repository on GitHub.
Issues without a reproduction link are likely to stall.
Run npx envinfo --preset jest
Paste the results here:
System:
OS: macOS 10.14.4
CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
Binaries:
Node: 10.15.1 - ~/.nvm/versions/node/v10.15.1/bin/node
Yarn: 1.13.0 - /usr/local/bin/yarn
npm: 6.9.0 - ~/.nvm/versions/node/v10.15.1/bin/npm
Issue Analytics
- State:
- Created 4 years ago
- Comments:16 (6 by maintainers)
Top Results From Across the Web
Empty object matches object with different properties on ...
toMatchObject ({}); });. Now, when comparing with object that has another property that is not part of the testingObject my test PASSES, which...
Read more >Expect - Jest
Matchers should return an object (or a Promise of an object) with two keys. pass indicates whether there was a match or not,...
Read more >Expect · Jest
isNot; this.utils; expect.anything(); expect.any(constructor); expect. ... Matchers should return an object with two keys. pass indicates whether there was ...
Read more >JavaScript object basics - Learn web development | MDN
Congratulations, you've reached the end of our first JS objects article ... But this is an empty object, so we can't really do...
Read more >Testing Arrays and Objects with Chai.js | by Titus Stone
Similar to arrays, comparing two objects will use strict or deep equality. ... expect([1,2,3]).to.have.all.members([3,2,1]); // passes.
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 Free
Top 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
Yeah, I agree we can do better, because the reason is debatable in the words of Tim https://github.com/facebook/jest/issues/8308#issuecomment-482348139
A similar positive test passes (bad) for non-object received value when expected object is empty:
Because
Array.prototype.every
passes if expected object has no keys and the logic seems to assume an object as received value at:https://github.com/facebook/jest/blob/master/packages/expect/src/utils.ts#L267-L281
A similar negative test passes (good) if the object contains a property:
Here is a question about timing: Do y’all think that a change for the better is breaking?
@pedrottimark a bit late on this discussion, but as i see in the PR that was the product of this issue still accepts
{}
to be equal to any object. In that PR circular dependencies are handled.Just pointing to the fact that people should expect this still to happen. For me it was not clear that closing this issue did not make test with mentioned assertion fail.