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

Add toHaveBeenCalledOnceWith matcher #430

Merged

Conversation

thibautsabot
Copy link
Contributor

What

Add toHaveBeenCalledOnceWith matcher

Why

To fix the second part of #138

Housekeeping

  • Unit tests
  • Documentation is up to date
  • No additional lint warnings
  • Typescript definitions are added/updated where relevant

@codecov
Copy link

codecov bot commented Mar 27, 2022

Codecov Report

Merging #430 (b5096b0) into main (5097d2a) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##              main      #430   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           71        72    +1     
  Lines          582       594   +12     
  Branches       148       151    +3     
=========================================
+ Hits           582       594   +12     
Impacted Files Coverage Δ
src/matchers/toHaveBeenCalledOnceWith.js 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@keeganwitt
Copy link
Collaborator

keeganwitt commented Jun 25, 2022

I think it'd be more flexible to do this using the existing matchers. For example,

const arg = 1;
mock(arg);
expect(mock).toHaveBeenCalledOnce();
expect(mock.mock.calls[0][0]).toEqual(arg);
// or expect(mock.mock.lastCall[0]).toEqual(arg);
// or expect(mock.mock.invocationCallOrder[0]).toEqual(arg);

Using this pattern, you can use toEqual() or your choice of any matcher instead of only equals().

@thibautsabot
Copy link
Contributor Author

Hey @keeganwitt! Thank you for taking the time to comment the PR.

I do agree that having a toHaveBeenCalledOnceWith helper will only use the basic equality.
However I feel like this is what most helpers are doing (like the official toHaveBeenCalledWith)

Use .toHaveBeenCalledWith to ensure that a mock function was called with specific arguments. The arguments are checked with the same algorithm that .toEqual uses.

@keeganwitt
Copy link
Collaborator

Thank you for the suggestion and the PR (sorry I forgot to say that earlier)!

The fact Jest's own toHaveBeenCalledWith uses equals() is a good point. Lemme see if I can get another opinion. Our goal is to add matchers that add enough value to be worth adding, and not every conceivable matcher. I'm still getting a feel for where to draw the line.

@G-Rath
Copy link
Contributor

G-Rath commented Jul 4, 2022

I think it makes sense to have this matcher if we've got toHaveBeenCalledOnce as it is a natural complement & it doesn't add that much (I'd actually think we could try and deduplicate the code with the non-with matcher, but that doesn't have to be in this PR), but I would expect it to try and use the same logic as toHaveBeenCalledWith, whatever that is, since this is meant to be in effect the same matcher just combined with a count checked.

@G-Rath
Copy link
Contributor

G-Rath commented Jul 4, 2022

I'm trying to find the underlying matcher in jest which I think is here: https://github.com/facebook/jest/blob/51fa61964f0b8f4d06db93a014b49b21ec53ea71/packages/expect/src/spyMatchers.ts#L553

I think we should confirm if the checks there are the equivalent to this.equal.

@Fouzyyyy
Copy link

Hi there

I thought about having a util function in our company codebase that is called toHaveBeenCalledOnceWith and under the hood, it calls toHaveBeenCalledTimes(1) and toHaveBeenCalledWith()

How is this different from the proposed implementation here?

@keeganwitt
Copy link
Collaborator

It would do the same thing. So would toBeCalledTimes(1) and toHaveBeenNthCalledWith(1, expected).

@keeganwitt
Copy link
Collaborator

I'm wondering if it'd maybe be better to change this to toHaveBeenCalledNTimesWith, where the equivalent of the current implementation would be toHaveBeenCalledNTimesWith(1, expected). Thoughts?

@monokrome
Copy link

monokrome commented Aug 16, 2022

@keeganwitt I think that toHaveBeenCalledNTimesWith could be a separate matcher, but I'd hate to conflate it with this feature personally. In my personal experience, toHaveBeenCalledOnceWith is a much more common use-case.

Also, if we wanted to consider toHaveBeenCalledNTimesWith, we're going to have to have the discussion of how the expected argument works. "Can I pass multiple arguments as ...expected for specifying expectations for each call?", "Does it just match every call expecting the same call if I only provide one?", etc. are things that would still need ironed out. At that point, it seems questionable whether it's easier to use this matcher or we should just manually check each call consecutively.

Also, I personally think that toHaveBeenCalledOnceWith feels "discoverable". If I needed toHaveBeenCalledNTimesWith, I wouldn't have found it by just expecting it to be there / guessing the name. I think that says something about toHaveBeenCalledOnceWith having value even if it ended up just wrapping a toHaveBeenCalledNTimesWith matcher.

That feels like a completely separate discussion / issue to me.

@monokrome
Copy link

@thibautsabot FYI it looks like this was approved 😺

@keeganwitt
Copy link
Collaborator

I'm trying to find the underlying matcher in jest which I think is here: https://github.com/facebook/jest/blob/51fa61964f0b8f4d06db93a014b49b21ec53ea71/packages/expect/src/spyMatchers.ts#L553

I think we should confirm if the checks there are the equivalent to this.equal.

I'm pretty sure those are the same.

@keeganwitt keeganwitt merged commit 1f04716 into jest-community:main Aug 16, 2022
fuxingloh pushed a commit to levaintech/sticky that referenced this pull request Oct 18, 2022
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [jest-extended](https://togithub.com/jest-community/jest-extended) |
[`3.0.2` ->
`3.1.0`](https://renovatebot.com/diffs/npm/jest-extended/3.0.2/3.1.0) |
[![age](https://badges.renovateapi.com/packages/npm/jest-extended/3.1.0/age-slim)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://badges.renovateapi.com/packages/npm/jest-extended/3.1.0/adoption-slim)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://badges.renovateapi.com/packages/npm/jest-extended/3.1.0/compatibility-slim/3.0.2)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://badges.renovateapi.com/packages/npm/jest-extended/3.1.0/confidence-slim/3.0.2)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>jest-community/jest-extended</summary>

###
[`v3.1.0`](https://togithub.com/jest-community/jest-extended/releases/tag/v3.1.0)

[Compare
Source](https://togithub.com/jest-community/jest-extended/compare/v3.0.2...v3.1.0)

#### What's Changed

- Add toHaveBeenCalledOnceWith matcher by
[@&#8203;thibautsabot](https://togithub.com/thibautsabot) in
[jest-community/jest-extended#430
- docs: mention `eslint-plugin-jest-extended` in README by
[@&#8203;G-Rath](https://togithub.com/G-Rath) in
[jest-community/jest-extended#493
- Update jest monorepo to v29 (major) by
[@&#8203;renovate](https://togithub.com/renovate) in
[jest-community/jest-extended#495

#### New Contributors

- [@&#8203;thibautsabot](https://togithub.com/thibautsabot) made their
first contribution in
[jest-community/jest-extended#430

**Full Changelog**:
jest-community/jest-extended@v3.0.2...v3.1.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, click
this checkbox.

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://app.renovatebot.com/dashboard#github/BirthdayResearch/sticky).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzMi4xODQuMiIsInVwZGF0ZWRJblZlciI6IjMyLjIwNC41In0=-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
@wiese
Copy link

wiese commented Nov 8, 2022

I like that this matcher exists but am surprised by it only supporting one expected value. Native .toHaveBeenCalledWith() and its siblings all support multiple arguments. I think there is value in consistency and it would add capabilities. Can somebody shed some light on the reasoning, please?

Ironically, #518 moved things in the opposite direction.

@keeganwitt
Copy link
Collaborator

#518 made the type match the actual implementation. #517 would change the implementation to accept more than just 1 argument.

kodiakhq bot pushed a commit to X-oss-byte/Nextjs that referenced this pull request Oct 13, 2023
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [jest-extended](https://togithub.com/jest-community/jest-extended) | [`1.2.1` -> `4.0.2`](https://renovatebot.com/diffs/npm/jest-extended/1.2.1/4.0.2) | [![age](https://developer.mend.io/api/mc/badges/age/npm/jest-extended/4.0.2?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/jest-extended/4.0.2?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/jest-extended/1.2.1/4.0.2?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/jest-extended/1.2.1/4.0.2?slim=true)](https://docs.renovatebot.com/merge-confidence/) |

---

### Release Notes

<details>
<summary>jest-community/jest-extended (jest-extended)</summary>

### [`v4.0.2`](https://togithub.com/jest-community/jest-extended/blob/HEAD/CHANGELOG.md#402)

[Compare Source](https://togithub.com/jest-community/jest-extended/compare/v4.0.1...v4.0.2)

##### Patch Changes

-   [`1f88101`](https://togithub.com/jest-community/jest-extended/commit/1f88101): Mark 2nd param of toHaveBeenCalledBefore and toHaveBeenCalledAfter optional

### [`v4.0.1`](https://togithub.com/jest-community/jest-extended/blob/HEAD/CHANGELOG.md#401)

[Compare Source](https://togithub.com/jest-community/jest-extended/compare/v4.0.0...v4.0.1)

##### Patch Changes

-   [`bad7056`](https://togithub.com/jest-community/jest-extended/commit/bad7056): Fix `toHaveBeenCalledExactlyOnceWith` typings
-   [`1609897`](https://togithub.com/jest-community/jest-extended/commit/1609897): Remove problematic Vitest types

### [`v4.0.0`](https://togithub.com/jest-community/jest-extended/blob/HEAD/CHANGELOG.md#400)

[Compare Source](https://togithub.com/jest-community/jest-extended/compare/v3.2.4...v4.0.0)

##### Major Changes

-   [`ab20845`](https://togithub.com/jest-community/jest-extended/commit/ab20845): Rename toHaveBeenCalledOnceWith to toHaveBeenCalledExactlyOnceWith

##### Minor Changes

-   [`5b80ca8`](https://togithub.com/jest-community/jest-extended/commit/5b80ca8): support vitest 0.31.0

##### Patch Changes

-   [`ab20845`](https://togithub.com/jest-community/jest-extended/commit/ab20845): Make CustomMatchers extend Record to fix a TypeScript error
-   [`ab20845`](https://togithub.com/jest-community/jest-extended/commit/ab20845): Fix toHaveBeenCalledOnceWith messages
-   [`ab20845`](https://togithub.com/jest-community/jest-extended/commit/ab20845): Change unknown to any in toHaveBeenCalledBefore and toHaveBeenCalledAfter

### [`v3.2.4`](https://togithub.com/jest-community/jest-extended/blob/HEAD/CHANGELOG.md#324)

[Compare Source](https://togithub.com/jest-community/jest-extended/compare/v3.2.3...v3.2.4)

##### Patch Changes

-   [`0b07513`](https://togithub.com/jest-community/jest-extended/commit/0b07513): Support varargs in toHaveBeenCalledOnceWith ([#&#8203;557](https://togithub.com/jest-community/jest-extended/issues/557))

### [`v3.2.3`](https://togithub.com/jest-community/jest-extended/releases/tag/v3.2.3)

[Compare Source](https://togithub.com/jest-community/jest-extended/compare/v3.2.2...v3.2.3)

#### What's Changed

-   fix(types): fix jest AsymmetricMatcher type by [@&#8203;rluvaton](https://togithub.com/rluvaton) in [jest-community/jest-extended#551

**Full Changelog**: jest-community/jest-extended@v3.2.2...v3.2.3

### [`v3.2.2`](https://togithub.com/jest-community/jest-extended/releases/tag/v3.2.2)

[Compare Source](https://togithub.com/jest-community/jest-extended/compare/v3.2.1...v3.2.2)

#### What's Changed

-   fix(types): add vitest support for AsymmetricMatchers by [@&#8203;rluvaton](https://togithub.com/rluvaton) in [jest-community/jest-extended#549

#### New Contributors

-   [@&#8203;rluvaton](https://togithub.com/rluvaton) made their first contribution in [jest-community/jest-extended#549

**Full Changelog**: jest-community/jest-extended@v3.2.1...v3.2.2

### [`v3.2.1`](https://togithub.com/jest-community/jest-extended/releases/tag/v3.2.1)

[Compare Source](https://togithub.com/jest-community/jest-extended/compare/v3.2.0...v3.2.1)

#### What's Changed

-   Change return type in Expect interface to void by [@&#8203;keeganwitt](https://togithub.com/keeganwitt) in [jest-community/jest-extended#535
-   Create pass/fail error messages only if required by [@&#8203;overlookmotel](https://togithub.com/overlookmotel) in [jest-community/jest-extended#545

#### New Contributors

-   [@&#8203;verdecchia](https://togithub.com/verdecchia) made their first contribution in [jest-community/jest-extended#541
-   [@&#8203;Unclemortuary](https://togithub.com/Unclemortuary) made their first contribution in [jest-community/jest-extended#540

**Full Changelog**: jest-community/jest-extended@v3.2.0...v3.2.1

### [`v3.2.0`](https://togithub.com/jest-community/jest-extended/releases/tag/v3.2.0)

[Compare Source](https://togithub.com/jest-community/jest-extended/compare/v3.1.0...v3.2.0)

#### What's Changed

-   Change return type in Expect interface from `Result` to `any` (closes [#&#8203;484](https://togithub.com/jest-community/jest-extended/issues/484)) by [@&#8203;keeganwitt](https://togithub.com/keeganwitt) in [jest-community/jest-extended#492
-   Fix `toHaveBeenCalledOnceWith` typing by [@&#8203;akoreh](https://togithub.com/akoreh) in [jest-community/jest-extended#507
-   Add `vitest` info to docs by [@&#8203;silverwind](https://togithub.com/silverwind) in [jest-community/jest-extended#515
-   Mark `jest` peerDependency as optional by [@&#8203;silverwind](https://togithub.com/silverwind) in [jest-community/jest-extended#520
-   `expect`: Add matcher `toBeInRange()` to validate if array elements are within the specified range by [@&#8203;mayankshukla94](https://togithub.com/mayankshukla94) in [jest-community/jest-extended#461
-   Fix `toHaveBeenCalledOnceWith` type definition (closes [#&#8203;518](https://togithub.com/jest-community/jest-extended/issues/518)) by [@&#8203;keeganwitt](https://togithub.com/keeganwitt) in [jest-community/jest-extended#523

#### New Contributors

-   [@&#8203;akoreh](https://togithub.com/akoreh) made their first contribution in [jest-community/jest-extended#507
-   [@&#8203;silverwind](https://togithub.com/silverwind) made their first contribution in [jest-community/jest-extended#515
-   [@&#8203;mayankshukla94](https://togithub.com/mayankshukla94) made their first contribution in [jest-community/jest-extended#461

**Full Changelog**: jest-community/jest-extended@v3.1.0...v3.2.0

### [`v3.1.0`](https://togithub.com/jest-community/jest-extended/releases/tag/v3.1.0)

[Compare Source](https://togithub.com/jest-community/jest-extended/compare/v3.0.2...v3.1.0)

#### What's Changed

-   Add toHaveBeenCalledOnceWith matcher by [@&#8203;thibautsabot](https://togithub.com/thibautsabot) in [jest-community/jest-extended#430
-   docs: mention `eslint-plugin-jest-extended` in README by [@&#8203;G-Rath](https://togithub.com/G-Rath) in [jest-community/jest-extended#493
-   Update jest monorepo to v29 (major) by [@&#8203;renovate](https://togithub.com/renovate) in [jest-community/jest-extended#495

#### New Contributors

-   [@&#8203;thibautsabot](https://togithub.com/thibautsabot) made their first contribution in [jest-community/jest-extended#430

**Full Changelog**: jest-community/jest-extended@v3.0.2...v3.1.0

### [`v3.0.2`](https://togithub.com/jest-community/jest-extended/releases/tag/v3.0.2)

[Compare Source](https://togithub.com/jest-community/jest-extended/compare/v3.0.1...v3.0.2)

#### What's Changed

-   Fix typings of `toThrowWithMessage`, support unconstructable errors by [@&#8203;GerkinDev](https://togithub.com/GerkinDev) in [jest-community/jest-extended#475
-   Fix toHaveBeenCalledBefore/toHaveBeenCalledAfter type definition by [@&#8203;aheuermann](https://togithub.com/aheuermann) in [jest-community/jest-extended#292
-   Declare matchers for use with expect.not.MATCHER ([#&#8203;385](https://togithub.com/jest-community/jest-extended/issues/385)) by [@&#8203;dharkness](https://togithub.com/dharkness) in [jest-community/jest-extended#386
-   Change any to unknown by [@&#8203;keeganwitt](https://togithub.com/keeganwitt) in [jest-community/jest-extended#477

#### New Contributors

-   [@&#8203;aheuermann](https://togithub.com/aheuermann) made their first contribution in [jest-community/jest-extended#292

**Full Changelog**: jest-community/jest-extended@v3.0.1...v3.0.2

### [`v3.0.1`](https://togithub.com/jest-community/jest-extended/releases/tag/v3.0.1)

[Compare Source](https://togithub.com/jest-community/jest-extended/compare/v3.0.0...v3.0.1)

#### What's Changed

-   fix: use `readonly` array types in matchers by [@&#8203;ilyub](https://togithub.com/ilyub) in [jest-community/jest-extended#465
-   fix(types): optional `failIfNoSecondInvocation` parameter by [@&#8203;j-waaang](https://togithub.com/j-waaang) in [jest-community/jest-extended#468

#### New Contributors

-   [@&#8203;ilyub](https://togithub.com/ilyub) made their first contribution in [jest-community/jest-extended#465
-   [@&#8203;j-waaang](https://togithub.com/j-waaang) made their first contribution in [jest-community/jest-extended#468

**Full Changelog**: jest-community/jest-extended@v3.0.0...v3.0.1

### [`v3.0.0`](https://togithub.com/jest-community/jest-extended/releases/tag/v3.0.0)

[Compare Source](https://togithub.com/jest-community/jest-extended/compare/v2.1.0...v3.0.0)

This is the same as https://github.com/jest-community/jest-extended/releases/tag/v2.1.0, but properly released as a major

### [`v2.1.0`](https://togithub.com/jest-community/jest-extended/releases/tag/v2.1.0)

[Compare Source](https://togithub.com/jest-community/jest-extended/compare/v2.0.0...v2.1.0)

⚠️ This should have been released as a major as it dropped Node 12 and 17 ⚠️

#### What's Changed

-   Restructure project by [@&#8203;mattphillips](https://togithub.com/mattphillips) in [jest-community/jest-extended#410
-   Fix broken link in example by [@&#8203;mattalexx](https://togithub.com/mattalexx) in [jest-community/jest-extended#422
-   Update toHaveBeenCalledAfter to fail if second mock is never called by [@&#8203;theryansmee](https://togithub.com/theryansmee) in [jest-community/jest-extended#420
-   Remove unsupported Node versions by [@&#8203;keeganwitt](https://togithub.com/keeganwitt) in [jest-community/jest-extended#450
-   chore(deps): lock file maintenance by [@&#8203;renovate](https://togithub.com/renovate) in [jest-community/jest-extended#453
-   chore(deps): lock file maintenance by [@&#8203;renovate](https://togithub.com/renovate) in [jest-community/jest-extended#454
-   Fix typos in matcherName in matcherHints by [@&#8203;keeganwitt](https://togithub.com/keeganwitt) in [jest-community/jest-extended#457
-   feat: upgrade to jest v28 by [@&#8203;G-Rath](https://togithub.com/G-Rath) in [jest-community/jest-extended#463

#### New Contributors

-   [@&#8203;mattalexx](https://togithub.com/mattalexx) made their first contribution in [jest-community/jest-extended#422
-   [@&#8203;theryansmee](https://togithub.com/theryansmee) made their first contribution in [jest-community/jest-extended#420
-   [@&#8203;G-Rath](https://togithub.com/G-Rath) made their first contribution in [jest-community/jest-extended#463

**Full Changelog**: jest-community/jest-extended@v2.0.0...v2.1.0

### [`v2.0.0`](https://togithub.com/jest-community/jest-extended/releases/tag/v2.0.0)

[Compare Source](https://togithub.com/jest-community/jest-extended/compare/v1.2.1...v2.0.0)

#### What's Changed

-   feat: delete expect and jest-matcher-utils dependencies by [@&#8203;keeganwitt](https://togithub.com/keeganwitt) in [jest-community/jest-extended#405

#### Breaking changes

-   Only `jest@27.2.5` and newer is supported

**Full Changelog**: jest-community/jest-extended@v1.2.1...v2.0.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/X-oss-byte/Nextjs).
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.

None yet

6 participants