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

New assertion matcher "toHaveBeenCalledOnceWith" #6849

Closed
4 tasks done
DMartens opened this issue Nov 4, 2024 · 1 comment · Fixed by #6894
Closed
4 tasks done

New assertion matcher "toHaveBeenCalledOnceWith" #6849

DMartens opened this issue Nov 4, 2024 · 1 comment · Fixed by #6894
Labels
enhancement New feature or request pr welcome

Comments

@DMartens
Copy link

DMartens commented Nov 4, 2024

Clear and concise description of the problem

Currently there is no way to test whether a spy is only called once with certain arguments but both assertions are essential.
This currently requires two assertions:
expect(spy).toHaveBeenCalledWith(...args) and expect(spy).toHaveBeenCalledOnce().
I argue this case is so common that there should be a matcher included by default for this.

Suggested solution

Add a new default assertion matcher "toHaveBeenCalledOnceWith" which combines the checks of toHaveBeenCalledOnce and toHaveBeenCalledWith:

  1. checks the spy has been called once
  2. the provided arguments match
    The matcher should throw on error when there are less than two arguments.

Alternative

  • Use expect.extend per project
  • Add a more universal way to combine multiple assertion matchers

Additional context

There is an existing matcher for Jest via jest-extended.
I am willing to submit a PR for this if accepted.

Validations

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Nov 4, 2024

Yeah, it looks nice to have. We'll have toHaveBeenCalledAfter/Before soon #6056, so toHaveBeenCalledExactlyOnceWith is the last one missing from https://jest-extended.jestcommunity.dev/docs/matchers/Mock

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request pr welcome
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants