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

feat(@jest/environment, jest-runtime): allow requireActual and requireMock to take a type argument #13253

Merged
merged 3 commits into from
Sep 13, 2022
Merged

feat(@jest/environment, jest-runtime): allow requireActual and requireMock to take a type argument #13253

merged 3 commits into from
Sep 13, 2022

Conversation

mrazauskas
Copy link
Contributor

@mrazauskas mrazauskas commented Sep 13, 2022

From #13199 (comment)

Summary

Currently jest.requireActual and jest.requireMock return unknown. It is possible to use as typeof import('../someModule') to cast the return value. At the same time, it is somewhat nicer to do this via generic type argument:

const original = jest.requireActual<typeof import('../someModule')>('../someModule');

Test plan

Type tests are added.

Comment on lines -2132 to +2133
requireActual: this.requireActual.bind(this, from),
requireMock: this.requireMock.bind(this, from),
requireActual: moduleName => this.requireActual(from, moduleName),
requireMock: moduleName => this.requireMock(from, moduleName),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this fine? Not sure I understand what bind does here. Rework it, because I couldn’t find a clean way to return T through bind. Only work around was though casting, but this way it looked cleaner. All test are passing locally, let’s see what CI thinks.

Comment on lines -429 to +427
getRandom: jest.fn().mockReturnValue(10),
getRandom: jest.fn(() => 10),
Copy link
Contributor Author

@mrazauskas mrazauskas Sep 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way it doesn’t require additional typings in TS example. Less noise.

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@SimenB
Copy link
Member

SimenB commented Sep 28, 2022

@github-actions
Copy link

This pull request 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 Oct 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants