Skip to content

Commit 00e52eb

Browse files
thymikeeSimenB
authored andcommitted
chore: followup to jest-mock TS migration (#7850)
1 parent 100b033 commit 00e52eb

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
- `[@jest/types]`: New package to handle shared types ([#7834](https://github.com/facebook/jest/pull/7834))
2424
- `[jest-util]`: Migrate to TypeScript ([#7844](https://github.com/facebook/jest/pull/7844))
2525
- `[jest-watcher]`: Migrate to TypeScript ([#7843](https://github.com/facebook/jest/pull/7843))
26-
- `[jest-mock]`: Migrate to TypeScript ([#7847](https://github.com/facebook/jest/pull/7847))
26+
- `[jest-mock]`: Migrate to TypeScript ([#7847](https://github.com/facebook/jest/pull/7847), [#7850](https://github.com/facebook/jest/pull/7850))
2727

2828
### Performance
2929

packages/jest-mock/src/index.ts

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ type MockFunctionConfig = {
5353
specificMockImpls: Array<Function>;
5454
};
5555

56+
// see https://github.com/Microsoft/TypeScript/issues/25215
57+
type NonFunctionPropertyNames<T> = {
58+
[K in keyof T]: T[K] extends (...args: any[]) => any ? never : K
59+
}[keyof T] &
60+
string;
61+
type FunctionPropertyNames<T> = {
62+
[K in keyof T]: T[K] extends (...args: any[]) => any ? K : never
63+
}[keyof T] &
64+
string;
65+
5666
interface Mock<T, Y extends unknown[] = unknown[]>
5767
extends Function,
5868
MockInstance<T, Y> {
@@ -328,12 +338,12 @@ function isReadonlyProp(object: any, prop: string): boolean {
328338
}
329339

330340
class ModuleMockerClass {
331-
_environmentGlobal: Global;
332-
_mockState: WeakMap<Mock<any, any>, MockFunctionState<any, any>>;
333-
_mockConfigRegistry: WeakMap<Function, MockFunctionConfig>;
334-
_spyState: Set<() => void>;
341+
private _environmentGlobal: Global;
342+
private _mockState: WeakMap<Mock<any, any>, MockFunctionState<any, any>>;
343+
private _mockConfigRegistry: WeakMap<Function, MockFunctionConfig>;
344+
private _spyState: Set<() => void>;
345+
private _invocationCallCounter: number;
335346
ModuleMocker: typeof ModuleMockerClass;
336-
_invocationCallCounter: number;
337347

338348
/**
339349
* @see README.md
@@ -349,7 +359,7 @@ class ModuleMockerClass {
349359
this._invocationCallCounter = 1;
350360
}
351361

352-
_getSlots(object?: Object): Array<string> {
362+
private _getSlots(object?: Object): Array<string> {
353363
if (!object) {
354364
return [];
355365
}
@@ -396,7 +406,9 @@ class ModuleMockerClass {
396406
return Array.from(slots);
397407
}
398408

399-
_ensureMockConfig<T, Y extends unknown[]>(f: Mock<T, Y>): MockFunctionConfig {
409+
private _ensureMockConfig<T, Y extends unknown[]>(
410+
f: Mock<T, Y>,
411+
): MockFunctionConfig {
400412
let config = this._mockConfigRegistry.get(f);
401413
if (!config) {
402414
config = this._defaultMockConfig();
@@ -405,7 +417,7 @@ class ModuleMockerClass {
405417
return config;
406418
}
407419

408-
_ensureMockState<T, Y extends unknown[]>(
420+
private _ensureMockState<T, Y extends unknown[]>(
409421
f: Mock<T, Y>,
410422
): MockFunctionState<T, Y> {
411423
let state = this._mockState.get(f);
@@ -416,7 +428,7 @@ class ModuleMockerClass {
416428
return state;
417429
}
418430

419-
_defaultMockConfig(): MockFunctionConfig {
431+
private _defaultMockConfig(): MockFunctionConfig {
420432
return {
421433
defaultReturnValue: undefined,
422434
isReturnValueLastSet: false,
@@ -427,7 +439,7 @@ class ModuleMockerClass {
427439
};
428440
}
429441

430-
_defaultMockState<T, Y extends unknown[]>(): MockFunctionState<T, Y> {
442+
private _defaultMockState<T, Y extends unknown[]>(): MockFunctionState<T, Y> {
431443
return {
432444
calls: [],
433445
instances: [],
@@ -436,31 +448,31 @@ class ModuleMockerClass {
436448
};
437449
}
438450

439-
_makeComponent<T, Y extends unknown[]>(
451+
private _makeComponent<T, Y extends unknown[]>(
440452
metadata: Mocks.MockFunctionMetadata<T, Y, 'object'>,
441453
restore?: () => void,
442454
): Object;
443-
_makeComponent<T, Y extends unknown[]>(
455+
private _makeComponent<T, Y extends unknown[]>(
444456
metadata: Mocks.MockFunctionMetadata<T, Y, 'array'>,
445457
restore?: () => void,
446458
): Array<unknown>;
447-
_makeComponent<T, Y extends unknown[]>(
459+
private _makeComponent<T, Y extends unknown[]>(
448460
metadata: Mocks.MockFunctionMetadata<T, Y, 'regexp'>,
449461
restore?: () => void,
450462
): RegExp;
451-
_makeComponent<T, Y extends unknown[]>(
463+
private _makeComponent<T, Y extends unknown[]>(
452464
metadata: Mocks.MockFunctionMetadata<
453465
T,
454466
Y,
455467
'constant' | 'collection' | 'null' | 'undefined'
456468
>,
457469
restore?: () => void,
458470
): T;
459-
_makeComponent<T, Y extends unknown[]>(
471+
private _makeComponent<T, Y extends unknown[]>(
460472
metadata: Mocks.MockFunctionMetadata<T, Y, 'function'>,
461473
restore?: () => void,
462474
): Mock<T, Y>;
463-
_makeComponent<T, Y extends unknown[]>(
475+
private _makeComponent<T, Y extends unknown[]>(
464476
metadata: Mocks.MockFunctionMetadata<T, Y>,
465477
restore?: () => void,
466478
): Object | Array<unknown> | RegExp | T | undefined | Mock<T, Y> {
@@ -706,7 +718,7 @@ class ModuleMockerClass {
706718
}
707719
}
708720

709-
_createMockFunction<T, Y extends unknown[]>(
721+
private _createMockFunction<T, Y extends unknown[]>(
710722
metadata: Mocks.MockFunctionMetadata<T, Y>,
711723
mockConstructor: Function,
712724
): Function {
@@ -766,7 +778,7 @@ class ModuleMockerClass {
766778
return createConstructor(mockConstructor);
767779
}
768780

769-
_generateMock<T, Y extends unknown[]>(
781+
private _generateMock<T, Y extends unknown[]>(
770782
metadata: Mocks.MockFunctionMetadata<T, Y>,
771783
callbacks: Array<Function>,
772784
refs: {
@@ -899,7 +911,7 @@ class ModuleMockerClass {
899911
return metadata;
900912
}
901913

902-
isMockFunction(fn: any): boolean {
914+
isMockFunction<T>(fn: any): fn is Mock<T> {
903915
return !!fn && fn._isMockFunction === true;
904916
}
905917

@@ -912,26 +924,26 @@ class ModuleMockerClass {
912924
return fn;
913925
}
914926

915-
spyOn<T extends {}, M extends keyof T>(
927+
spyOn<T extends {}, M extends NonFunctionPropertyNames<T>>(
916928
object: T,
917929
methodName: M,
918930
accessType: 'get',
919931
): SpyInstance<T[M], []>;
920932

921-
spyOn<T extends {}, M extends keyof T>(
933+
spyOn<T extends {}, M extends NonFunctionPropertyNames<T>>(
922934
object: T,
923935
methodName: M,
924936
accessType: 'set',
925937
): SpyInstance<void, [T[M]]>;
926938

927-
spyOn<T extends {}, M extends keyof T>(
939+
spyOn<T extends {}, M extends FunctionPropertyNames<T>>(
928940
object: T,
929941
methodName: M,
930942
): T[M] extends (...args: any[]) => any
931943
? SpyInstance<ReturnType<T[M]>, ArgsType<T[M]>>
932944
: never;
933945

934-
spyOn<T extends {}, M extends keyof T>(
946+
spyOn<T extends {}, M extends NonFunctionPropertyNames<T>>(
935947
object: T,
936948
methodName: M,
937949
accessType?: 'get' | 'set',
@@ -973,7 +985,7 @@ class ModuleMockerClass {
973985
return object[methodName];
974986
}
975987

976-
_spyOnProperty<T extends {}, M extends keyof T>(
988+
private _spyOnProperty<T extends {}, M extends NonFunctionPropertyNames<T>>(
977989
obj: T,
978990
propertyName: M,
979991
accessType: 'get' | 'set' = 'get',
@@ -1060,7 +1072,7 @@ class ModuleMockerClass {
10601072
this._spyState = new Set();
10611073
}
10621074

1063-
_typeOf(value: any): string {
1075+
private _typeOf(value: any): string {
10641076
return value == null ? '' + value : typeof value;
10651077
}
10661078
}

0 commit comments

Comments
 (0)