speedskater / babel-plugin-rewire

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

Couldn't find preset (despite having npm install-ed rewire) #162

Closed machineghost closed 8 years ago

machineghost commented 8 years ago

I am trying to use babel-plugin-rewire for the first time, but I'm having some difficulty. I have installed it and Babel (via npm install --save-dev babel-core babel-plugin-rewire), and I've added it to my .babelrc file:

"presets": ["es2015", "react", "rewire"]

and to my Webpack config too, just for good measure:

loaders: 'babel?plugins=rewire'

Then, in my test I have the following (an attempt to rewire a postMessage function exported from a MessageQueue ,odule):

import { __RewireAPI__ as postMessage } from '~/src/store/ducks/MessageQueue'

before(() => {
    __RewireAPI__.__Rewire__('postMessage', () => console.log('win!'))
});

However, I get the following error:

 appHome/node_modules/babel-core/lib/transformation/file/options/option-manager.js:281
      throw new Error("Couldn't find preset " + (0, _stringify2.default)(val) + " relative to directory " + (0, _stringify2.default)(dirname));
      ^
 Error: Couldn't find preset "rewire" relative to directory "appHome/"

That error seems to suggest that I didn't install rewire, but I most certainly did, which Node confirms:

npm list | grep rewire
├── babel-plugin-rewire@1.0.0

Is there anything else I might be doing wrong to cause this? I've tried searching both on the web and through this project's issues, but I haven't seen this particular error, so I strongly suspect it's a matter of user error and not a true bug (but having never used rewire successfully before, I can't say for sure).

jrans commented 8 years ago

@machineghost looks like you've put rewire in as a preset rather than a plugin in your .babelrc file

machineghost commented 8 years ago

Sadly this is not the first time I've made that particular bone-headed mistake ... thanks!