New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

eslint-plugin-calledwith-calledtimes

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslint-plugin-calledwith-calledtimes

suggest using toHaveBeenCalledTimes when using toHaveBeenCalledWith

  • 1.4.2
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

eslint-plugin-calledwith-calledtimes

Tests Workflow NPM Downloads

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
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 called
  • toHaveBeenCalledTimes 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);

Inspiration

[!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:

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.

NameDescription🔧
jestEnsures that using test matcher toHaveBeenCalledWith is followed by toHaveBeenCalledTimes🔧

Keywords

FAQs

Package last updated on 19 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc