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

chore: followup to jest-mock TS migration #7850

Merged
merged 4 commits into from
Feb 10, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
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
58 changes: 35 additions & 23 deletions packages/jest-mock/src/index.ts
Original file line number Diff line number Diff line change
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 @@ -912,19 +924,19 @@ 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
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 keyof T>(
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
private _spyOnProperty<T extends {}, M extends keyof T>(
private _spyOnProperty<T extends {}, M extends NonFunctionPropertyNames<T>>(

?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This causes TS to choke :( Wanna have a look locally?

Copy link
Member

Choose a reason for hiding this comment

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

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