chore: migrate jest-mock to TypeScript#7847
Conversation
f32c1b8 to
53f040c
Compare
|
|
||
| descriptor[accessType] = this._makeComponent({type: 'function'}, () => { | ||
| // $FlowFixMe | ||
| if (!descriptor) { |
There was a problem hiding this comment.
we're in a closure so technically this can be altered, but will it really?
There was a problem hiding this comment.
I'm fine keeping the ignores - I think people would have complained by now if this was actually an issue?
| let members: {[key: string]: MockFunctionMetadata<T, Y>} | null = null; | ||
| // Leave arrays alone | ||
| if (type !== 'array') { | ||
| if (type !== 'undefined') { |
There was a problem hiding this comment.
we check and bail on 'undefined' earlier so TS told me I can remove it
SimenB
left a comment
There was a problem hiding this comment.
Wooo, this is awesome! 😀
the module.exports at the bottom must be just export so TS generates the correct typings.
Also, please add types to package.json
a7fe846 to
d081e21
Compare
I didn't want to do that, because it changes public API of |
d081e21 to
bab6b97
Compare
Codecov Report
@@ Coverage Diff @@
## master #7847 +/- ##
==========================================
- Coverage 63.63% 62.66% -0.98%
==========================================
Files 201 200 -1
Lines 7922 7590 -332
Branches 6 5 -1
==========================================
- Hits 5041 4756 -285
+ Misses 2879 2832 -47
Partials 2 2Continue to review full report at Codecov.
|
| mockClear(): void; | ||
| mockReset(): void; | ||
| mockRestore(): void; | ||
| mockImplementation(fn: (...args: Y) => T): Mock<T, Y>; |
There was a problem hiding this comment.
I think we can make these return types this to be more accurate (and shorter)?
| // @ts-ignore TODO: figure out better typings here | ||
| this._moduleMocker.fn().mockImplementation(impl); | ||
|
|
||
| // TODO: add better typings; these are mocks, but typed as regular timers |
There was a problem hiding this comment.
the two comments are connected. cc @SimenB
There was a problem hiding this comment.
I wanna remove the autospying anyways (I've removed it in the Lolex implementation)
There was a problem hiding this comment.
However, we should be able to infer the type of the function passed in (can do later)
|
Thoughts on making stuff private: diff --git c/packages/jest-mock/src/index.ts w/packages/jest-mock/src/index.ts
index 9e6450e09..b118e9694 100644
--- c/packages/jest-mock/src/index.ts
+++ w/packages/jest-mock/src/index.ts
@@ -328,12 +328,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>;
ModuleMocker: typeof ModuleMockerClass;
- _invocationCallCounter: number;
+ private _invocationCallCounter: number;
/**
* @see README.md
@@ -349,7 +349,7 @@ class ModuleMockerClass {
this._invocationCallCounter = 1;
}
- _getSlots(object?: Object): Array<string> {
+ private _getSlots(object?: Object): Array<string> {
if (!object) {
return [];
}
@@ -396,7 +396,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();
@@ -405,7 +407,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);
@@ -416,7 +418,7 @@ class ModuleMockerClass {
return state;
}
- _defaultMockConfig(): MockFunctionConfig {
+ private _defaultMockConfig(): MockFunctionConfig {
return {
defaultReturnValue: undefined,
isReturnValueLastSet: false,
@@ -427,7 +429,7 @@ class ModuleMockerClass {
};
}
- _defaultMockState<T, Y extends unknown[]>(): MockFunctionState<T, Y> {
+ private _defaultMockState<T, Y extends unknown[]>(): MockFunctionState<T, Y> {
return {
calls: [],
instances: [],
@@ -436,19 +438,19 @@ 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,
@@ -456,11 +458,11 @@ class ModuleMockerClass {
>,
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> {
@@ -706,7 +708,7 @@ class ModuleMockerClass {
}
}
- _createMockFunction<T, Y extends unknown[]>(
+ private _createMockFunction<T, Y extends unknown[]>(
metadata: Mocks.MockFunctionMetadata<T, Y>,
mockConstructor: Function,
): Function {
@@ -766,7 +768,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: {
@@ -973,7 +975,7 @@ class ModuleMockerClass {
return object[methodName];
}
- _spyOnProperty<T extends {}, M extends keyof T>(
+ private _spyOnProperty<T extends {}, M extends keyof T>(
obj: T,
propertyName: M,
accessType: 'get' | 'set' = 'get',
@@ -1060,7 +1062,7 @@ class ModuleMockerClass {
this._spyState = new Set();
}
- _typeOf(value: any): string {
+ private _typeOf(value: any): string {
return value == null ? '' + value : typeof value;
}
}Results in this definition file: diff --git i/packages/jest-mock/build/index.d.ts w/packages/jest-mock/build/index.d.ts
index e6ae9a0cb..23cb93a44 100644
--- i/packages/jest-mock/build/index.d.ts
+++ w/packages/jest-mock/build/index.d.ts
@@ -39,14 +39,6 @@ declare type MockFunctionState<T, Y extends unknown[]> = {
*/
results: Array<MockFunctionResult>;
};
-declare type MockFunctionConfig = {
- isReturnValueLastSet: boolean;
- defaultReturnValue: unknown;
- mockImpl: Function | undefined;
- mockName: string;
- specificReturnValues: Array<unknown>;
- specificMockImpls: Array<Function>;
-};
interface Mock<T, Y extends unknown[] = unknown[]> extends Function, MockInstance<T, Y> {
new (...args: Y): T;
(...args: Y): T;
@@ -76,32 +68,26 @@ interface MockInstance<T, Y extends unknown[]> {
mockRejectedValueOnce(value: T): this;
}
declare class ModuleMockerClass {
- _environmentGlobal: Global;
- _mockState: WeakMap<Mock<any, any>, MockFunctionState<any, any>>;
- _mockConfigRegistry: WeakMap<Function, MockFunctionConfig>;
- _spyState: Set<() => void>;
+ private _environmentGlobal;
+ private _mockState;
+ private _mockConfigRegistry;
+ private _spyState;
ModuleMocker: typeof ModuleMockerClass;
- _invocationCallCounter: number;
+ private _invocationCallCounter;
/**
* @see README.md
* @param global Global object of the test environment, used to create
* mocks
*/
constructor(global: Global);
- _getSlots(object?: Object): Array<string>;
- _ensureMockConfig<T, Y extends unknown[]>(f: Mock<T, Y>): MockFunctionConfig;
- _ensureMockState<T, Y extends unknown[]>(f: Mock<T, Y>): MockFunctionState<T, Y>;
- _defaultMockConfig(): MockFunctionConfig;
- _defaultMockState<T, Y extends unknown[]>(): MockFunctionState<T, Y>;
- _makeComponent<T, Y extends unknown[]>(metadata: Mocks.MockFunctionMetadata<T, Y, 'object'>, restore?: () => void): Object;
- _makeComponent<T, Y extends unknown[]>(metadata: Mocks.MockFunctionMetadata<T, Y, 'array'>, restore?: () => void): Array<unknown>;
- _makeComponent<T, Y extends unknown[]>(metadata: Mocks.MockFunctionMetadata<T, Y, 'regexp'>, restore?: () => void): RegExp;
- _makeComponent<T, Y extends unknown[]>(metadata: Mocks.MockFunctionMetadata<T, Y, 'constant' | 'collection' | 'null' | 'undefined'>, restore?: () => void): T;
- _makeComponent<T, Y extends unknown[]>(metadata: Mocks.MockFunctionMetadata<T, Y, 'function'>, restore?: () => void): Mock<T, Y>;
- _createMockFunction<T, Y extends unknown[]>(metadata: Mocks.MockFunctionMetadata<T, Y>, mockConstructor: Function): Function;
- _generateMock<T, Y extends unknown[]>(metadata: Mocks.MockFunctionMetadata<T, Y>, callbacks: Array<Function>, refs: {
- [key: string]: Object | Array<unknown> | RegExp | T | undefined | Mock<T, Y>;
- }): Mock<T, Y>;
+ private _getSlots;
+ private _ensureMockConfig;
+ private _ensureMockState;
+ private _defaultMockConfig;
+ private _defaultMockState;
+ private _makeComponent;
+ private _createMockFunction;
+ private _generateMock;
/**
* @see README.md
* @param _metadata Metadata for the mock in the schema returned by the
@@ -118,11 +104,11 @@ declare class ModuleMockerClass {
spyOn<T extends {}, M extends keyof T>(object: T, methodName: M, accessType: 'get'): SpyInstance<T[M], []>;
spyOn<T extends {}, M extends keyof T>(object: T, methodName: M, accessType: 'set'): SpyInstance<void, [T[M]]>;
spyOn<T extends {}, M extends keyof T>(object: T, methodName: M): T[M] extends (...args: any[]) => any ? SpyInstance<ReturnType<T[M]>, ArgsType<T[M]>> : never;
- _spyOnProperty<T extends {}, M extends keyof T>(obj: T, propertyName: M, accessType?: 'get' | 'set'): Mock<T>;
+ private _spyOnProperty;
clearAllMocks(): void;
resetAllMocks(): void;
restoreAllMocks(): void;
- _typeOf(value: any): string;
+ private _typeOf;
}
declare const _default: ModuleMockerClass;
export = _default; |
|
I didn't want to make this diff any bigger but this should look like you proposed, way better |
|
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. |
Summary
build/index.js
Details
build-es5/index.js
Details
Test plan
Green CI