Skip to content

useCalledWith (JavaScript)

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

Enforce using toHaveBeenCalledWith() to check the arguments of a function call.

toHaveBeenCalled() and its alias toBeCalled() only check that a mock function (a fake function that records how it is called) was called at least once. The test still passes when the function receives the wrong arguments. toHaveBeenCalledWith() also checks the arguments, so the test catches more mistakes.

When the value of an argument is hard to predict, pass a placeholder such as expect.anything() or expect.any(Date). The test then still checks the number and the position of the arguments. When the function is expected to be called without arguments, use toHaveBeenCalledWith() without arguments.

This rule also reports toHaveBeenCalledOnce() from Vitest, which has toHaveBeenCalledExactlyOnceWith() as the equivalent that checks the arguments.

Assertions that use .not are ignored. For example, expect(sendEmail).not.toHaveBeenCalled() checks that the function was never called, so there are no arguments to check.

expect(sendEmail).toHaveBeenCalled();
code-block.js:1:19 lint/nursery/useCalledWith ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ toHaveBeenCalled() doesn't check the arguments that the function was called with.

> 1 │ expect(sendEmail).toHaveBeenCalled();
│ ^^^^^^^^^^^^^^^^
2 │

ℹ The test passes even when the function receives the wrong arguments.

ℹ Use toHaveBeenCalledWith() and pass the expected arguments. For an argument that is hard to predict, pass expect.anything().

ℹ 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.

expect(sendEmail).toBeCalled();
code-block.js:1:19 lint/nursery/useCalledWith ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ toBeCalled() doesn't check the arguments that the function was called with.

> 1 │ expect(sendEmail).toBeCalled();
│ ^^^^^^^^^^
2 │

ℹ The test passes even when the function receives the wrong arguments.

ℹ Use toBeCalledWith() and pass the expected arguments. For an argument that is hard to predict, pass expect.anything().

ℹ 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.

expect(sendEmail).toHaveBeenCalledOnce();
code-block.js:1:19 lint/nursery/useCalledWith ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ toHaveBeenCalledOnce() doesn't check the arguments that the function was called with.

> 1 │ expect(sendEmail).toHaveBeenCalledOnce();
│ ^^^^^^^^^^^^^^^^^^^^
2 │

ℹ The test passes even when the function receives the wrong arguments.

ℹ Use toHaveBeenCalledExactlyOnceWith() and pass the expected arguments. For an argument that is hard to predict, pass expect.anything().

ℹ 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.

expect(sendEmail).toHaveBeenCalledWith("user@example.com", expect.anything());
expect(sendEmail).toHaveBeenCalledExactlyOnceWith("user@example.com");
expect(refresh).toHaveBeenCalledWith();
expect(sendEmail).toHaveBeenCalledTimes(1);
expect(sendEmail).not.toHaveBeenCalled();