Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running tests with karma and webpack

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?

like image 381
Michel Uncini Avatar asked Mar 28 '26 03:03

Michel Uncini


1 Answers

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.

like image 182
George Brassey Avatar answered Mar 31 '26 08:03

George Brassey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!