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

If a property is not matching in toEqual, a expect.any(Number) reports non-matching even when it is matching #9298

Closed
o-e opened this issue Dec 12, 2019 · 6 comments

Comments

@o-e
Copy link

o-e commented Dec 12, 2019

🐛 Bug Report

When using expect.any(Number) for a property when using toEqual Jest reports a non-matching value even when there is none, but only if another property is non-matching.

To Reproduce

The following test works fine and succeeds as expected.

  expect({ aNumber: 100 }).toEqual({
    aNumber: expect.any(Number),
  });

However, this tests fail - also as expected - BUT also reports that aNumber is non-matching (when it actually is matching):

  expect({ aNumber: 100, aString: "hello" }).toEqual({
    aNumber: expect.any(Number),
    aString: "goodbye"
  });

Outputs:

    expect(received).toEqual(expected) // deep equality

    - Expected
    + Received

      Object {
    -   "aNumber": Any<Number>,
    -   "aString": "goodbye",
    +   "aNumber": 100,
    +   "aString": "hello",
      }

Expected behavior

I am thinking the following output would be expected:

    - Expected
    + Received

      Object {
    -   "aString": "goodbye",
    +   "aString": "hello",
      }

envinfo

  System:
    OS: macOS 10.15.2
  Binaries:
    Node: 12.13.1 - ~/.nvm/versions/node/v12.13.1/bin/node
    Yarn: 1.19.2 - /usr/local/bin/yarn
    npm: 6.13.4 - ~/.nvm/versions/node/v12.13.1/bin/npm

jest: 24.9.0
@caiorsantanna
Copy link

I am also getting this bug! But with expect.any(String) inside a toEqual

@alexandervantrijffel
Copy link

as an alternative, you can use toMatchObject for that scenario eg this does test for aString and does suceed:

            expect({ aNumber: 100, aString: "hello" }).toMatchObject({
                aString: "hello"
            });

@dandv
Copy link
Contributor

dandv commented Aug 18, 2021

Still seeing this in Jest 27 with toMatchObject. Repl

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

No branches or pull requests

5 participants