useExpectToHaveBeenCalled (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Rule available since:
v2.6.8 - Diagnostic Category:
lint/nursery/useExpectToHaveBeenCalled - This rule has a safe fix.
- The default severity of this rule is information.
- This rule belongs to the following domains:
- Sources:
- Same as
jest/prefer-to-have-been-called
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useExpectToHaveBeenCalled": "error" } } }}Description
Section titled “Description”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.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”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);Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.