Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mikedidomizio/eslint-plugin-calledwith-calledtimes
Lint rule to suggest using `toHaveBeenCalledTimes` when using `toHaveBeenCalledWith`
https://github.com/mikedidomizio/eslint-plugin-calledwith-calledtimes
eslint eslint-plugin eslint-rules jasmine jest testing-library tohavebeencalledtimes tohavebeencalledwith vitest
Last synced: 25 days ago
JSON representation
Lint rule to suggest using `toHaveBeenCalledTimes` when using `toHaveBeenCalledWith`
- Host: GitHub
- URL: https://github.com/mikedidomizio/eslint-plugin-calledwith-calledtimes
- Owner: mikedidomizio
- License: mit
- Created: 2024-07-23T23:16:19.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2024-09-23T21:10:28.000Z (about 1 month ago)
- Last Synced: 2024-09-29T01:04:07.893Z (about 1 month ago)
- Topics: eslint, eslint-plugin, eslint-rules, jasmine, jest, testing-library, tohavebeencalledtimes, tohavebeencalledwith, vitest
- Language: JavaScript
- Homepage:
- Size: 117 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# eslint-plugin-calledwith-calledtimes
[![Tests Workflow](https://github.com/mikedidomizio/eslint-plugin-calledwith-calledtimes/actions/workflows/tests.yaml/badge.svg)](https://github.com/mikedidomizio/eslint-plugin-calledwith-calledtimes/actions/workflows/tests.yaml?query=branch%3Amain)
[![NPM Downloads](https://img.shields.io/npm/dm/eslint-plugin-calledwith-calledtimes)](https://www.npmjs.com/package/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 arguments
- `toHaveBeenCalledTimes` checks that a function was called an expected amount of times```tsx
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.
```diff
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 called
- `toHaveBeenCalledTimes` checks that a function was called an expected amount of times```tsx
expect(consoleSpy).toHaveBeenNthCalledWith(1, 'sending');
expect(consoleSpy).toHaveBeenNthCalledWith(2, 'cancelling');
expect(consoleSpy).toHaveBeenCalledTimes(2);
```[Inspiration](https://twitter.com/kentcdodds/status/1162098139609698304)
> [!NOTE]
> Currently, this has only been tested with `jest` but it should work with other test frameworks
> like `jasmine` and `vitest`. Please let me know if it works with these.## Installation
You'll first need to install [ESLint](https://eslint.org/):
```sh
npm i eslint --save-dev
```Next, install `eslint-plugin-calledwith-calledtimes`:
```sh
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:```json
{
"plugins": ["calledwith-calledtimes"]
}
```This only needs to run against
Then configure the rules you want to use under the rules section.
```json
{
"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](https://eslint.org/docs/user-guide/command-line-interface#--fix).
| Name | Description | 🔧 |
| :------------------------- | :-------------------------------------------------------------------------------------------- | :- |
| [jest](docs/rules/jest.md) | Ensures that using test matcher `toHaveBeenCalledWith` is followed by `toHaveBeenCalledTimes` | 🔧 |