noExpectToHaveBeenCalledOnce (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Rule available since:
v2.6.9 - Diagnostic Category:
lint/nursery/noExpectToHaveBeenCalledOnce - This rule has a safe fix.
- The default severity of this rule is information.
- This rule belongs to the following domains:
- Sources:
- Same as
vitest/prefer-called-times
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noExpectToHaveBeenCalledOnce": "error" } } }}Description
Section titled “Description”Disallow toHaveBeenCalledOnce() in favor of toHaveBeenCalledTimes(1).
Vitest provides toHaveBeenCalledOnce() and its alias toBeCalledOnce() to check that
a mock function (a fake function that records how it is called) was called exactly once.
toHaveBeenCalledTimes(1) and toBeCalledTimes(1) make the same check.
toHaveBeenCalledTimes() works for any number of calls, so using it for a single call
keeps every check of a call count in the same form.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”expect(sendEmail).toHaveBeenCalledOnce();code-block.js:1:19 lint/nursery/noExpectToHaveBeenCalledOnce FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ toHaveBeenCalledOnce() is used to check that a function was called exactly once.
> 1 │ expect(sendEmail).toHaveBeenCalledOnce();
│ ^^^^^^^^^^^^^^^^^^^^
2 │
ℹ toHaveBeenCalledTimes() makes the same check and works for any number of calls, so all checks of a call count look the same.
ℹ Use toHaveBeenCalledTimes(1) instead.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Safe fix: Use toHaveBeenCalledTimes(1) instead.
1 │ - expect(sendEmail).toHaveBeenCalledOnce();
1 │ + expect(sendEmail).toHaveBeenCalledTimes(1);
2 2 │
expect(sendEmail).toBeCalledOnce();code-block.js:1:19 lint/nursery/noExpectToHaveBeenCalledOnce FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ toBeCalledOnce() is used to check that a function was called exactly once.
> 1 │ expect(sendEmail).toBeCalledOnce();
│ ^^^^^^^^^^^^^^
2 │
ℹ toBeCalledTimes() makes the same check and works for any number of calls, so all checks of a call count look the same.
ℹ Use toBeCalledTimes(1) instead.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Safe fix: Use toBeCalledTimes(1) instead.
1 │ - expect(sendEmail).toBeCalledOnce();
1 │ + expect(sendEmail).toBeCalledTimes(1);
2 2 │
expect(sendEmail).toHaveBeenCalledTimes(1);expect(sendEmail).toBeCalledTimes(1);expect(sendEmail).not.toHaveBeenCalledTimes(1);Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.