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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

objectContaining not matching valid subset per docs #11126

Closed
cdaringe opened this issue Feb 27, 2021 · 4 comments · May be fixed by #11157
Closed

objectContaining not matching valid subset per docs #11126

cdaringe opened this issue Feb 27, 2021 · 4 comments · May be fixed by #11157

Comments

@cdaringe
Copy link

🐛 Bug Report

Per the docs https://jestjs.io/docs/en/expect#expectobjectcontainingobject, the following minimal assertion is expected to pass

describe("objectContaining", () => {
  it("should work", () => {
    const headers = new Headers({});
    headers.set("weee", "weee");
    expect({
      a: "a",
      headers,
      b: {
        c: new Map(),
        d: "awfiotnaf",
      },
    }).toEqual(
      expect.objectContaining({
        b: {
          d: expect.any(String),
        },
      })
    );
  });
});

.toEqual seems suspicious, but peers in the community have asserted that because of the subset language, the above should still. take.

To Reproduce

pnpx jest

Link to repl or repo (highly encouraged)

https://github.com/cdaringe/jest-object-containing

envinfo


  System:
    OS: macOS 10.15.7
    CPU: (12) x64 Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
  Binaries:
    Node: 14.4.0 - /usr/local/bin/node
    Yarn: 1.22.5 - ~/.yarn/bin/yarn
    npm: 6.14.4 - /usr/local/bin/npm


@ninevra
Copy link
Contributor

ninevra commented Mar 1, 2021

This is a duplicate of #10462.

Despite what the documentation says, expect.objectContaining() is not recursive. expect.objectContaining({b: {d: expect.any(String)}}) matches any object containing a property b which deep-equals {d: expect.any(String)}. To get the effect you expect, you would need to write:

describe("objectContaining", () => {
  it("should work", () => {
    const headers = new Headers({});
    headers.set("weee", "weee");
    expect({
      a: "a",
      headers,
      b: {
        c: new Map(),
        d: "awfiotnaf",
      },
    }).toEqual(
      expect.objectContaining({
        b: expect.objectContaining({
          d: expect.any(String),
        }),
      })
    );
  });
});

A previous attempt to fix this was made in #10508. Unfortunately, #10508 broke a lot of things (see #10689, #10716), and had to be reverted, in #10766. The disposition as of #10716 (comment) was that the implementation is correct and the documentation is wrong, but the docs haven't been fixed yet.

@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 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
3 participants