Skip to content
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

[Bug]: jest.fn() no longer supports direct implementations with return values as parameter #13927

Closed
jeremy-brooks opened this issue Feb 16, 2023 · 3 comments

Comments

@jeremy-brooks
Copy link

Version

29.4.3

Steps to reproduce

  1. Create a basic test and using jest.fn with a parameter for the implementation that returns a value.
  2. Run the test and see that the mock returns undefined, where it used to return the mocked value.
  3. 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.

Environment

System:
    OS: Linux 5.4 Ubuntu 20.04.5 LTS (Focal Fossa)        
    CPU: (8) x64 Intel(R) Core(TM) i7-10610U CPU @ 1.80GHz
  Binaries:
    Node: 18.14.0 - ~/.nvm/versions/node/v18.14.0/bin/node
    npm: 9.4.2 - ~/.nvm/versions/node/v18.14.0/bin/npm    
  npmPackages:
    jest: ^29.4.3 => 29.4.3
@mrazauskas
Copy link
Contributor

mrazauskas commented Feb 16, 2023

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.

@jeremy-brooks
Copy link
Author

It turns out you are correct, however this did only start to happen recently, so I'm not really sure what to say about that, weird.

I'll close this issue off anyway.

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants