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

the second argument of jest.mock must be a function #4588

Closed
01binary opened this issue Oct 2, 2017 · 8 comments
Closed

the second argument of jest.mock must be a function #4588

01binary opened this issue Oct 2, 2017 · 8 comments

Comments

@01binary
Copy link
Contributor

01binary commented Oct 2, 2017

What is the current behavior?

jest.mock(moduleName, func) throws the error, the second argument of jest.mock must be a function if func is defined as:

function func() {
   return 'Hello';
}

...but works if the func is defined inline as an arrow function:
jest.mock(moduleName, () => 'Hello')

In both cases moduleName is a string like 'fs'

What is the expected behavior?

Either of:

  • Change error message to say the second argument of jest.mock must be an arrow function
  • Make jest.mock accept a function that's not an arrow function

This is in a bare-bones Node app that doesn't use Babel and has index.js that prints a message to console.

@cpojer
Copy link
Member

cpojer commented Oct 2, 2017

It accepts functions, it just doesn't accept references. If you'd like, you could send a PR to babel-plugin-jest-hoist to adjust the error message to say must be an inline function. What do you think?

@nicolasiensen
Copy link
Contributor

@cpojer is there a good reason why mock it doesn't accept function reference?

@cpojer
Copy link
Member

cpojer commented Oct 3, 2017

Yes, because your mock may be required before references are fully initialized. It's easier to see jest.mock calls as fully isolated, and the function as an isolated module scope.

Consider a module Banana that depends on Kiwi, which is mocked:

import Banana from './Banana';
jest.mock('Kiwi', () => name);
const name = ''I am a pear';

When Banana requires Kiwi, we know Kiwi is mocked. We run the function and then it throws saying name does not exist/is not initialized, simply because it is sequentially executed:

  • require Banana
  • Banana requires Kiwi
  • Kiwi is mocked, and runs the mock function.
  • The mock function would like to return name.
  • name isn't initialized yet

This is why you cannot reference anything from the outer scope. Hope that makes sense.

@01binary 01binary changed the title the second argument of jest.mock must be a function (with no Babel) the second argument of jest.mock must be a function Oct 3, 2017
@cpojer
Copy link
Member

cpojer commented Oct 3, 2017

Thanks! See #4593

@cpojer cpojer closed this as completed Oct 3, 2017
@mrlubos
Copy link

mrlubos commented Jan 31, 2018

Hi, is there a way to automatically call mock on every test suite? I ran into this (now updated) error message when trying to create a mock module for i18n and call it as jest.mock('react-i18next', fn);.

I know this function will be required in multiple modules so I am trying to avoid writing this fix in every test.

jest.mock('react-i18next', () => ({
  // this mock makes sure any components using the translate HoC receive the t function as a prop
  translate: () => Component => props => <Component t={() => ''} {...props} />,
}));

@SimenB
Copy link
Member

SimenB commented Feb 11, 2018

@lmenus add your mock to a file called react-i18next in __mocks__. See https://facebook.github.io/jest/docs/en/manual-mocks.html

@mrlubos
Copy link

mrlubos commented Feb 11, 2018

@SimenB Ah, missed that, thank you! This certainly reduces it to a mere jest.mock('react-i18next'); now!

@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 May 13, 2021
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

6 participants