babel-plugin-rewire icon indicating copy to clipboard operation
babel-plugin-rewire copied to clipboard

Rewire with Webpack and Karma

Open bunkat opened this issue 9 years ago • 14 comments

I can't figure out how to make this plugin work correctly. I'm using webpack and have installed the latest version of the plugin. I've tried adding the following to my webpack config:

module: {
    loaders: [
      {
        test: /\.js$/,
        loaders: ['react-hot', 'babel?stage=0', 'babel-loader?plugins=babel-plugin-rewire'],
        include: path.join(__dirname, 'src/js')
      },
     ...
}

I'm not seeing any errors from webpack, but when I then do the following:

sessionActions.js

export function login(email, password) {
    ...
};

tests/sessionActions-test.js

import * as SessionRewireAPI from '../sessionActions';

There is no __Rewire__ call being added to the module. Obviously I'm missing something and I can't figure out what. How exactly does the plugin get loaded? How can I test to see if it is getting loaded correctly?

I've also tried the following but then SesssionRewireAPI is just undefined.

import {login, __RewireAPI__ as SessionRewireAPI} from '../sessionActions';

bunkat avatar Aug 22 '15 19:08 bunkat

@bunkat I am not sure. But it could be that the problem is that you use babel loader twice. "'babel?stage=0', 'babel-loader?plugins=babel-plugin-rewire'" If this is not the case could you please create a small sample project which replicates your error.

speedskater avatar Aug 24 '15 07:08 speedskater

Same here...

volkanunsal avatar Sep 11 '15 20:09 volkanunsal

My problem was indeed loading the loader twice. That fixed it for me.

volkanunsal avatar Sep 11 '15 21:09 volkanunsal

This is happening to me as well. Not sure if its due to the fact that I'm loading webpack via karma?

module.exports = function (config) {

  config.set({
    // browsers: ['PhantomJS'],
    browsers: ['Chrome'],

    frameworks: [
      'jasmine',
      'jasmine-matchers'
    ],

    reporters: ['progress', 'beep'],

    files: [
      'https://code.jquery.com/jquery-2.1.4.js',
      'public/scripts/vendor/tinymce/tinymce.full.js',
      'karma.tests.webpack.js'
    ],

    preprocessors: {
      'karma.tests.webpack.js': [
        'webpack',
        'sourcemap'
      ]
    },

    webpack: {
      devtool: 'inline-source-map',

      module: {
        loaders: [
          {
            test: /\.js$/,
            loader: 'babel-loader',
            exclude: /node_modules/,
            query: {
              plugins: ['babel-plugin-rewire']
            }
          }
        ]
      }
    },

    webpackServer: {
      noInfo: true
    }
  });
};

damassi avatar Sep 14 '15 04:09 damassi

query: {
    plugins: ['babel-plugin-rewire']
}

I'd never seen the query for a loader written in this form before. According to the Webpack docs, query should be a string. That might be the root cause. For what it's worth, my loader config looks like this:

{
   test: /\.js$/,
   loader: 'babel?plugins[]=babel-plugin-rewire'
}

volkanunsal avatar Sep 14 '15 05:09 volkanunsal

@volkanunsal This might be the case. We use babel-plugin-rewire with webpack and karma with the following loader config: loader: 'babel-loader?plugins=rewire'

speedskater avatar Sep 14 '15 07:09 speedskater

Same here. Any solution for that?

screendriver avatar Aug 18 '16 08:08 screendriver

@screendriver could you please provide a simple failing sample project?

speedskater avatar Aug 18 '16 08:08 speedskater

I made a sample project

Just make a npm install and after that npm test and you should see what's happening. Thank you 👍

screendriver avatar Aug 18 '16 08:08 screendriver

I have a sample working project here, which you could have a look at and see if it helps. It was created with another reason in mind, but perhaps it is useful: https://github.com/jseminck/karma-rewire-istanbul-example

jseminck avatar Aug 27 '16 15:08 jseminck

@screendriver thank you for your sample project. I created two pull request for your sample project making it work. Could please let me know if this helps? @screendriver, @jseminck a soon as your sample projects are working, Would it be okay for you if I would link your projects in the README of the project?

speedskater avatar Aug 29 '16 09:08 speedskater

@speedskater thank you. It seems to work. But how can I rewire the whole function instead extracting the return value to a variable? For example if I have a function that returns internal state or something? If I remove the variable in my sample project the Rewire object is getting undefined.

screendriver avatar Sep 05 '16 06:09 screendriver

@screendriver you can mock a complete function as long as it represents a dependency in the module (in this context a dependency is anything defined at module scope which is used seomewhere in the module e.g. variable, class, function, import..) If you never use the function imho the function itself doesn't make sense.

speedskater avatar Sep 12 '16 06:09 speedskater

I'm trying to get babel-plugin-rewire to work in a project based on react-boilerplate with inspiration from https://github.com/jseminck/karma-rewire-istanbul-example by @jseminck. I't doesn't mess with babel loader in webpack config, instead rewire is added in the plugins section in .babelrc. I think this is a good way to configure babel-plugin-rewire but when I try to do it in my project the tests passes but report generation fails.

I have forked react-boilerplate, bumped some dependencies, changed karma config to use PhantomJS and added babel-plugin-rewire if someone would like to have a look https://github.com/olabalboa/react-boilerplate

olabalboa avatar Nov 30 '16 15:11 olabalboa