
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@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
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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.