Running tests with karma and webpack

1.6k views Asked by At

My project has external dependecies so I configured webpack like that:

externals:{
        'd3':'d3',
        'another-external-dep': 'another-external-dep'
}

And then in the code I require the dependecies like that:

var someProp = require('another-external-dep').someProp.

All good until I integrated karma. So karma when run the tests fail to find the module another-external-dep clearly because it is an external dependecies and I did not included in the karma config on the list of files.

How can I mock another-external-dep so require('another-external-dep') returns a mock? Also where I can specify this mock, in the config or in the mock?

1

There are 1 answers

1
George Brassey On

You can link to external dependencies during karma tests by including dependencies in the array of files in karma.config.js.

module.exports = function karmaConfig(config) {
    config.set({
        ...
        files: [
            'path/to/external/jquery.js',
            'tests.webpack.js',
        ],
        webpack: {
            externals: {
                'jquery': 'jQuery',
            },
        },
        ...
    });
};

This makes the dependencies available in the global context, which you can then reference from the webpack'd files, replicating your development context.