Skip to content

Fix calledWith(objectContaining) when there are multiple calls #15508

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

Merged
merged 1 commit into from
May 22, 2025

Conversation

ecraig12345
Copy link
Contributor

Summary

#15038 introduced an issue (#15501) where expect(fn).toHaveBeenCalledWith(expect.objectContaining(...)) would not work if the function was called multiple times, due to mutation of the sample state after each non-matching call.

const f = jest.fn();
f({ a: 1, b: 2 });
f({ a: 3, b: 7 });

// fails because the sample was mutated to be { a: 1, b: 7 }
expect(f).toHaveBeenCalledWith(expect.objectContaining({ b: 7 }));

This PR reverts the change and adds tests which will catch the issue if it's re-introduced.

Initially I thought I'd found a way to fix the issue and preserve the formatting improvements, but while adding tests, I found some cases where the failure messages were misleading (extra "expected" object properties shown that weren't originally present--example). I couldn't think of a way to fix that case without adding a new option to pretty-format, so it seemed best to just revert the change since correctness is more important than improved formatting.

Test plan

Existing tests pass (one snapshot changed due to the revert). Added tests which would detect if this issue is re-introduced.

Fixes #15501

Copy link

linux-foundation-easycla bot commented Feb 15, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: ecraig12345 / name: Elizabeth Craig (6375e33)

Copy link

netlify bot commented Feb 15, 2025

Deploy Preview for jestjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 6a658e1
🔍 Latest deploy log https://app.netlify.com/sites/jestjs/deploys/67afe6ff25f780000895031c
😎 Deploy Preview https://deploy-preview-15508--jestjs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Feb 15, 2025

Deploy Preview for jestjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 6375e33
🔍 Latest deploy log https://app.netlify.com/sites/jestjs/deploys/67b5009102dcf200081ed8df
😎 Deploy Preview https://deploy-preview-15508--jestjs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@ecraig12345 ecraig12345 force-pushed the object-containing-fix branch from bc00fc9 to 6375e33 Compare February 18, 2025 21:50
Copy link

This PR is stale because it has been open 90 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 May 19, 2025
@ecraig12345
Copy link
Contributor Author

Still waiting for review...?

Copy link
Member

@cpojer cpojer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. We should absolutely never mutate user data.

@cpojer cpojer merged commit 6d7e796 into jestjs:main May 22, 2025
86 checks passed
@ecraig12345 ecraig12345 deleted the object-containing-fix branch May 22, 2025 19:12
@jkillian
Copy link

I didn't verify, but I believe this also fixes #15442

Copy link

This pull request 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 Jun 29, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Incorrect comparison with calledWith(objectContaining) and multiple calls (reopening)
3 participants