You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Create a basic test and using jest.fn with a parameter for the implementation that returns a value.
Run the test and see that the mock returns undefined, where it used to return the mocked value.
Move the implementation into a beforeEach or test block and use the mockImplementation method instead, then run the test and the mock will return what ever you returned from the implementation.
I have not bothered putting full code here as the setup is so basic.
Expected behavior
// mock syntax that mocks a function and implements it with a return value const mock = jest.fn(() => true);
Actual behavior
// mock syntax that mocks a function and implements it with a return value
// Bug: usage returns undefined const mock = jest.fn(() => true);
Additional context
If I move the implementation to a separate line for example:
// mock
const mock = jest.fn();
// impl in a test instead
test('something', ()=> {
mock.mockImplementation(() => true);
...
});
Then the mocked function returns value and has an implementation.
Can it be that you have restoreMocks: true in your config?
It should "Automatically restore mock state and implementation before every test." Which means that all what you did outside of a test gets restored before the test runs.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.
Version
29.4.3
Steps to reproduce
undefined
, where it used to return the mocked value.beforeEach
ortest
block and use themockImplementation
method instead, then run the test and the mock will return what ever you returned from the implementation.I have not bothered putting full code here as the setup is so basic.
Expected behavior
// mock syntax that mocks a function and implements it with a return value
const mock = jest.fn(() => true);
Actual behavior
// mock syntax that mocks a function and implements it with a return value
// Bug: usage returns
undefined
const mock = jest.fn(() => true);
Additional context
If I move the implementation to a separate line for example:
Then the mocked function returns value and has an implementation.
Environment
The text was updated successfully, but these errors were encountered: