Skip to content

Conversation

@nicolasgillot
Copy link

@nicolasgillot nicolasgillot commented Nov 12, 2025

Summary

This PR introduces a new ESLint rule pair-to-have-been-called-assertions that enforces using toHaveBeenCalledTimes() or toBeCalledTimes() whenever using matchers like toHaveBeenCalledWith(), toBeCalledWith(), toHaveBeenNthCalledWith(), toBeNthCalledWith(), toHaveBeenLastCalledWith(), or toBeLastCalledWith().

Why?

When testing mock functions, developers often use toHaveBeenCalledWith() to verify that a function was called with specific arguments. However, without also checking the call count, the test can pass even if the mock was called more times than expected, potentially masking bugs.

What does this rule do?

  • Requires pairing toHaveBeenCalledWith() (and similar matchers) with toHaveBeenCalledTimes()
  • Provides auto-fix that adds the missing toHaveBeenCalledTimes(1) assertion
  • Detects contradictory assertions (e.g., toHaveBeenCalledTimes(0) with toHaveBeenCalledWith())
  • Works with all call-checking matchers: toHaveBeenCalledWith, toBeCalledWith, toHaveBeenNthCalledWith, toBeNthCalledWith, toHaveBeenLastCalledWith, toBeLastCalledWith

Examples

❌ Incorrect:

expect(mockFn).toHaveBeenCalledWith('arg');

✅ Correct:

expect(mockFn).toHaveBeenCalledTimes(1);
expect(mockFn).toHaveBeenCalledWith('arg');

Resolves #1820

@nicolasgillot nicolasgillot force-pushed the pair-to-have-been-called-assertions branch from 0f96239 to ce3724f Compare November 12, 2025 16:24
@nicolasgillot nicolasgillot force-pushed the pair-to-have-been-called-assertions branch from ce3724f to dd0b9b3 Compare November 12, 2025 16:28
@G-Rath G-Rath changed the title feat: create new pair-to-have-been-called-assertions rule feat: create new pair-to-have-been-called-assertions rule Nov 13, 2025
Copy link
Collaborator

@G-Rath G-Rath left a comment

Choose a reason for hiding this comment

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

looks like a good start - could you remove the contradictoryCallTimes logic and do any resulting simplifications, and review the istanbul comments as I'm pretty sure most should be coverable


When testing mock functions, developers often use `toHaveBeenCalledWith()`,
`toBeCalledWith()`, `toHaveBeenNthCalledWith()`, `toBeNthCalledWith()`,
`toHaveBeenLastCalledWith()`, or `toBeLastCalledWith()` to verify that a
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
`toHaveBeenLastCalledWith()`, or `toBeLastCalledWith()` to verify that a
`toHaveBeenLastCalledWith()`, and `toBeLastCalledWith()` to verify that a


## Examples

### Incorrect
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
### Incorrect
Examples of **incorrect** code for this rule:

expect(mockFn).toBeLastCalledWith('last');
```

### Correct
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
### Correct
Examples of **correct** code with this rule:


## Additional Checks

### Contradictory Assertions
Copy link
Collaborator

Choose a reason for hiding this comment

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

At this point I think this is out of scope for this rule - can we remove it for now?

Comment on lines +139 to +142

However, it's generally recommended to keep this rule enabled as it helps catch
bugs where functions are called more times than expected, leading to more robust
and reliable tests.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think this is particularly useful to have in this section, especially as this a rule that is enabled by default

Suggested change
However, it's generally recommended to keep this rule enabled as it helps catch
bugs where functions are called more times than expected, leading to more robust
and reliable tests.

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.

Rule proposal: jest/prefer-strict-mock-assertions

2 participants