question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[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>>();

https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgKwKYGd4F84DMoQhwDkAAmpgPQDmANhAEYCGt6xA3AFCcwCeYqOAFleAMQCuAOwDGMYBEkAVfoIC8cABTBJYcTABccTFG3UAlHFUA+OJPEgGqKF07SFmI2F6WUGGADpcSQAeACVUGHEoJRVgkQkZOQVlASsAGjgABSYoJhAIp3Q4sSlZeRjUqw0zLiA

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:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
mrazauskascommented, Feb 24, 2022

Working on it. Almost there. (Shape it up, just have to figure out a couple of details.)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found