asapach / babel-plugin-rewire-exports

Babel plugin for stubbing [ES6, ES2015] module exports
MIT License
66 stars 7 forks source link

Is there a way to mock import from node_module? #11

Closed w8jcik closed 6 years ago

w8jcik commented 6 years ago

Because modules coming from node_modules are not handled by Babel they don't have rewire$ functions. Do you have some solution for this problem?

asapach commented 6 years ago

That's tricky. If the node module is in CJS there's nothing this plugin can do, since there are no exports to rewire. If it's in ESM, you could try configuring Babel to process that module, which should add the rewire functions. This plugin doesn't touch your imports, but there are other tools that do: e.g. proxyquire, rewire or babel-plugin-rewire. I could try looking at rewiring imports (e.g. create a new plugin - babel-plugin-rewire-imports) if you have any good suggestions on how the API should look.

asapach commented 6 years ago

Yeah, forgot to mention that imports might be tricky to mock/stub/rewire, because unlike CJS, in ESM you're importing a live binding and not a value (explanation). So we first need to find a reliable technique to do this without breaking existing code.

w8jcik commented 6 years ago

Sorry for being not responsive and thank you for great explanation.

It is logical that this package cannot do it and it is not that big issue.

Of course some babel-rewire-imports with matching API would be great but I am happy enough with this one being stable and working as intended.

You could still put it into the README.md for clarification (and close this issue).

asapach commented 6 years ago

Readme kind of already mentions this:

Unlike babel-plugin-rewire it doesn't modify the module internals (e.g. imports and top-level variables and functions).