eslint-plugin-calledwith-calledtimes
v1.4.2
Published
suggest using toHaveBeenCalledTimes when using toHaveBeenCalledWith
Downloads
10
Maintainers
Readme
eslint-plugin-calledwith-calledtimes
Description
This plugin checks your test matchers (jasmine, jest, vitest) when
toHaveBeenCalledWith
is used, and let's you know you should pair it with toHaveBeenCalledTimes
.
The purpose of this is to ensure that not only our function is called with arguments, but it is called the exact amount of times that we expected.
Loose Checking
toHaveBeenCalledWith
checks that a function was called with specific argumentstoHaveBeenCalledTimes
checks that a function was called an expected amount of times
expect(consoleSpy).toHaveBeenCalledWith('sending');
expect(consoleSpy).toHaveBeenCalledWith('cancelling');
This doesn't check that the function was called the amount of times we expected it to be called.
expect(consoleSpy).toHaveBeenCalledWith("sending");
expect(consoleSpy).toHaveBeenCalledWith("cancelling");
+ expect(consoleSpy).toHaveBeenCalledTimes(2);
Now if consoleSpy
happens to accidentally get called more than we expected, it will error.
This gives us confidence that our code works exactly as expected.
Granular Checking
toHaveBeenNthCalledWith
checks that a function was called with specific arguments on the nth time the function was calledtoHaveBeenCalledTimes
checks that a function was called an expected amount of times
expect(consoleSpy).toHaveBeenNthCalledWith(1, 'sending');
expect(consoleSpy).toHaveBeenNthCalledWith(2, 'cancelling');
expect(consoleSpy).toHaveBeenCalledTimes(2);
[!NOTE]
Currently, this has only been tested withjest
but it should work with other test frameworks likejasmine
andvitest
. Please let me know if it works with these.
Installation
You'll first need to install ESLint:
npm i eslint --save-dev
Next, install eslint-plugin-calledwith-calledtimes
:
npm install eslint-plugin-calledwith-calledtimes --save-dev
Usage
Add calledwith-calledtimes
to the plugins section of your .eslintrc
configuration file. You
can omit the eslint-plugin-
prefix:
{
"plugins": ["calledwith-calledtimes"]
}
This only needs to run against
Then configure the rules you want to use under the rules section.
{
"rules": {
"calledwith-calledtimes/jest": "warn"
}
}
Configurations
TODO: Run eslint-doc-generator to generate the configs list (or delete this section if no configs are offered).
Rules
🔧 Automatically fixable by the --fix
CLI option.
| Name | Description | 🔧 |
| :------------------------- | :-------------------------------------------------------------------------------------------- | :- |
| jest | Ensures that using test matcher toHaveBeenCalledWith
is followed by toHaveBeenCalledTimes
| 🔧 |