Skip to content

useExpectToHaveBeenCalled (JavaScript)

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

Enforce using toHaveBeenCalled() to check whether a mock function was called.

toHaveBeenCalledTimes(0) and toBeCalledTimes(0) check that a mock function was never called by comparing its number of calls to zero. not.toHaveBeenCalled() states the same check directly. When it fails, Jest and Vitest also list the calls that the function received, which makes the failure easier to understand.

In the same way, not.toHaveBeenCalledTimes(0) checks that a mock function was called at least once, which is what toHaveBeenCalled() does.

expect(sendEmail).toHaveBeenCalledTimes(0);
code-block.js:1:19 lint/nursery/useExpectToHaveBeenCalled  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This assertion compares the number of calls to 0.

> 1 │ expect(sendEmail).toHaveBeenCalledTimes(0);
│ ^^^^^^^^^^^^^^^^^^^^^
2 │

ℹ A comparison to zero is an indirect way to check whether a function was called, and it makes the failure message harder to understand.

ℹ Use not.toHaveBeenCalled() to check whether the function was called.

ℹ 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 not.toHaveBeenCalled() instead.

1 │ - expect(sendEmail).toHaveBeenCalledTimes(0);
1 │ + expect(sendEmail).not.toHaveBeenCalled();
2 2 │

expect(sendEmail).not.toHaveBeenCalledTimes(0);
code-block.js:1:23 lint/nursery/useExpectToHaveBeenCalled  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This assertion compares the number of calls to 0.

> 1 │ expect(sendEmail).not.toHaveBeenCalledTimes(0);
│ ^^^^^^^^^^^^^^^^^^^^^
2 │

ℹ A comparison to zero is an indirect way to check whether a function was called, and it makes the failure message harder to understand.

ℹ Use toHaveBeenCalled() to check whether the function was called.

ℹ 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 toHaveBeenCalled() instead.

1 │ - expect(sendEmail).not.toHaveBeenCalledTimes(0);
1 │ + expect(sendEmail).toHaveBeenCalled();
2 2 │

expect(sendEmail).toBeCalledTimes(0);
code-block.js:1:19 lint/nursery/useExpectToHaveBeenCalled  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This assertion compares the number of calls to 0.

> 1 │ expect(sendEmail).toBeCalledTimes(0);
│ ^^^^^^^^^^^^^^^
2 │

ℹ A comparison to zero is an indirect way to check whether a function was called, and it makes the failure message harder to understand.

ℹ Use not.toHaveBeenCalled() to check whether the function was called.

ℹ 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 not.toHaveBeenCalled() instead.

1 │ - expect(sendEmail).toBeCalledTimes(0);
1 │ + expect(sendEmail).not.toHaveBeenCalled();
2 2 │

expect(sendEmail).not.toHaveBeenCalled();
expect(sendEmail).toHaveBeenCalled();
expect(sendEmail).toHaveBeenCalledTimes(2);
expect(sendEmail).toHaveBeenCalledTimes(expectedCalls);