Socket
Socket
Sign inDemoInstall

@types/jest-when

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@types/jest-when

TypeScript definitions for jest-when


Version published
Weekly downloads
451K
increased by24.27%
Maintainers
1
Weekly downloads
 
Created

What is @types/jest-when?

@types/jest-when is a TypeScript type definition package for the jest-when library, which provides a way to mock functions in Jest based on specific argument conditions. It enhances Jest's mocking capabilities by allowing more granular control over mock behavior.

What are @types/jest-when's main functionalities?

Mocking based on specific arguments

This feature allows you to mock a function to return different values based on the arguments it is called with. The code sample demonstrates how to set up a mock function that returns 'one' when called with 1, 'two' when called with 2, and undefined for any other arguments.

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 mock a function based on multiple arguments. The code sample shows how to set up a mock function that returns 'one-a' when called with (1, 'a') and 'two-b' when called with (2, 'b').

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 default behavior

This feature allows you to set a default return value for a mock function, which will be used if none of the specific argument conditions are met. The code sample demonstrates a mock function that returns 'one' when called with 1 and 'default' for any other arguments.

const when = require('jest-when');
const mockFn = jest.fn().mockReturnValue('default');
when(mockFn).calledWith(1).mockReturnValue('one');

console.log(mockFn(1)); // 'one'
console.log(mockFn(2)); // 'default'

Other packages similar to @types/jest-when

FAQs

Package last updated on 18 Oct 2023

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