speedskater / babel-plugin-rewire

A babel plugin adding the ability to rewire module dependencies. This enables to mock modules for testing purposes.
841 stars 90 forks source link

Rewire a non-existing dependency #135

Closed mariotacke closed 8 years ago

mariotacke commented 8 years ago

I'm trying to test a file with a default export which imports a non-existing dependency. It does not exist at this time because webpack targets a specific environment (electron in my case) during my actual build. When I try to run the test, it breaks on the import because the dependency does not exist yet.

Is there a way to rewire the dependency before it actually loads the file? I used proxyquire before which worked in a similar way.

I run mocha tests like this: ./node_modules/.bin/mocha --compilers js:babel-core/register

It fails with Cannot find module 'doesnt-exist', naturally.

someFunction.js
import { unknown } from 'doesnt-exist';

export default function someFunction () { }
test.js
import someFunction, { __Rewire__ } from '../src/someFunction';
__Rewire__('doesnt-exist', { unknown: 'some value' });
.babelrc
{
  "presets": [
    "es2015"
  ],
  "plugins": [
    "rewire"
  ]
}
speedskater commented 8 years ago

@mariotacke I think what you trying to achieve with this library is not possible. Maybe you should provide a dummy electron module for all your tests or use https://github.com/jprichardson/electron-mocha. Could this be a solution for you?