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

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ecraig12345
Copy link

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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)
1 participant