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/Regression]: Failed match mutates ObjectContaining #15442

Open
jeengbe opened this issue Jan 11, 2025 · 3 comments
Open

[Bug/Regression]: Failed match mutates ObjectContaining #15442

jeengbe opened this issue Jan 11, 2025 · 3 comments

Comments

@jeengbe
Copy link

jeengbe commented Jan 11, 2025

Version

30.0.0-alpha.6

Steps to reproduce

describe('should be able to use multiple times', () => {
  test('minimal reproduction', () => {
    const matcher = expect.objectContaining({ id: '2' });

    expect({ id: '1', prop: 'prop1' }).not.toEqual(matcher);
    expect({ id: '2' }).toEqual(matcher);
  });

  test('example use case', () => {
    // Check that the items received are what we expect them to be.
    // Note: The order of the items is not defined.
    const itemsFromDb = [
      {
        id: '1',
        prop: 'prop1',
      },
      {
        id: '2',
      },
    ];

    expect(itemsFromDb).toEqual(
      expect.arrayContaining([
        expect.objectContaining({ id: '1' }),
        expect.objectContaining({ id: '2' }),
      ]),
    );
    expect(itemsFromDb.length).toBe(2);

    /*
     * expect(received).toEqual(expected) // deep equality
     *
     * Expected: ArrayContaining [ObjectContaining {"id": "1"}, ObjectContaining {"id": "2", "prop": "prop1"}]
     *                                                                                       ~~~~~~~~~~~~~~~
     * Received: [{"id": "1", "prop": "prop1"}, {"id": "2"}]
     */
  });
});

The first test case demonstrates the bare-bones issue, the second shows how this pattern can arise in normal code.

Expected behavior

ObjectContaining should work within ArrayContaining.

Actual behavior

If an ObjectContaining matcher instance is invoked after having failed before (see the first test), the second invocation expects excess properties from the first invocation to be present.

Additional context

This broke in #15038.

Environment

System:
  OS: Linux 5.15 Ubuntu 22.04.5 LTS 22.04.5 LTS (Jammy Jellyfish)
  CPU: (24) x64 AMD Ryzen 9 3900XT 12-Core Processor
Binaries:
  Node: 22.1.0 - /usr/local/bin/node
  Yarn: 1.22.19 - /usr/local/bin/yarn
  npm: 10.9.2 - /usr/local/bin/npm
  pnpm: 9.15.3 - /usr/local/bin/pnpm
  bun: 1.1.20 - ~/.bun/bin/bun
npmPackages:
  jest: ^30.0.0-alpha.6 => 30.0.0-alpha.6
@mohammednumaan
Copy link
Contributor

@jeengbe It works perfectly fine here (or did I misunderstand the problem?) 🤔

Image

@jeengbe
Copy link
Author

jeengbe commented Jan 16, 2025

Those two tests should fail.

Image

Could you check you're using any of the alpha.4-6 versions, that's when the bug is introduced.

@mohammednumaan
Copy link
Contributor

@jeengbe I don't think I'm using any of the alpha versions 🤔

Image

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

2 participants