-
-
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 6 commits
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 | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -135,6 +135,8 @@ export interface MockInstance<T extends FunctionLike = UnknownFunction> { | |||||||||
| mockRestore(): void; | ||||||||||
| mockImplementation(fn: T): this; | ||||||||||
| mockImplementationOnce(fn: T): this; | ||||||||||
| withImplementation(fn: T, callback: () => Promise<unknown>): Promise<void>; | ||||||||||
| withImplementation(fn: T, callback: () => void): void; | ||||||||||
| mockName(name: string): this; | ||||||||||
| mockReturnThis(): this; | ||||||||||
| mockReturnValue(value: ReturnType<T>): this; | ||||||||||
|
|
@@ -768,6 +770,38 @@ export class ModuleMocker { | |||||||||
| return f; | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| f.withImplementation = withImplementation.bind(this); | ||||||||||
|
|
||||||||||
| function withImplementation(fn: T, callback: () => void): void; | ||||||||||
| function withImplementation( | ||||||||||
| fn: T, | ||||||||||
| callback: () => Promise<unknown>, | ||||||||||
| ): Promise<void>; | ||||||||||
| function withImplementation( | ||||||||||
| this: ModuleMocker, | ||||||||||
| fn: T, | ||||||||||
| callback: (() => void) | (() => Promise<unknown>), | ||||||||||
| ): void | Promise<void> { | ||||||||||
| // 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.