
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
@types/jest-when
Advanced tools
TypeScript definitions for 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.
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'
jest-mock-extended is a package that provides extended mocking capabilities for Jest, including type-safe mocks and deep mocks. It is similar to jest-when in that it enhances Jest's mocking capabilities, but it focuses more on type safety and deep mocking.
ts-mockito is a mocking library for TypeScript inspired by the Java Mockito library. It provides a fluent API for creating mocks, stubs, and spies. Compared to jest-when, ts-mockito offers a more comprehensive and type-safe approach to mocking in TypeScript.
sinon is a standalone test spies, stubs, and mocks library for JavaScript. It can be used with any testing framework, including Jest. While sinon is not specific to Jest, it provides similar functionalities for mocking and stubbing functions, making it a versatile alternative.
npm install --save @types/jest-when
This package contains type definitions for jest-when (https://github.com/timkindberg/jest-when#readme).
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/jest-when.
These definitions were written by Alden Taylor, Trung Dang, Gregor Stamać, Nicholas Hehr, and Bogi Napoleon Wennerström.
FAQs
TypeScript definitions for jest-when
The npm package @types/jest-when receives a total of 490,813 weekly downloads. As such, @types/jest-when popularity was classified as popular.
We found that @types/jest-when demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.