Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jest-when

Package Overview
Dependencies
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-when

An extension lib for jest

  • 3.7.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
964K
increased by9.8%
Maintainers
1
Weekly downloads
 
Created

What is jest-when?

The jest-when package is an extension for Jest that allows for more readable and maintainable mock function behavior. It provides a way to define mock implementations based on specific argument conditions, making it easier to set up complex mocks.

What are jest-when's main functionalities?

Mocking based on arguments

This feature allows you to define different return values for a mock function based on the arguments it is called with. This makes it easier to handle multiple scenarios in your tests.

const { when } = require('jest-when');
const mockFn = jest.fn();

when(mockFn).calledWith(1).mockReturnValue('one');
when(mockFn).calledWith(2).mockReturnValue('two');

console.log(mockFn(1)); // 'one'
console.log(mockFn(2)); // 'two'
console.log(mockFn(3)); // undefined

Mocking based on multiple arguments

This feature allows you to define return values for a mock function based on multiple arguments, providing more granular control over the mock behavior.

const { when } = require('jest-when');
const mockFn = jest.fn();

when(mockFn).calledWith(1, 'a').mockReturnValue('one-a');
when(mockFn).calledWith(2, 'b').mockReturnValue('two-b');

console.log(mockFn(1, 'a')); // 'one-a'
console.log(mockFn(2, 'b')); // 'two-b'
console.log(mockFn(1, 'b')); // undefined

Mocking with custom matchers

This feature allows you to use Jest's built-in matchers to define mock behavior, making it easier to handle a wide range of input scenarios.

const { when } = require('jest-when');
const mockFn = jest.fn();

when(mockFn).calledWith(expect.any(Number)).mockReturnValue('number');
when(mockFn).calledWith(expect.any(String)).mockReturnValue('string');

console.log(mockFn(123)); // 'number'
console.log(mockFn('abc')); // 'string'
console.log(mockFn(true)); // undefined

Other packages similar to jest-when

FAQs

Package last updated on 03 Dec 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