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

objectContaining should be recursive but isn't #10462

Closed
BuckAMayzing opened this issue Aug 28, 2020 · 4 comments 路 May be fixed by #11157
Closed

objectContaining should be recursive but isn't #10462

BuckAMayzing opened this issue Aug 28, 2020 · 4 comments 路 May be fixed by #11157

Comments

@BuckAMayzing
Copy link

BuckAMayzing commented Aug 28, 2020

馃悰 Bug Report

I couldn't find a duplicate of this, even though it seems like something that could have come up before. The closest I found was #10186, which is related, but only tangentially.

expect.objectContaining does not work recursively as docs state.

To Reproduce

Here's a small test that will fail:

it('testy test test', () => {
  const expected = {
    a: 1,
    subProps: {
      someArray: [1],
    },
  }

  const received = {
    a: 1,
    subProps: {
      someArray: [1],
      someOtherArray: [],
    },
  }

  expect(received).toEqual(expect.objectContaining(expected))
})

Expected behavior

The test above should have passed according to the docs.

Link to repl or repo (highly encouraged)

Passing and failing tests here

envinfo

doesn't appear to be environment specific - repl and local machines both reproduce. I can add mine and repl's if it seems to be necessary.

workaround

use toMatchObject instead (which incidentally seems to have a nicer api)

additional info

If we look at toMatchObject vs objectContaining, the customTesters passed to the equals function differ - it looks like toMatchObject is actually true subset equality where objectContaining is only subset equality for the first level in the object - in this loop, recursion isn't happening.

Looks like there are two options to fix this: either objectContaining can implement a recursive asymmetricMatch, or it can share a code path with toMatchObject, since the two appear to be trying to accomplish the same goal (unless I'm missing something). The latter seems easier, at least on the surface, but I've not looked into it deeply.

It also looks like #10430 could potentially replace this fix, and objectContaining could become deprecated in favor of toMatchObject.

@ioancole
Copy link
Contributor

ioancole commented Sep 14, 2020

PR #10508 should fix this.

In the meantime, if you want the objectContaining behavior that you describe, you should also wrap expected-result sub-objects in objectContaining. The following test should pass:

it('testy test test', () => {
  const expected = expect.objectContaining({
    a: 1,
    subProps: expect.objectContaining({
      someArray: [1],
    }),
  });

  const received = {
    a: 1,
    subProps: {
      someArray: [1],
      someOtherArray: [],
    },
  };

  expect(received).toEqual(expected);
});

Or, of course, just use toMatchObject...

@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
Copy link

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 19, 2023
@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
3 participants