Deep mocking results in a TypeScript error + extreme compilation slowness
See original GitHub issueUpgrading to v2.0.7
(see original PR) from v2.0.6
seems to have brought in something fishy when using mockDeep
. On a fresh install, TypeScript compilation is now ridiculously slow.
yarn tsc --noEmit
- before: 1.5-3.5s
- first compilation after upgrading to the new version: 200s (i.e. over 3 minutes)
- follow-up compilations on 2.0.7: once again <4s
TypeScript v4.7.4
, Node v16.14.2
, jest v28.1.3
, ts-jest 28.0.7
, running on an M1 Pro chip
Reproducible example:
import { DeepMockProxy, mockDeep } from 'jest-mock-extended';
interface Thing {
foo: {
bar: () => void;
};
}
export const mockThing: DeepMockProxy<Thing> = mockDeep<Thing>();
Not only is this excruciatingly slow (but only the first time), it also complains with a type error:
node_modules/jest-mock-extended/lib/Mock.d.ts:27:222 - error TS2536: Type 'K_1' cannot be used to index type 'T[K]'.
27 export declare const mockDeep: <T>(mockImplementation?: DeepPartial<T> | undefined) => { [K in keyof T]: T[K] extends (...args: infer A) => infer B ? CalledWithMock<B, A> & (T[K] extends infer T_1 ? { [K_1 in keyof T_1]: T[K][K_1] extends (...args: infer A) => infer B ? CalledWithMock<B, A> & (T[K][K_1] extends infer T_2 ? { [K_2 in keyof T_2]: T[K][K_1][K_2] extends (...args: infer A) => infer B ? CalledWithMock<B, A> & (T[K][K_1][K_2] extends infer T_3 ? { [K_3 in keyof T_3]: T[K][K_1][K_2][K_3] extends (...args: infer A) => infer B ? CalledWithMock<B, A> & (T[K][K_1][K_2][K_3] extends infer T_4 ? { [K_4 in keyof T_4]: T[K][K_1][K_2][K_3][K_4] extends (...args: infer A) => infer B ? CalledWithMock<B, A> & (T[K][K_1][K_2][K_3][K_4] extends infer T_5 ? { [K_5 in keyof T_5]: T[K][K_1][K_2][K_3][K_4][K_5] extends (...args: infer A) => infer B ? CalledWithMock<B, A> & (T[K][K_1][K_2][K_3][K_4][K_5] extends infer T_6 ? { [K_6 in keyof T_6]: T[K][K_1][K_2][K_3][K_4][K_5][K_6] extends (...args: infer A) => infer B ? CalledWithMock<B, A> & (T[K][K_1][K_2][K_3][K_4][K_5][K_6] extends infer T_7 ? { [K_7 in keyof T_7]: T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] extends (...args: infer A) => infer B ? CalledWithMock<B, A> & (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] extends infer T_8 ? { [K_8 in keyof T_8]: T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] extends (...args: infer A) => infer B ? CalledWithMock<B, A> & (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] extends infer T_9 ? { [K_9 in keyof T_9]: T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] extends (...args: infer A) => infer B ? CalledWithMock<B, A> & (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] extends infer T_10 ? { [K_10 in keyof T_10]: T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10] extends (...args: infer A) => infer B ? CalledWithMock<B, A> & any & T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10] : DeepMockProxy<T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10]>; } : never) & T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] : DeepMockProxy<T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9]>; } : never) & T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] : DeepMockProxy<T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8]>; } : never) & T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] : DeepMockProxy<T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7]>; } : never) & T[K][K_1][K_2][K_3][K_4][K_5][K_6] : DeepMockProxy<T[K][K_1][K_2][K_3][K_4][K_5][K_6]>; } : never) & T[K][K_1][K_2][K_3][K_4][K_5] : DeepMockProxy<T[K][K_1][K_2][K_3][K_4][K_5]>; } : never) & T[K][K_1][K_2][K_3][K_4] : DeepMockProxy<T[K][K_1][K_2][K_3][K_4]>; } : never) & T[K][K_1][K_2][K_3] : DeepMockProxy<T[K][K_1][K_2][K_3]>; } : never) & T[K][K_1][K_2] : DeepMockProxy<T[K][K_1][K_2]>; } : never) & T[K][K_1] : DeepMockProxy<T[K][K_1]>; } : never) & T[K] : DeepMockProxy<T[K]>; } & T;
~~~~~~~~~
node_modules/jest-mock-extended/lib/Mock.d.ts:27:2781 - error TS2536: Type 'K_1' cannot be used to index type 'T[K]'.
27 export declare const mockDeep: <T>(mockImplementation?: DeepPartial<T> | undefined) => { [K in keyof T]: T[K] extends (...args: infer A) => infer B ? CalledWithMock<B, A> & (T[K] extends infer T_1 ? { [K_1 in keyof T_1]: T[K][K_1] extends (...args: infer A) => infer B ? CalledWithMock<B, A> & (T[K][K_1] extends infer T_2 ? { [K_2 in keyof T_2]: T[K][K_1][K_2] extends (...args: infer A) => infer B ? CalledWithMock<B, A> & (T[K][K_1][K_2] extends infer T_3 ? { [K_3 in keyof T_3]: T[K][K_1][K_2][K_3] extends (...args: infer A) => infer B ? CalledWithMock<B, A> & (T[K][K_1][K_2][K_3] extends infer T_4 ? { [K_4 in keyof T_4]: T[K][K_1][K_2][K_3][K_4] extends (...args: infer A) => infer B ? CalledWithMock<B, A> & (T[K][K_1][K_2][K_3][K_4] extends infer T_5 ? { [K_5 in keyof T_5]: T[K][K_1][K_2][K_3][K_4][K_5] extends (...args: infer A) => infer B ? CalledWithMock<B, A> & (T[K][K_1][K_2][K_3][K_4][K_5] extends infer T_6 ? { [K_6 in keyof T_6]: T[K][K_1][K_2][K_3][K_4][K_5][K_6] extends (...args: infer A) => infer B ? CalledWithMock<B, A> & (T[K][K_1][K_2][K_3][K_4][K_5][K_6] extends infer T_7 ? { [K_7 in keyof T_7]: T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] extends (...args: infer A) => infer B ? CalledWithMock<B, A> & (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] extends infer T_8 ? { [K_8 in keyof T_8]: T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] extends (...args: infer A) => infer B ? CalledWithMock<B, A> & (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] extends infer T_9 ? { [K_9 in keyof T_9]: T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] extends (...args: infer A) => infer B ? CalledWithMock<B, A> & (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] extends infer T_10 ? { [K_10 in keyof T_10]: T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10] extends (...args: infer A) => infer B ? CalledWithMock<B, A> & any & T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10] : DeepMockProxy<T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10]>; } : never) & T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] : DeepMockProxy<T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9]>; } : never) & T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] : DeepMockProxy<T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8]>; } : never) & T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] : DeepMockProxy<T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7]>; } : never) & T[K][K_1][K_2][K_3][K_4][K_5][K_6] : DeepMockProxy<T[K][K_1][K_2][K_3][K_4][K_5][K_6]>; } : never) & T[K][K_1][K_2][K_3][K_4][K_5] : DeepMockProxy<T[K][K_1][K_2][K_3][K_4][K_5]>; } : never) & T[K][K_1][K_2][K_3][K_4] : DeepMockProxy<T[K][K_1][K_2][K_3][K_4]>; } : never) & T[K][K_1][K_2][K_3] : DeepMockProxy<T[K][K_1][K_2][K_3]>; } : never) & T[K][K_1][K_2] : DeepMockProxy<T[K][K_1][K_2]>; } : never) & T[K][K_1] : DeepMockProxy<T[K][K_1]>; } : never) & T[K] : DeepMockProxy<T[K]>; } & T;
Issue Analytics
- State:
- Created a year ago
- Reactions:23
- Comments:10 (3 by maintainers)
Top Results From Across the Web
Typescript and Jest: Avoiding type errors on mocked functions
The mocked test helper provides typings on your mocked modules and even their deep methods, based on the typing of its source. It...
Read more >Safari Technology Preview Release Notes - Apple Developer
It marks both the free space and gaps between flex items to reveal how they affect the result. If you see bugs or...
Read more >Announcing TypeScript 4.4 - Microsoft Developer Blogs
Today we're excited to announce the availability of TypeScript 4.4! If you haven't heard of TypeScript yet, it's a language that builds on ......
Read more >Lots of Ways to Use Math.random() in JavaScript - CSS-Tricks
Math.random() is an API in JavaScript. It is a function that gives you a random number. The number returned will be between 0...
Read more >Typescript unit testing pitfalls with Jest - Salto
Jest has a really nice framework for creating mock functions for unit ... cause the typescript compiler to emit an error on line...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ll try and get a fix for this, without breaking the functionality that added to the slowness.
Fixed in 3.0.0