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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Export MockedObject & similar utility types? #12784

Closed
JoshuaKGoldberg opened this issue Apr 30, 2022 · 10 comments
Closed

[Feature]: Export MockedObject & similar utility types? #12784

JoshuaKGoldberg opened this issue Apr 30, 2022 · 10 comments

Comments

@JoshuaKGoldberg
Copy link
Contributor

JoshuaKGoldberg commented Apr 30, 2022

🚀 Feature Proposal

Could TypeScript types such as MockedObject please be exported from some package that consumers would have access to while using the @jest/globals package?

Motivation

It'd be convenient for declaring types of objects that contain mocks. Right now there's now easy way to declare in the type system that an object is a MockedObject<T> for any T object type.

Example

In typescript-eslint/tslint-to-eslint-config#1458 I'm trying to get the TypeScript types to work for a createStubWritableStream function that returns a mocked version of a NodeJS.WritableStream. Here's a minimum reproduction in the TypeScript playground.

Pitch

MockedObject isn't exported in any way I can find -nor documented on the website-. There's no way to declare the return type of createStubWritableStream -or variables that store that value type- to be MockedObject<NodeJS.WritableStream>.

Even if I'm using jest.fn() and/or the types wrong, being able to refer to MockedObject<...> is quite useful for debugging type system woes such as this one.

@mrazauskas
Copy link
Contributor

Did you check these from jest-mock:

https://github.com/facebook/jest/blob/49393d01cdda7dfe75718aa1a6586210fa197c72/packages/jest-mock/src/index.ts#L77-L87

Documentation is missing, but seems to be exposed. Or these aren’t exactly what you need?

@JoshuaKGoldberg
Copy link
Contributor Author

JoshuaKGoldberg commented Apr 30, 2022

I think those are exactly what I need, yes and thank you! +1 to documentation existing -- I'd assumed jest-mock was an internal package not meant for consumers.

@mrazauskas
Copy link
Contributor

Current I work on #12727. It will add Mocked utility type to Jest object (similar to jest.Mocked from @types/jest) for those who import {jest} from '@jest/globals'. Documentation will be part of that PR, of course.

@SimenB
Copy link
Member

SimenB commented May 1, 2022

Documenting that all the mock types are available from jest-mock seems reasonable. Not sure putting all of them on jest makes sense, but making sure they are consumable from jest-mock sounds good

@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label May 31, 2022
@alesmenzel
Copy link
Contributor

@SimenB From the user's perspective it is very hard to discover what types are available in which internal jest-[whatever] package because you as a user of jest don't even know those packages like jest-mock are installed because they are not a peer dependency, but rather a direct dependency. I would like jest to reexport all TS types in the main jest package so it is easy to look for/import those types rather than scanning through jest's overwhelming number of dependencies.

Screenshot 2022-06-22 at 18 10 14

Screenshot 2022-06-22 at 18 10 41

@github-actions github-actions bot removed the Stale label Jun 22, 2022
@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Jul 22, 2022
@SimenB SimenB added Pinned and removed Stale labels Aug 11, 2022
@mrazauskas
Copy link
Contributor

mrazauskas commented Aug 13, 2022

From #12856 (comment)

#12727 landed recently adding refreshed (see #13123) utility types jest.Mocked, jest.MockedClass, jest.MockedFunction and jest.MockedObject. Will be released with Jest 29. Simply import {jest} from '@jest/globals' and use them. Docs: https://jestjs.io/docs/next/mock-function-api#jestmockedsource

@JoshuaKGoldberg @alesmenzel Just wanted to ask – are there any other types you would find useful? (Or any use cases where typing becomes cumbersome?)

@JoshuaKGoldberg
Copy link
Contributor Author

This looks like all I'd need for now, thank you very much!

Copy link

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 Mar 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants