-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
refactor(jest-mock)!: rework Mocked*
utility types
#13123
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4c07ee5
fix(jest-mock): improve `jest.mocked()` types
mrazauskas c3ce6c8
add changelog record
mrazauskas 0858ba4
upgrading guide
mrazauskas 5bb6ef4
rebase
mrazauskas ca799d8
fix changelog
mrazauskas a9ca370
fix type test
mrazauskas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {expectAssignable, expectError, expectType} from 'tsd-lite'; | ||
import type {MockInstance, Mocked} from 'jest-mock'; | ||
|
||
/// mocks class | ||
|
||
class SomeClass { | ||
constructor(one: string, two?: boolean) {} | ||
|
||
methodA() { | ||
return true; | ||
} | ||
methodB(a: string, b?: number) { | ||
return; | ||
} | ||
} | ||
|
||
const MockSomeClass = SomeClass as Mocked<typeof SomeClass>; | ||
|
||
expectType<[one: string, two?: boolean]>(MockSomeClass.mock.calls[0]); | ||
|
||
expectType<[]>(MockSomeClass.prototype.methodA.mock.calls[0]); | ||
expectType<[a: string, b?: number]>( | ||
MockSomeClass.prototype.methodB.mock.calls[0], | ||
); | ||
|
||
expectError(MockSomeClass.prototype.methodA.mockReturnValue('true')); | ||
expectError( | ||
MockSomeClass.prototype.methodB.mockImplementation( | ||
(a: string, b?: string) => { | ||
return; | ||
}, | ||
), | ||
); | ||
|
||
expectType<[]>(MockSomeClass.mock.instances[0].methodA.mock.calls[0]); | ||
expectType<[a: string, b?: number]>( | ||
MockSomeClass.prototype.methodB.mock.calls[0], | ||
); | ||
|
||
const mockSomeInstance = new MockSomeClass('a') as Mocked< | ||
InstanceType<typeof MockSomeClass> | ||
>; | ||
|
||
expectType<[]>(mockSomeInstance.methodA.mock.calls[0]); | ||
expectType<[a: string, b?: number]>(mockSomeInstance.methodB.mock.calls[0]); | ||
|
||
expectError(mockSomeInstance.methodA.mockReturnValue('true')); | ||
expectError( | ||
mockSomeInstance.methodB.mockImplementation((a: string, b?: string) => { | ||
return; | ||
}), | ||
); | ||
|
||
expectAssignable<SomeClass>(mockSomeInstance); | ||
|
||
// mocks function | ||
|
||
function someFunction(a: string, b?: number): boolean { | ||
return true; | ||
} | ||
|
||
const mockFunction = someFunction as Mocked<typeof someFunction>; | ||
|
||
expectType<[a: string, b?: number]>(mockFunction.mock.calls[0]); | ||
|
||
expectError(mockFunction.mockReturnValue(123)); | ||
expectError(mockFunction.mockImplementation((a: boolean, b?: number) => true)); | ||
|
||
expectAssignable<typeof someFunction>(mockFunction); | ||
|
||
// mocks async function | ||
|
||
async function someAsyncFunction(a: Array<boolean>): Promise<string> { | ||
return 'true'; | ||
} | ||
|
||
const mockAsyncFunction = someAsyncFunction as Mocked<typeof someAsyncFunction>; | ||
|
||
expectType<[Array<boolean>]>(mockAsyncFunction.mock.calls[0]); | ||
|
||
expectError(mockAsyncFunction.mockResolvedValue(123)); | ||
expectError( | ||
mockAsyncFunction.mockImplementation((a: Array<boolean>) => | ||
Promise.resolve(true), | ||
), | ||
); | ||
|
||
expectAssignable<typeof someAsyncFunction>(mockAsyncFunction); | ||
|
||
// mocks function object | ||
|
||
interface SomeFunctionObject { | ||
(a: number, b?: string): void; | ||
one: { | ||
(oneA: number, oneB?: boolean): boolean; | ||
more: { | ||
time: { | ||
(time: number): void; | ||
}; | ||
}; | ||
}; | ||
} | ||
|
||
declare const someFunctionObject: SomeFunctionObject; | ||
|
||
const mockFunctionObject = someFunctionObject as Mocked< | ||
typeof someFunctionObject | ||
>; | ||
|
||
expectType<[a: number, b?: string]>(mockFunctionObject.mock.calls[0]); | ||
|
||
expectError(mockFunctionObject.mockReturnValue(123)); | ||
expectError(mockFunctionObject.mockImplementation(() => true)); | ||
|
||
expectType<[time: number]>(mockFunctionObject.one.more.time.mock.calls[0]); | ||
|
||
expectError(mockFunctionObject.one.more.time.mockReturnValue(123)); | ||
expectError( | ||
mockFunctionObject.one.more.time.mockImplementation((time: string) => { | ||
return; | ||
}), | ||
); | ||
|
||
expectAssignable<typeof someFunctionObject>(mockFunctionObject); | ||
|
||
// mocks object | ||
|
||
const someObject = { | ||
SomeClass, | ||
|
||
methodA() { | ||
return; | ||
}, | ||
methodB(b: string) { | ||
return true; | ||
}, | ||
methodC: (c: number) => true, | ||
|
||
one: { | ||
more: { | ||
time: (t: number) => { | ||
return; | ||
}, | ||
}, | ||
}, | ||
|
||
propertyA: 123, | ||
propertyB: 'value', | ||
|
||
someClassInstance: new SomeClass('value'), | ||
}; | ||
|
||
const mockObject = someObject as Mocked<typeof someObject>; | ||
|
||
expectType<[]>(mockObject.methodA.mock.calls[0]); | ||
expectType<[b: string]>(mockObject.methodB.mock.calls[0]); | ||
expectType<[c: number]>(mockObject.methodC.mock.calls[0]); | ||
|
||
expectType<[t: number]>(mockObject.one.more.time.mock.calls[0]); | ||
|
||
expectType<[one: string, two?: boolean]>(mockObject.SomeClass.mock.calls[0]); | ||
expectType<[]>(mockObject.SomeClass.prototype.methodA.mock.calls[0]); | ||
expectType<[a: string, b?: number]>( | ||
mockObject.SomeClass.prototype.methodB.mock.calls[0], | ||
); | ||
|
||
expectType<[]>(mockObject.someClassInstance.methodA.mock.calls[0]); | ||
expectType<[a: string, b?: number]>( | ||
mockObject.someClassInstance.methodB.mock.calls[0], | ||
); | ||
|
||
expectError(mockObject.methodA.mockReturnValue(123)); | ||
expectError(mockObject.methodA.mockImplementation((a: number) => 123)); | ||
expectError(mockObject.methodB.mockReturnValue(123)); | ||
expectError(mockObject.methodB.mockImplementation((b: number) => 123)); | ||
expectError(mockObject.methodC.mockReturnValue(123)); | ||
expectError(mockObject.methodC.mockImplementation((c: number) => 123)); | ||
|
||
expectError(mockObject.one.more.time.mockReturnValue(123)); | ||
expectError(mockObject.one.more.time.mockImplementation((t: boolean) => 123)); | ||
|
||
expectError(mockObject.SomeClass.prototype.methodA.mockReturnValue(123)); | ||
expectError( | ||
mockObject.SomeClass.prototype.methodA.mockImplementation((a: number) => 123), | ||
); | ||
expectError(mockObject.SomeClass.prototype.methodB.mockReturnValue(123)); | ||
expectError( | ||
mockObject.SomeClass.prototype.methodB.mockImplementation((a: number) => 123), | ||
); | ||
|
||
expectError(mockObject.someClassInstance.methodA.mockReturnValue(123)); | ||
expectError( | ||
mockObject.someClassInstance.methodA.mockImplementation((a: number) => 123), | ||
); | ||
expectError(mockObject.someClassInstance.methodB.mockReturnValue(123)); | ||
expectError( | ||
mockObject.someClassInstance.methodB.mockImplementation((a: number) => 123), | ||
); | ||
|
||
expectAssignable<typeof someObject>(mockObject); | ||
|
||
// mocks 'console' object | ||
|
||
const mockConsole = console as Mocked<typeof console>; | ||
|
||
expectAssignable<typeof console.log>( | ||
mockConsole.log.mockImplementation(() => {}), | ||
); | ||
expectAssignable<MockInstance<typeof console.log>>( | ||
mockConsole.log.mockImplementation(() => {}), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,68 +47,50 @@ export type PropertyLikeKeys<T> = Exclude< | |
ConstructorLikeKeys<T> | MethodLikeKeys<T> | ||
>; | ||
|
||
// TODO Figure out how to replace this with TS ConstructorParameters utility type | ||
// https://www.typescriptlang.org/docs/handbook/utility-types.html#constructorparameterstype | ||
type ConstructorParameters<T> = T extends new (...args: infer P) => any | ||
? P | ||
: never; | ||
|
||
export type MaybeMockedConstructor<T> = T extends new ( | ||
...args: Array<any> | ||
) => infer R | ||
? MockInstance<(...args: ConstructorParameters<T>) => R> | ||
: T; | ||
|
||
export interface MockWithArgs<T extends FunctionLike> extends MockInstance<T> { | ||
new (...args: ConstructorParameters<T>): T; | ||
(...args: Parameters<T>): ReturnType<T>; | ||
} | ||
export type MockedClass<T extends ClassLike> = MockInstance< | ||
(...args: ConstructorParameters<T>) => Mocked<InstanceType<T>> | ||
> & | ||
MockedObject<T>; | ||
|
||
export type MockedFunction<T extends FunctionLike> = MockWithArgs<T> & { | ||
[K in keyof T]: T[K]; | ||
}; | ||
export type MockedFunction<T extends FunctionLike> = MockInstance<T> & | ||
MockedObject<T>; | ||
|
||
export type MockedFunctionDeep<T extends FunctionLike> = MockWithArgs<T> & | ||
MockedObjectDeep<T>; | ||
type MockedFunctionShallow<T extends FunctionLike> = MockInstance<T> & T; | ||
|
||
export type MockedObject<T> = MaybeMockedConstructor<T> & { | ||
[K in MethodLikeKeys<T>]: T[K] extends FunctionLike | ||
export type MockedObject<T extends object> = { | ||
[K in keyof T]: T[K] extends ClassLike | ||
? MockedClass<T[K]> | ||
: T[K] extends FunctionLike | ||
? MockedFunction<T[K]> | ||
: T[K] extends object | ||
? MockedObject<T[K]> | ||
: T[K]; | ||
} & {[K in PropertyLikeKeys<T>]: T[K]}; | ||
} & T; | ||
|
||
export type MockedObjectDeep<T> = MaybeMockedConstructor<T> & { | ||
[K in MethodLikeKeys<T>]: T[K] extends FunctionLike | ||
? MockedFunctionDeep<T[K]> | ||
type MockedObjectShallow<T extends object> = { | ||
[K in keyof T]: T[K] extends ClassLike | ||
? MockedClass<T[K]> | ||
: T[K] extends FunctionLike | ||
? MockedFunctionShallow<T[K]> | ||
: T[K]; | ||
} & {[K in PropertyLikeKeys<T>]: MaybeMockedDeep<T[K]>}; | ||
} & T; | ||
|
||
export type MaybeMocked<T> = T extends FunctionLike | ||
export type Mocked<T extends object> = T extends ClassLike | ||
? MockedClass<T> | ||
: T extends FunctionLike | ||
? MockedFunction<T> | ||
: T extends object | ||
? MockedObject<T> | ||
: T; | ||
|
||
export type MaybeMockedDeep<T> = T extends FunctionLike | ||
? MockedFunctionDeep<T> | ||
type MockedShallow<T extends object> = T extends ClassLike | ||
? MockedClass<T> | ||
: T extends FunctionLike | ||
? MockedFunctionShallow<T> | ||
: T extends object | ||
? MockedObjectDeep<T> | ||
? MockedObjectShallow<T> | ||
: T; | ||
|
||
export type Mocked<T> = { | ||
[P in keyof T]: T[P] extends FunctionLike | ||
? MockInstance<T[P]> | ||
: T[P] extends ClassLike | ||
? MockedClass<T[P]> | ||
: T[P]; | ||
} & T; | ||
|
||
export type MockedClass<T extends ClassLike> = MockInstance< | ||
(args: T extends new (...args: infer P) => any ? P : never) => InstanceType<T> | ||
> & { | ||
prototype: T extends {prototype: any} ? Mocked<T['prototype']> : never; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be this could be a solution for |
||
} & T; | ||
|
||
export type UnknownFunction = (...args: Array<unknown>) => unknown; | ||
|
||
/** | ||
|
@@ -1234,13 +1216,13 @@ export class ModuleMocker { | |
return value == null ? `${value}` : typeof value; | ||
} | ||
|
||
// the typings test helper | ||
mocked<T>(item: T, deep?: false): MaybeMocked<T>; | ||
|
||
mocked<T>(item: T, deep: true): MaybeMockedDeep<T>; | ||
|
||
mocked<T>(item: T, _deep = false): MaybeMocked<T> | MaybeMockedDeep<T> { | ||
return item as any; | ||
mocked<T extends object>(item: T, deep?: false): MockedShallow<T>; | ||
mocked<T extends object>(item: T, deep: true): Mocked<T>; | ||
mocked<T extends object>( | ||
item: T, | ||
_deep = false, | ||
): Mocked<T> | MockedShallow<T> { | ||
return item as Mocked<T> | MockedShallow<T>; | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done ;D