jest icon indicating copy to clipboard operation
jest copied to clipboard

fix(jest-mock): improved `MockedClass` type

Open k-rajat19 opened this issue 1 year ago • 7 comments

Summary

Fixes #14458

added constructor signature in MockedClass type so that it returns mocked Instance (of type Mocked<InstanceType<typeof original class>>) instead of returning the instance typed with original class type.

Test plan

Tests added

k-rajat19 avatar Jul 11 '24 11:07 k-rajat19

Deploy Preview for jestjs ready!

Built without sensitive environment variables

Name Link
Latest commit 3b171f13ab1577ad39e02162cf499932c201ad24
Latest deploy log https://app.netlify.com/sites/jestjs/deploys/66b0f05b1a208e00080fa4c2
Deploy Preview https://deploy-preview-15179--jestjs.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

netlify[bot] avatar Jul 11 '24 11:07 netlify[bot]

This is failing type tests in packages/jest-types/__typetests__/jest.test.ts

SimenB avatar Jul 12 '24 07:07 SimenB

I have run build command multiple times and also see the updated types in build folder,but still all type tests are passing in my local. After looking at CI failure I found that tests which are using Class.prototype are failing but they are supposed to be passed as MockedObject<T> will provide all typings for the prototype object in a Class.

export type MockedClass<T extends ClassLike> = MockInstance<
  (...args: ConstructorParameters<T>) => Mocked<InstanceType<T>>
 > &
  (new (...args: ConstructorParameters<T>) => Mocked<InstanceType<T>>) &
+ MockedObject<T>;

k-rajat19 avatar Jul 12 '24 07:07 k-rajat19

The type test is failing in CI when using TypeScript 5.0. Locally you can run yarn test-types --target 5.0 to see the same error.

This change is working without issues from Typescript 5.4 and up. You can check this with: yarn test-types --target 5.4,5.5

mrazauskas avatar Jul 12 '24 08:07 mrazauskas

An easy solution could be to set the lowest supported TypeScript version to 5.4. If @SimenB agrees (;

Otherwise the way forward would be make [email protected] work as well. If that helps, it is possible to use bisect from every-ts with TSTyche to find out which exact comment to TypeScript repo makes this implementation work. (Just for fun, I will try to do this a bit later. Too busy at the moment.)

mrazauskas avatar Jul 12 '24 08:07 mrazauskas

I'd be down with increasing to 5.2 or something if it'd help, but not 5.4

SimenB avatar Jul 12 '24 08:07 SimenB

tests are failing with 5.2, we can hold this PR till jest upgrades ts to 5.4

Otherwise the way forward would be make [email protected] work as well. If that helps, it is possible to use bisect from every-ts with TSTyche to find out which exact comment to TypeScript repo makes this implementation work. (Just for fun, I will try to do this a bit later. Too busy at the moment.)

Or may be try this ...

k-rajat19 avatar Jul 12 '24 08:07 k-rajat19

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[bot] avatar Nov 10 '24 00:11 github-actions[bot]