Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Ergonomic way to jest.fn over a function type in TypeScript #12479

Closed
JoshuaKGoldberg opened this issue Feb 23, 2022 · 6 comments 路 Fixed by #12489
Closed

[Feature]: Ergonomic way to jest.fn over a function type in TypeScript #12479

JoshuaKGoldberg opened this issue Feb 23, 2022 · 6 comments 路 Fixed by #12489

Comments

@JoshuaKGoldberg
Copy link
Contributor

JoshuaKGoldberg commented Feb 23, 2022

馃殌 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.

@SimenB
Copy link
Member

SimenB commented Feb 23, 2022

We should definitely have that, at the very least as an overload.

@mrazauskas since you've been working on the types in jest-mock the last few days, thoughts?

@mrazauskas
Copy link
Contributor

Indeed I was looking at this yesterday. Some hour before the issue came in. Might be possible to do and really worth to a try.

@SimenB
Copy link
Member

SimenB commented Feb 24, 2022

FWIW I'm happy to replace the two generics we have today with a single function one - the types we have are inherited from @types/jest, not something we put a lot of thought into at the time

@mrazauskas
Copy link
Contributor

mrazauskas commented Feb 24, 2022

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

@SimenB
Copy link
Member

SimenB commented Mar 1, 2022

https://github.com/facebook/jest/releases/tag/v28.0.0-alpha.6

@github-actions
Copy link

github-actions bot commented Apr 1, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants