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

[Bug]: objectContaining doesn't handle Symbols #14308

Closed
petedenham opened this issue Jul 7, 2023 · 3 comments · Fixed by #14414
Closed

[Bug]: objectContaining doesn't handle Symbols #14308

petedenham opened this issue Jul 7, 2023 · 3 comments · Fixed by #14414

Comments

@petedenham
Copy link

petedenham commented Jul 7, 2023

Version

29.5.0

Steps to reproduce

  1. git clone git@github.com:peterrattew/jest-object-containing-example.git
  2. npm install
  3. npm run test

Expected behavior

In objectContaining.symbols.test.js I would expect the first test to fail and the second test to pass, but it is currently the reverse.

When converting the Symols to strings or using toMatchObject the correct tests pass.

➜ jest-object-containing-example (main) ✔ npm run test

> object-containing-example@0.0.0 test
> jest

 FAIL  ./toMatchObject.symols.test.js
  ● symbol: toMatchObject should fail

    expect(received).toMatchObject(expected)

    - Expected  - 1
    + Received  + 1

      Object {
    -   Symbol(bar): "bar",
    +   Symbol(foo): "foo",
      }

      4 |
      5 | it('symbol: toMatchObject should fail', () => {
    > 6 |   expect(obj).toMatchObject({ [bar]: 'bar' });
        |               ^
      7 | });
      8 |
      9 | it('symbol: toMatchObject should pass', () => {

      at Object.toMatchObject (toMatchObject.symols.test.js:6:15)

 FAIL  ./objectContaining.symbols.test.js
  ● symbol: objectNotContaining should pass

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

    - Expected  - 2
    + Received  + 2

    - ObjectNotContaining {
    -   Symbol(bar): "bar",
    + Object {
    +   Symbol(foo): "foo",
      }

       8 |
       9 | it('symbol: objectNotContaining should pass', () => {
    > 10 |   expect(obj).toEqual(expect.not.objectContaining({ [bar]: 'bar' }));
         |               ^
      11 | });
      12 |

      at Object.toEqual (objectContaining.symbols.test.js:10:15)

 FAIL  ./objectContaining.strings.test.js
  ● string: objectContaining should fail

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

    - Expected  - 2
    + Received  + 2

    - ObjectContaining {
    -   "bar": "bar",
    + Object {
    +   "foo": "foo",
      }

      4 |
      5 | it('string: objectContaining should fail', () => {
    > 6 |   expect(obj).toEqual(expect.objectContaining({ [bar]: 'bar' }));
        |               ^
      7 | });
      8 |
      9 | it('string: objectNotContaining should pass', () => {

      at Object.toEqual (objectContaining.strings.test.js:6:15)

Test Suites: 3 failed, 3 total
Tests:       3 failed, 3 passed, 6 total
Snapshots:   0 total
Time:        1.524 s, estimated 2 s
Ran all test suites.

Actual behavior

The first test in objectContaining.symbols.test.js passes and the second fails.

Additional context

This is my first time raising a bug here so please let me know if I have done something wrong.

Environment

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
  Binaries:
    Node: 16.20.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.4.2 - /usr/local/bin/npm
    pnpm: 8.6.3 - /usr/local/bin/pnpm
  npmPackages:
    jest: ^29.5.0 => 29.5.0
@petedenham petedenham changed the title [Bug]: objectContaining doesn't [Bug]: objectContaining doesn't handle Symbols Jul 7, 2023
@mrazauskas
Copy link
Contributor

Confirmed: expect.objectContaining() does not work if Symbols are used as property keys. At the same time, toMatchObject() matcher does the right job. Something goes wrong with the logic of objectContaining.

@WCByrne
Copy link

WCByrne commented Sep 13, 2023

Hm... after upgrading from 29.5.0 to 29.7.0 I'm getting an error on a similar test. Though it's within a toBeCalledWith call.

expect(mySpy).toHaveBeenCalledWith(
      expect.objectContaining({
        [MY_SYMBOL]: { data: { name: inputValue } },
      })
    );

All my other similar usage without Symbols works fine.

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

Successfully merging a pull request may close this issue.

3 participants