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]: toStrictEquals incorrectly compares class instances with getters that access private fields #13535

Closed
PSanetra opened this issue Oct 30, 2022 · 7 comments

Comments

@PSanetra
Copy link

PSanetra commented Oct 30, 2022

Version

29.2.2

Steps to reproduce

Execute the following test that fails:

class A {
  #_a;

  constructor(a) {
    this.#_a = a;
  }

  get a() {
    return this.#_a;
  }
}

describe('toStrictEqual', () => {
  it('should not consider class instances with getters that access private fields with different values as equal', () => {
    expect(new A('v1')).not.toStrictEqual(new A('v2'));
  });
});

with the following jest config

export default {
  testEnvironment: 'node',
  transform: {},
};

Also see stackblitz: https://stackblitz.com/edit/node-z2xset?file=index.spec.js

Expected behavior

I expect that the test above is successful.

Actual behavior

The test fails.

Additional context

This issue might be somehow related to the follwing issues, but I am not 100% sure as there are errors thrown in those issues.

Environment

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
  Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 7.17.0 - /usr/local/bin/npm
  npmPackages:
    jest: 29.2.2 => 29.2.2
@PSanetra
Copy link
Author

On further investigation I suspect that getters of classes are generally not considered on class instances. Otherwise getters of simple objects (e.g. { get a() { return 'v1' } }) are considered during comparing objects. In my opinion this is inconsistent.

I think the cause of this issue is the key enumeration code: https://github.com/facebook/jest/blob/54ce1053aa462cd745127ac41d571d812c268580/packages/expect-utils/src/jasmineUtils.ts#L205-L217

for (const key in obj) and generally Object.keys(obj) will enumerate the getter keys of simple objects, but not those of class instances.

@github-actions
Copy link

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 github-actions bot added Stale and removed Stale labels Nov 29, 2022
@github-actions
Copy link

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 github-actions bot added Stale and removed Stale labels Dec 30, 2022
@github-actions
Copy link

github-actions bot commented Feb 1, 2023

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 github-actions bot added Stale and removed Stale labels Feb 1, 2023
@github-actions
Copy link

github-actions bot commented Mar 4, 2023

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 github-actions bot added the Stale label Mar 4, 2023
@github-actions
Copy link

github-actions bot commented Apr 3, 2023

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 Apr 3, 2023
@github-actions
Copy link

github-actions bot commented May 4, 2023

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 May 4, 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

1 participant