Skip to content

noExpectToHaveBeenCalledOnce (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noExpectToHaveBeenCalledOnce": "error"
}
}
}
}

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.

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);