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

Support for jest.mock() #97

Closed
sheerun opened this issue Nov 9, 2016 · 7 comments
Closed

Support for jest.mock() #97

sheerun opened this issue Nov 9, 2016 · 7 comments

Comments

@sheerun
Copy link

sheerun commented Nov 9, 2016

Hey,

I have following use-case: I'd like to mock require call with Jest testing framework, but when I do it this way:

file.js

// foobar is rewritten to ../../foobar by this plugin
import foobar from 'foobar'

file.test.js

jest.mock('foobar', () => {
  ...
})

then mock is not instatiated, as foobar is rewritten in tested file by this plugin to ../../foobar

Ideally I would be able to do something like this:

jest.mock(require.resolve('foobar'), () => {
  ...
})

and it would translate to:

jest.mock('../../foobar', () => {
  ...
})

what would result in proper require mocks.

Could this plugin support rewriting also require.resolve(XXX)?

@tleunen
Copy link
Owner

tleunen commented Nov 9, 2016

Not sure to understand what the issue is. You said file.test.js had foobar translated to ../../foobar, which causes an issue. And then you're asking to have require.resolve('foobar') to be translated to ../../foobar. Which is the same thing, right?

@tleunen
Copy link
Owner

tleunen commented Nov 9, 2016

Oh nvm, I misread it.
You're saying the jest test file is not updated with new paths ;)

Hmm.. instead of using require.resolve(), which people won't really use.. Why having a custom syntax for your tests? I'd prefer having a "updateJestMock" option to convert jest.mock(X) to the relative path.

What do you think?

@sheerun
Copy link
Author

sheerun commented Nov 10, 2016

that might work :) but it's probably issue not only with jest but also other mocking frameworks

@tleunen
Copy link
Owner

tleunen commented Nov 10, 2016

Sure, but I prefer having a custom setting by framework then than having to force people using a different syntax for their tests ;)

@tleunen tleunen changed the title Support for require.resolve Support for jest.mock() Nov 25, 2016
tleunen added a commit that referenced this issue Nov 29, 2016
@arcanis
Copy link

arcanis commented Feb 22, 2017

Oh, that's a shame, being able to use require.resolve would have helped me in my case too (I don't use jest, and I use it with a simple readFileSync call which module-resolve probably shouldn't automatically transform) :( If you want to support extra frameworks I can see the reasoning, but shouldn't native functions be supported as well?

@tleunen
Copy link
Owner

tleunen commented Feb 22, 2017

I believe we can also add support for require.resolve. Would you mind creating a new ticket for that?

@arcanis
Copy link

arcanis commented Feb 22, 2017

Sure (#126)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants