Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
@types/jest-when
Advanced tools
@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 441,602 weekly downloads. As such, @types/jest-when popularity was classified as popular.
We found that @types/jest-when demonstrated a healthy version release cadence and project activity because the last version was released less than 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
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.