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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Add a "toHaveBeenCalledExactlyOnceWith" assertion (name TBD) #15021

Open
mdentremont opened this issue Apr 8, 2024 · 2 comments
Open

Comments

@mdentremont
Copy link

mdentremont commented Apr 8, 2024

馃殌 Feature Proposal

Our codebase is littered with:

 expect(thing).toHaveBeenCalledTimes(1);
 expect(thing).toHaveBeenCalledWith(expected);

There is a toHaveBeenCalledTimesWith, but this doesn't assert the same thing, it allows other calls.

Rather than add a local extension to our projects, it would be amazing if something could be added to the lib that we could switch to in order to remove a bunch of unneeded calls.

Motivation

Allows for cleaner assertions on a common use case.

Example

 expect(sendSms).toHaveBeenCalledExactlyOnceWith(expectedMessage);

Pitch

Such a method would also encourage more strict tests as many folks tend to just use toHaveBeenCalledWith which doesn't fail when there are unexpected calls.

In environment like React where re-rendering is automatically triggered, it's very easy to assert that "X" was called with "true", only for it to be immediately followed up with a "false", and toHaveBeenCalledWith would be providing false confidence.

@mdentremont
Copy link
Author

It looks like this exists verbatim in jest-extended https://jest-extended.jestcommunity.dev/docs/matchers/mock/#tohavebeencalledexactlyoncewith

Could we use this issue to request that it be moved from extended into jest? (Likely a no, but I'll try 馃槢)

Copy link

github-actions bot commented May 8, 2024

This issue is stale because it has been open 30 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 8, 2024
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