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 #3893

Closed
PSanetra opened this issue Oct 30, 2022 · 1 comment

Comments

@PSanetra
Copy link

PSanetra commented Oct 30, 2022

Version

29.0.3

Steps to reproduce

Execute the following test that fails:

class ClassWithPrivateJsField {
  #_a: string;

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

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

describe('toStrictEqual', () => {
  describe('on class instances with private javascript fields', () => {
    it('should not consider objects with different js-private field values via getter to be equal', () => {
      expect(new ClassWithPrivateJsField('v1')).not.toStrictEqual(
        new ClassWithPrivateJsField('v2')
      );
    });
  });
});

Also see https://stackblitz.com/edit/node-5wy1ts?file=index.spec.ts

Expected behavior

The test above should be successful

Actual behavior

The test above fails

Debug log

See stackblitz

Additional context

This issue might be somehow related to the follwing jest issues, but I am not 100% sure as there are errors thrown in those issues. In my case with ts-jest toStrictEqual just considers both instances as equal although they are not.

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

I am closing this issue as the same issue is reproducible via plain jest: jestjs/jest#13535

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant