-
-
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 all 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 |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
|
|
||
| /* eslint-disable local/ban-types-eventually, local/prefer-rest-params-eventually */ | ||
|
|
||
| import * as util from 'util'; | ||
| import vm, {Context} from 'vm'; | ||
| import {ModuleMocker, fn, mocked, spyOn} from '../'; | ||
|
|
||
|
|
@@ -1073,6 +1074,62 @@ describe('moduleMocker', () => { | |
| }); | ||
| }); | ||
|
|
||
| describe('withImplementation', () => { | ||
| it('sets an implementation which is available within the callback', () => { | ||
| const mock1 = jest.fn(); | ||
| const mock2 = jest.fn(); | ||
|
|
||
| const Module = jest.fn(() => ({someFn: mock1})); | ||
| const testFn = function () { | ||
| const m = new Module(); | ||
| m.someFn(); | ||
| }; | ||
|
|
||
| Module.withImplementation( | ||
| () => ({someFn: mock2}), | ||
| () => { | ||
| testFn(); | ||
| expect(mock2).toHaveBeenCalled(); | ||
| expect(mock1).not.toHaveBeenCalled(); | ||
| }, | ||
| ); | ||
|
|
||
| testFn(); | ||
| expect(mock1).toHaveBeenCalled(); | ||
|
|
||
| expect.assertions(3); | ||
|
Contributor
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. Just wondering: should
Contributor
Author
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. Since the callback function will always be executed immediately by
Contributor
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. Ah, right. Got it (; |
||
| }); | ||
|
|
||
| it('returns a promise if the provided callback is asynchronous', async () => { | ||
jeppester marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const mock1 = jest.fn(); | ||
| const mock2 = jest.fn(); | ||
|
|
||
| const Module = jest.fn(() => ({someFn: mock1})); | ||
| const testFn = function () { | ||
| const m = new Module(); | ||
| m.someFn(); | ||
| }; | ||
|
|
||
| const promise = Module.withImplementation( | ||
| () => ({someFn: mock2}), | ||
| async () => { | ||
| testFn(); | ||
| expect(mock2).toHaveBeenCalled(); | ||
| expect(mock1).not.toHaveBeenCalled(); | ||
| }, | ||
| ); | ||
|
|
||
| expect(util.types.isPromise(promise)).toBe(true); | ||
|
|
||
| await promise; | ||
|
|
||
| testFn(); | ||
| expect(mock1).toHaveBeenCalled(); | ||
|
|
||
| expect.assertions(4); | ||
| }); | ||
| }); | ||
|
|
||
| test('mockReturnValue does not override mockImplementationOnce', () => { | ||
| const mockFn = jest | ||
| .fn() | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.