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]: Jest 28 fails on Immutable.List().filter toEqual comparison #12752

Closed
romellem opened this issue Apr 26, 2022 · 6 comments · Fixed by #12763
Closed

[Bug]: Jest 28 fails on Immutable.List().filter toEqual comparison #12752

romellem opened this issue Apr 26, 2022 · 6 comments · Fixed by #12763

Comments

@romellem
Copy link
Contributor

Version

28.0.1

Steps to reproduce

  1. Install latest immutable - (e.g. yarn add immutable@^4.0.0).

  2. Create the following test file:

    const { List } = require("immutable");
    const numbers = List([1, 2]);
    
    describe("test", () => {
      it("fails in Jest 28.0.1, passes in Jest 27.5.1", () => {
        const twos = numbers.filter((num) => num === 2);
        expect(twos).toEqual(List([2]));
      });
    });
  3. Run the file in Jest 28.0.1.

Expected behavior

I expect the test to pass, as it does in Jest 27.5.1

Actual behavior

The test fails with the following error:

```
 FAIL  ./example.test.js
  test
    ✕ fails in Jest 28.0.1, passes in Jest 27.5.1 (6 ms)

  ● test › fails in Jest 28.0.1, passes in Jest 27.5.1

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

    Expected: Immutable.List [2]
    Received: serializes to the same string

       5 | 	it("fails in Jest 28.0.1, passes in Jest 27.5.1", () => {
       6 | 		const evens = numbers.filter((num) => num === 2);
    >  7 | 		expect(evens).toEqual(List([2]));
         | 		              ^
       8 | 	});
       9 | });
      10 |

      at Object.toEqual (example.test.js:7:17)
```

Additional context

I see the same issue with immutable@^3 as well.

Other deep equality comparisons with ImmutableJS objects that you'd expect to pass, do pass in Jest 28:

const { List, Map } = require("immutable");

it("passes1", () => {
    expect(List([2])).toEqual(List([2]));
});

it("passes2", () => {
    expect(Map({ hello: "world" })).toEqual(Map({ hello: "world" }));
});

it("passes3", () => {
    expect(List([Map({ hello: "world" }), Map({ foo: "bar" })])).toEqual(
        List([Map({ hello: "world" }), Map({ foo: "bar" })])
    );
});

The only reliable one I can reproduce is when a List gets filter'd.

Environment

System:
    OS: macOS 10.15.7
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
  Binaries:
    Node: 16.14.2 - ~/.nvm/versions/node/v16.14.2/bin/node
    Yarn: 1.22.18 - ~/.nvm/versions/node/v16.14.2/bin/yarn
    npm: 8.5.0 - ~/.nvm/versions/node/v16.14.2/bin/npm
  npmPackages:
    jest: ^27 => 27.5.1
@romellem romellem changed the title [Bug]: Jest 28 fails on Immutable.List().filter isEqual comparison [Bug]: Jest 28 fails on Immutable.List().filter toEqual comparison Apr 26, 2022
@SimenB
Copy link
Member

SimenB commented Apr 27, 2022

Bisecting, this was introduced in #8359.

@romellem would you be able to provide a PR fixing this? 🙂

@romellem
Copy link
Contributor Author

I can definitely take a look. Thanks for tracking down the offending commit, I'll do some digging and see what filter is doing with entries compared a regular List.

Any ideas that are coming to mind?

@romellem
Copy link
Contributor Author

Had some time to dig in, the problem is .filter (maybe other Collection methods too?) sets an ownerID on the _tail property whereas a direct List() does not. Since Lists are iterable, this path gets hit and eventually gets flagged as a difference.

List([1]).filter(v => v === 1)List([1])
filtered

direct

I see there are checks for isImmutableUnorderedSet() and isImmutableUnorderedKeyed(). Could a fix for this be to check if the object isList via its '@@__IMMUTABLE_LIST__@@' key, and check its iterable values similar to what happens with a Set?

@SimenB
Copy link
Member

SimenB commented Apr 27, 2022

That sounds reasonable to me!

@SimenB
Copy link
Member

SimenB commented May 6, 2022

@github-actions
Copy link

github-actions bot commented Jun 6, 2022

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 Jun 6, 2022
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.

2 participants