Skip to content

Commit

Permalink
chore: followup to jest-mock TS migration (#7850)
Browse files Browse the repository at this point in the history
  • Loading branch information
thymikee authored and SimenB committed Feb 10, 2019
1 parent 100b033 commit 00e52eb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -23,7 +23,7 @@
- `[@jest/types]`: New package to handle shared types ([#7834](https://github.com/facebook/jest/pull/7834))
- `[jest-util]`: Migrate to TypeScript ([#7844](https://github.com/facebook/jest/pull/7844))
- `[jest-watcher]`: Migrate to TypeScript ([#7843](https://github.com/facebook/jest/pull/7843))
- `[jest-mock]`: Migrate to TypeScript ([#7847](https://github.com/facebook/jest/pull/7847))
- `[jest-mock]`: Migrate to TypeScript ([#7847](https://github.com/facebook/jest/pull/7847), [#7850](https://github.com/facebook/jest/pull/7850))

### Performance

Expand Down
62 changes: 37 additions & 25 deletions packages/jest-mock/src/index.ts
Expand Up @@ -53,6 +53,16 @@ type MockFunctionConfig = {
specificMockImpls: Array<Function>;
};

// see https://github.com/Microsoft/TypeScript/issues/25215
type NonFunctionPropertyNames<T> = {
[K in keyof T]: T[K] extends (...args: any[]) => any ? never : K
}[keyof T] &
string;
type FunctionPropertyNames<T> = {
[K in keyof T]: T[K] extends (...args: any[]) => any ? K : never
}[keyof T] &
string;

interface Mock<T, Y extends unknown[] = unknown[]>
extends Function,
MockInstance<T, Y> {
Expand Down Expand Up @@ -328,12 +338,12 @@ function isReadonlyProp(object: any, prop: string): boolean {
}

class ModuleMockerClass {
_environmentGlobal: Global;
_mockState: WeakMap<Mock<any, any>, MockFunctionState<any, any>>;
_mockConfigRegistry: WeakMap<Function, MockFunctionConfig>;
_spyState: Set<() => void>;
private _environmentGlobal: Global;
private _mockState: WeakMap<Mock<any, any>, MockFunctionState<any, any>>;
private _mockConfigRegistry: WeakMap<Function, MockFunctionConfig>;
private _spyState: Set<() => void>;
private _invocationCallCounter: number;
ModuleMocker: typeof ModuleMockerClass;
_invocationCallCounter: number;

/**
* @see README.md
Expand All @@ -349,7 +359,7 @@ class ModuleMockerClass {
this._invocationCallCounter = 1;
}

_getSlots(object?: Object): Array<string> {
private _getSlots(object?: Object): Array<string> {
if (!object) {
return [];
}
Expand Down Expand Up @@ -396,7 +406,9 @@ class ModuleMockerClass {
return Array.from(slots);
}

_ensureMockConfig<T, Y extends unknown[]>(f: Mock<T, Y>): MockFunctionConfig {
private _ensureMockConfig<T, Y extends unknown[]>(
f: Mock<T, Y>,
): MockFunctionConfig {
let config = this._mockConfigRegistry.get(f);
if (!config) {
config = this._defaultMockConfig();
Expand All @@ -405,7 +417,7 @@ class ModuleMockerClass {
return config;
}

_ensureMockState<T, Y extends unknown[]>(
private _ensureMockState<T, Y extends unknown[]>(
f: Mock<T, Y>,
): MockFunctionState<T, Y> {
let state = this._mockState.get(f);
Expand All @@ -416,7 +428,7 @@ class ModuleMockerClass {
return state;
}

_defaultMockConfig(): MockFunctionConfig {
private _defaultMockConfig(): MockFunctionConfig {
return {
defaultReturnValue: undefined,
isReturnValueLastSet: false,
Expand All @@ -427,7 +439,7 @@ class ModuleMockerClass {
};
}

_defaultMockState<T, Y extends unknown[]>(): MockFunctionState<T, Y> {
private _defaultMockState<T, Y extends unknown[]>(): MockFunctionState<T, Y> {
return {
calls: [],
instances: [],
Expand All @@ -436,31 +448,31 @@ class ModuleMockerClass {
};
}

_makeComponent<T, Y extends unknown[]>(
private _makeComponent<T, Y extends unknown[]>(
metadata: Mocks.MockFunctionMetadata<T, Y, 'object'>,
restore?: () => void,
): Object;
_makeComponent<T, Y extends unknown[]>(
private _makeComponent<T, Y extends unknown[]>(
metadata: Mocks.MockFunctionMetadata<T, Y, 'array'>,
restore?: () => void,
): Array<unknown>;
_makeComponent<T, Y extends unknown[]>(
private _makeComponent<T, Y extends unknown[]>(
metadata: Mocks.MockFunctionMetadata<T, Y, 'regexp'>,
restore?: () => void,
): RegExp;
_makeComponent<T, Y extends unknown[]>(
private _makeComponent<T, Y extends unknown[]>(
metadata: Mocks.MockFunctionMetadata<
T,
Y,
'constant' | 'collection' | 'null' | 'undefined'
>,
restore?: () => void,
): T;
_makeComponent<T, Y extends unknown[]>(
private _makeComponent<T, Y extends unknown[]>(
metadata: Mocks.MockFunctionMetadata<T, Y, 'function'>,
restore?: () => void,
): Mock<T, Y>;
_makeComponent<T, Y extends unknown[]>(
private _makeComponent<T, Y extends unknown[]>(
metadata: Mocks.MockFunctionMetadata<T, Y>,
restore?: () => void,
): Object | Array<unknown> | RegExp | T | undefined | Mock<T, Y> {
Expand Down Expand Up @@ -706,7 +718,7 @@ class ModuleMockerClass {
}
}

_createMockFunction<T, Y extends unknown[]>(
private _createMockFunction<T, Y extends unknown[]>(
metadata: Mocks.MockFunctionMetadata<T, Y>,
mockConstructor: Function,
): Function {
Expand Down Expand Up @@ -766,7 +778,7 @@ class ModuleMockerClass {
return createConstructor(mockConstructor);
}

_generateMock<T, Y extends unknown[]>(
private _generateMock<T, Y extends unknown[]>(
metadata: Mocks.MockFunctionMetadata<T, Y>,
callbacks: Array<Function>,
refs: {
Expand Down Expand Up @@ -899,7 +911,7 @@ class ModuleMockerClass {
return metadata;
}

isMockFunction(fn: any): boolean {
isMockFunction<T>(fn: any): fn is Mock<T> {
return !!fn && fn._isMockFunction === true;
}

Expand All @@ -912,26 +924,26 @@ class ModuleMockerClass {
return fn;
}

spyOn<T extends {}, M extends keyof T>(
spyOn<T extends {}, M extends NonFunctionPropertyNames<T>>(
object: T,
methodName: M,
accessType: 'get',
): SpyInstance<T[M], []>;

spyOn<T extends {}, M extends keyof T>(
spyOn<T extends {}, M extends NonFunctionPropertyNames<T>>(
object: T,
methodName: M,
accessType: 'set',
): SpyInstance<void, [T[M]]>;

spyOn<T extends {}, M extends keyof T>(
spyOn<T extends {}, M extends FunctionPropertyNames<T>>(
object: T,
methodName: M,
): T[M] extends (...args: any[]) => any
? SpyInstance<ReturnType<T[M]>, ArgsType<T[M]>>
: never;

spyOn<T extends {}, M extends keyof T>(
spyOn<T extends {}, M extends NonFunctionPropertyNames<T>>(
object: T,
methodName: M,
accessType?: 'get' | 'set',
Expand Down Expand Up @@ -973,7 +985,7 @@ class ModuleMockerClass {
return object[methodName];
}

_spyOnProperty<T extends {}, M extends keyof T>(
private _spyOnProperty<T extends {}, M extends NonFunctionPropertyNames<T>>(
obj: T,
propertyName: M,
accessType: 'get' | 'set' = 'get',
Expand Down Expand Up @@ -1060,7 +1072,7 @@ class ModuleMockerClass {
this._spyState = new Set();
}

_typeOf(value: any): string {
private _typeOf(value: any): string {
return value == null ? '' + value : typeof value;
}
}
Expand Down

0 comments on commit 00e52eb

Please sign in to comment.