[Feature]: Ergonomic way to jest.fn over a function type in TypeScript
See original GitHub issue🚀 Feature Proposal
TypeScript users need a good way to represent that a jest.fn()
should adhere to a particular function type. I propose its generic type parameters be switched to a single type parameter representing a function type.
jest.fn<MyFunctionType>();
jest.fn<(input: string) => number>();
Motivation
Right now jest.fn
is generic on a T
for the return type and Y
for the args. That means there’s no easy way to go from some function type to a jest.fn
of that type.
The closest is to use ReturnType
and Parameters
:
import { jest } from '@jest/globals';
type MyFunctionType = (input: string) => number;
const spy = jest.fn<ReturnType<MyFunctionType>, Parameters<MyFunctionType>>();
Edit: or a wrapper like…
export const fn = <Reference extends (...args: any) => any>(reference?: Reference) =>
jest.fn(reference);
Example
import { jest } from '@jest/globals';
type MyFunctionType = (input: string) => number;
const spy = jest.fn<MyFunctionType>>();
Pitch
Proper function mock types is going to be an increasingly relevant pain point as ES Modules become more popular. @types/jest
defaults the generics for jest.fn()
mocks to any
instead of unknown
, so many users haven’t needed to have proper test types till now.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Mock Functions - Jest
You can create a mock function with jest.fn(). If no implementation is given, the mock function will return undefined when invoked.
Read more >Why jest.fn() can be assigned to any function in Typescript?
From my understanding, myClass.myFunction has a type of '() => string' but jest.fn() returns a 'Mock'. I notice that 'Mock' is in fact ......
Read more >Documentation - More on Functions - TypeScript
Let's learn about how to write types that describe functions. Function Type Expressions. The simplest way to describe a function is with a...
Read more >Testing TypeScript apps using Jest - LogRocket Blog
Learn how to use the popular testing library, Jest, for your TypeScript ... For our project, we will focus on two types of...
Read more >TypeScript Testing Tips - Mocking Functions with Jest
Control how the function behaves during tests via methods like mockReturnValue and mockReturnValueOnce . Verify how your code interacted with ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
https://github.com/facebook/jest/releases/tag/v28.0.0-alpha.6
Working on it. Almost there. (Shape it up, just have to figure out a couple of details.)