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 · 5 comments

Comments

@sterpe
Copy link

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

This comment has been minimized.

Copy link
Contributor

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.

@jackocnr

This comment has been minimized.

Copy link

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",
},
@flepretre

This comment has been minimized.

Copy link

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.

@jackocnr

This comment has been minimized.

Copy link

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".

¯\_(ツ)_/¯

@flepretre

This comment has been minimized.

Copy link

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 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.