jhnns / rewire-webpack

Dependency injection for webpack bundles
The Unlicense
121 stars 20 forks source link

Using rewire within test #10

Open nwhite89 opened 9 years ago

nwhite89 commented 9 years ago

I'm wanting to use rewire-webpack within a Karma test is this possible? I'm wanting to run my test load in the module in which to test and run it through rewire allowing me to change specific functions which I believe rewire allows you to do?

The problem I'm getting is that it doesn't seem to be able to load the module

TypeError: webpack_require.m[module] is undefined in /Users/nickwhite/Sites/rbx_formats/test/formats/karma/src/silicon/bust.spec.js (line 84)
rewire@/Users/nickwhite/Sites/rbx_formats/test/formats/karma/src/silicon/bust.spec.js:84
[0]/WEBPACK_AMD_DEFINE_RESULT</</<@/Users/nickwhite/Sites/rbx_formats/test/formats/karma/src/silicon/bust.spec.js

be great to get an idea on this thanks

jhnns commented 9 years ago

Sorry, this error description is too vague. Could you create an example repository that demonstrates the problem?

nwhite89 commented 9 years ago

I can look at doing something today / tomorrow thanks @jhnns

srph commented 9 years ago

Not sure if this is related. But I'll just put it here. Anyway, I'll just create an issue if not :wink:.

I'm using Karma along with Mocha and Webpack, and it doesn't seem to work although I've followed the instructions.

This is the error:

Uncaught ReferenceError: rewire is not defined

This is my karma config:

    // Webpack config
    webpack: {
      module: {
        loaders: [
          { test: /\.(js|jsx)$/, exclude: /node_modules/, loader: "babel-loader?modules=common&stage=0" }
        ]
      },
      resolve: {
        extensions: ['', '.js', '.jsx', '.es6']
      },
      devtool: 'inline-source-map',
      plugins: [new RewirePlugin()]
    },
janusch commented 9 years ago

@srph I do not think your question is related to this issue. Anyhow, you have to npm install rewire-webpack -save-dev and require it in your karma.conf var RewirePlugin = require('rewire-webpack');

I hope that solves yoour issue!

jhnns commented 9 years ago

Maybe related to #11?

wbinnssmith commented 9 years ago

I can confirm that I get this error when using rewire-webpack with the babel-loader (5.x or later, 4.x is just fine). My guess is that this, #12, and #11 are all the same issue.

srph commented 9 years ago

It's related. Thanks.

jhnns commented 9 years ago

@nwhite89 Are you using babel?

nwhite89 commented 9 years ago

No afraid not, I was basically trying to require one of my own AMD modules then within a Karma test require in the module I am wanting to test but I wanted to change a set variable within that module (which requires from another module). Purely testing purposes, I haven't tried anymore as I had to "timebox" what it was I was doing, I shall try and look at this again at some point this week @jhnns cheers