Skip to content

[Bug]: toStrictEquals incorrectly compares class instances with getters that access private fields #13535

@PSanetra

Description

@PSanetra

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions