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

How do I mock require('module/submodule') #553

Closed
seekshiva opened this issue Oct 16, 2015 · 13 comments
Closed

How do I mock require('module/submodule') #553

seekshiva opened this issue Oct 16, 2015 · 13 comments

Comments

@seekshiva
Copy link

It says in the doc that we can create mock for node modules by placing __mocks__ in the same directory as node_modules.

How do I mock require('module/submodule')?

I tried this:

/
  __mocks__/
    module/
      submodule.js

and this:

/
  __mocks__/
    module.js

where module.js would contain this:

module.exports = {
  submodule: () => {}
}

but neither of these seem to work.

@cpojer
Copy link
Member

cpojer commented Oct 16, 2015

If your module is located at <dir>/module.js you should create a mock under <dir>/__mocks__/module.js.

@cpojer cpojer closed this as completed Oct 16, 2015
@seekshiva
Copy link
Author

@cpojer It is a npm module.

I do require("collections/sorted-map"). The docs say that when I need to mock the functionality of a npm module, the mocks need to be present in the same folder that contains node_modules.

I tried doing __mocks__/collections/sorted-map.js and __mocks__/collections.js. In the second case, collection.js would contain module.exports = {"sorted-map": () => {}}

But neither of those cases work.

@cpojer
Copy link
Member

cpojer commented Oct 16, 2015

Are you using npm3?

@seekshiva
Copy link
Author

Yes

@cpojer
Copy link
Member

cpojer commented Oct 16, 2015

Unfortunately we don't have a good solution for this yet :( We aren't using npm3 at FB yet and I haven't been able to investigate this issue thoroughly yet. If you have an idea for a fix, I'd appreciate a pull request!

@seekshiva
Copy link
Author

I'm trying to mock collections/sorted-map because of this issue: babel/babel-jest#41. I thought it would be an issue with babel-jest and not jest itself, so I filed an issue there.

@seekshiva
Copy link
Author

Is there a development doc for Jest that I can take a look at?

@cpojer
Copy link
Member

cpojer commented Oct 16, 2015

what kind of development doc are you looking for? I'm working on revamping the CONTRIBUTING guide. All you need to do is make a change and submit a PR on GitHub. Our bot will then ask you to sign the CLA.

You can run jest on itself by doing node bin/jest.js.

@Xesued
Copy link

Xesued commented Jun 3, 2016

For anyone else looking into this, you can now create a map with: moduleNameMapper.

Jest configuration:

{
  ...
  "moduleNameMapper": [
    "collections/sorted-map": "<rootDir>/__mocks__/collections/sorted-map.js"
    ...
    ]
}

Really, you can map it to any file.

@StevePotter
Copy link

StevePotter commented Nov 5, 2017

@Xesued is correct but I was unable to use the same kind of naming. I was trying to mock a file called HeaderStyleInterpolator.s fromreact-navigation. This worked for me:

    "moduleNameMapper": {
      "./HeaderStyleInterpolator": "<rootDir>/js/testing/NavHeaderStyleInterpolatorMock.js"
    }

@jquense
Copy link
Contributor

jquense commented Jul 22, 2020

this should be re-opened. It's still a problem and mocking via moduleNameMapper is bad because it's impossible to require the real module from the mock file

@thernstig
Copy link
Contributor

@cpojer @xeodou @jquense @StevePotter

I recently opened this issue: #11136

Reading this thread, it seems it is not possible to mock require('fs/promises') without using the Jest configuration moduleNameMapper? That is not a proper solution.

Since Node.js 15 and later actually has promises as the first go-to for the fs module, and they also recommend require('fs/promises') to be used, this feels more urgent to solve.

Should I re-phrase my issue at #11136 slightly, or keep it as is? Because it does not seem to be a doc problem only. It also is not possible to mock, right?

@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 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants