Skip to content

Conversation

@AntonioVentilii
Copy link
Contributor

@AntonioVentilii AntonioVentilii commented Jun 20, 2025

I would like to propose a new rule prefer-called-once that enforces the use of toBeCalledOnce() or toHaveBeenCalledOnce() over toBeCalledTimes(1) or toHaveBeenCalledTimes(1).

Examples of incorrect code for this rule:

test('foo', () => {
  const mock = vi.fn()
  mock('foo')
  expect(mock).toBeCalledTimes(1)
  expect(mock).toHaveBeenCalledTimes(1)
})

Examples of correct code for this rule:

test('foo', () => {
  const mock = vi.fn()
  mock('foo')
  expect(mock).toBeCalledOnce()
  expect(mock).toHaveBeenCalledOnce()
})

@AntonioVentilii AntonioVentilii marked this pull request as ready for review June 20, 2025 14:47
Copy link
Member

@veritem veritem left a comment

Choose a reason for hiding this comment

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

thanks

@veritem veritem merged commit 9011281 into vitest-dev:main Jun 23, 2025
@AntonioVentilii
Copy link
Contributor Author

Thank you @veritem for taking care of it and for the approval!!!

@andykenward
Copy link
Contributor

It seems that having both prefer-called-times and prefer-called-once enabled breaks ESLint fix feature as they conflict?

 ESLintCircularFixesWarning: Circular fixes detected while fixing ...
  It is likely that you have conflicting rules in your configuration.

It might be my ESLint configuration. But so far solving the above issue by turning off this new rule.

'vitest/prefer-called-once': 'off',

@veritem
Copy link
Member

veritem commented Jul 1, 2025

@andykenward thanks for letting me know, I set vitest/prefer-called-once to turned off by default.

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.

3 participants