jest icon indicating copy to clipboard operation
jest copied to clipboard

[Bug]: Order of fields in class implementing Iterable

Open WandererXII opened this issue 2 years ago • 2 comments

Version

28

Steps to reproduce

Comparing a class that implements Iterable, fails when fields are initiated in different order.

class IterWrap implements Iterable<[string, number]> {
  a: number;
  b: number;

  *[Symbol.iterator](): Iterator<[string, number]> {
    for (const prop of ['a', 'b'] as ('a' | 'b')[]) {
      yield [prop, this[prop]];
    }
  }

  static order1(): IterWrap {
    const it = new IterWrap();
    it.a = 1;
    it.b = 1;
    return it;
  }
  static order2(): IterWrap {
    const it = new IterWrap();
    it.b = 1;
    it.a = 1;
    return it;
  }
}

expect(IterWrap.order1()).toEqual(IterWrap.order1()); // PASS
expect(IterWrap.order2()).toEqual(IterWrap.order2()); // PASS
expect(IterWrap.order1()).toEqual(IterWrap.order2()); // FAIL, should pass

Before version 28 this used to work. I believe the cause is here: https://github.com/facebook/jest/pull/8359 It uses Object.entries, which means it compares this, which fails:

Object.entries(NoIterWrap.order1()) // [ [ 'a', 1 ], [ 'b', 1 ] ] 
Object.entries(NoIterWrap.order2()) // [ [ 'b', 1 ], [ 'a', 1 ] ]

Expected behavior

If all the properties are the same, I expect it to pass. This was the behavior before version 28, and this is also the behavior for a class that doesn't implement Iterable.

Actual behavior

Fails. The reason for failing is also not really helpful:

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


Expected: {"a": 1, "b": 1}
Received: serializes to the same string

Additional context

No response

Environment

System:
    OS: Linux 5.15 Manjaro Linux
    CPU: (4) x64 Intel(R)
  Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/bin/yarn
    npm: 8.5.0 - /usr/local/bin/npm
  npmPackages:
    jest: ^28 => 28.1.2

WandererXII avatar Jul 10 '22 15:07 WandererXII

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Aug 09 '22 16:08 github-actions[bot]

Wanna send a PR fixing it?

SimenB avatar Aug 09 '22 16:08 SimenB

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Sep 08 '22 16:09 github-actions[bot]

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[bot] avatar Oct 08 '22 16:10 github-actions[bot]

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[bot] avatar Oct 08 '22 16:10 github-actions[bot]

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[bot] avatar Nov 08 '22 00:11 github-actions[bot]