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

Automocker __mocks__/ discovery should support node modules with @namespace/module naming. #462

Closed
sterpe opened this issue Aug 15, 2015 · 6 comments

Comments

@sterpe
Copy link

@sterpe sterpe commented Aug 15, 2015

I'm having a hard time getting the autoMocker to find manual mocks in the __mocks__/ folder
when the module name includes a @namespaces, e.g. require('@quux/foo');

Tried this:

__mocks__/
  @quux/
     foo.js

&&

__mocks__/
  foo.js

Right now I've ended up using setMock('@quux/foo', mockInstance) and pulling in mockInstance with require.requireActual but it seems less than ideal.

Other ideas? Or will this be supported in 0.5.x?

@cpojer
Copy link
Collaborator

@cpojer cpojer commented Sep 29, 2016

I recommend using jest.mock('@namespace/module/name', () => {…}) for this. I don't think we'll add support for nested mocks.

Loading

@jackocnr
Copy link

@jackocnr jackocnr commented May 15, 2018

I have a similar issue with importing modules from Braintree - in my code I'm importing modules from 'braintree-web/client', 'braintree-web/hosted-fields', 'braintree-web/data-collector', and 'braintree-web/paypal', and my mocks for these objects are non-trivial, so to rely on jest.mock for these things means pasting the same ~40 lines at the top of each of the relevant test files.

My current workaround is to manually map the module names in moduleNameMapper in package.json e.g.

"moduleNameMapper": {
    "braintree-web/client": "<rootDir>/__mocks__/braintree-web/client",
},

Loading

@flepretre
Copy link

@flepretre flepretre commented Jul 19, 2018

If your mock is light to setup, and you use it a lot, you can add the jest.mock('@namespace/module/name', () => {…}) in your setup file.

We had the same issue for our translation lib, which is used in 200 of our test files.

Loading

@jackocnr
Copy link

@jackocnr jackocnr commented Jul 23, 2018

Thanks for the idea @flepretre but I just tried removing my moduleNameMapper entries in package.json, and my namespaced mocks still seem to be working! e.g. __mocks__/braintree-web/client.js for my code which imports "braintree-web/client".

¯\_(ツ)_/¯

Loading

@flepretre
Copy link

@flepretre flepretre commented Jul 23, 2018

That's strange, I had the problem last week.
I'll verify with the latest version off jest, but for me it didn't work.

I should have done some mistake somewhere with the mock because it's definitively working now!
Nested modules mock are in fact fully supported 👍

Loading

@github-actions
Copy link

@github-actions github-actions bot commented May 12, 2021

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.

Loading

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants