-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
feat(@jest/mock): Add withImplementation #13281
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
Changes from 1 commit
c4cb677
dd2bb3c
bc2643b
dcef50b
12fddb2
b74d73b
bd99f78
93087bc
d252023
c776293
20300d8
67a4671
1c0d3bd
7acd442
552989c
51757a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -124,6 +124,12 @@ type RejectType<T extends FunctionLike> = ReturnType<T> extends PromiseLike<any> | |||||||||
| ? unknown | ||||||||||
| : never; | ||||||||||
|
|
||||||||||
| type WithImplementationSyncCallbackReturn = void | undefined; | ||||||||||
| type WithImplementationAsyncCallbackReturn = Promise<unknown>; | ||||||||||
| type WithImplementationCallbackReturn = | ||||||||||
| | WithImplementationSyncCallbackReturn | ||||||||||
| | WithImplementationAsyncCallbackReturn; | ||||||||||
jeppester marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
|
|
||||||||||
| export interface MockInstance<T extends FunctionLike = UnknownFunction> { | ||||||||||
| _isMockFunction: true; | ||||||||||
| _protoImpl: Function; | ||||||||||
|
|
@@ -135,6 +141,12 @@ export interface MockInstance<T extends FunctionLike = UnknownFunction> { | |||||||||
| mockRestore(): void; | ||||||||||
| mockImplementation(fn: T): this; | ||||||||||
| mockImplementationOnce(fn: T): this; | ||||||||||
| withImplementation<R extends WithImplementationCallbackReturn>( | ||||||||||
| fn: T, | ||||||||||
| callback: () => R, | ||||||||||
| ): R extends WithImplementationAsyncCallbackReturn | ||||||||||
| ? Promise<void> | ||||||||||
| : undefined; | ||||||||||
jeppester marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
| mockName(name: string): this; | ||||||||||
| mockReturnThis(): this; | ||||||||||
| mockReturnValue(value: ReturnType<T>): this; | ||||||||||
|
|
@@ -768,6 +780,34 @@ export class ModuleMocker { | |||||||||
| return f; | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| f.withImplementation = <R extends WithImplementationCallbackReturn>( | ||||||||||
| fn: UnknownFunction, | ||||||||||
| callback: () => R, | ||||||||||
| // @ts-expect-error: Type guards are not advanced enough for this use case | ||||||||||
| ): R extends WithImplementationAsyncCallbackReturn | ||||||||||
| ? Promise<void> | ||||||||||
| : undefined => { | ||||||||||
jeppester marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
| // Remember previous mock implementation, then set new one | ||||||||||
| const mockConfig = this._ensureMockConfig(f); | ||||||||||
| const previousImplementation = mockConfig.mockImpl; | ||||||||||
| mockConfig.mockImpl = fn; | ||||||||||
|
|
||||||||||
| const returnedValue = callback(); | ||||||||||
|
|
||||||||||
| if ( | ||||||||||
| typeof returnedValue === 'object' && | ||||||||||
| returnedValue !== null && | ||||||||||
|
||||||||||
| typeof returnedValue === 'object' && | |
| returnedValue !== null && | |
| returnedValue != null && | |
| typeof returnedValue === 'object' && |
probably doesn't matter, but easier to bail out early
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.
Good point. Like I mentioned in one of my previous comments I copied this verbatim from jest circus.
So there's probably a nice tiny performance enhancement waiting to be made there as well:
https://github.com/facebook/jest/blob/a20fd859673800c50f8f089cfb4a87faec119525/packages/jest-circus/src/utils.ts#L271-L275
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.
Hmm, we actually have https://github.com/facebook/jest/blob/c3a000e31343dd9e37da159e5903376c4312a0fd/packages/jest-util/src/isPromise.ts - should probably use that
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.
haha, the linked SO answer says not to use it 🙈
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.
I opened #13314
Uh oh!
There was an error while loading. Please reload this page.