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.

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:open
  • Created 4 years ago
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
pedrottimarkcommented, Apr 12, 2019

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:

  test('toMatchObject without key', () => {
    const expected = {a: {}};
    const received = {a: 13};
    expect(received).toMatchObject(expected);
  });

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:

  test('toMatchObject with key', () => {
    const expected = {a: {key: 'value'}};
    const received = {a: [{key: 'value'}]};
    expect(received).not.toMatchObject(expected);
  });

Here is a question about timing: Do y’all think that a change for the better is breaking?

0reactions
mario-petroviccommented, Aug 18, 2022

@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.

Read more comments on GitHub >

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

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