Running tests with karma and webpack

My project has external dependencies, so I configured webpack like this:

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

      

And then in the code I need dependencies like this:

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

...

It's okay until I turn on karma. Thus, karma does not another-external-dep

explicitly find the module when executing tests , because these are external dependencies, and I did not include the karma configuration in the file list.

How can I mock another-external-dep

so require('another-external-dep')

mock returns? Also, where can I specify this layout, in the config or in the layout?

+3


source to share


1 answer


You can reference external dependencies during karma tests, including dependencies in an array 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 can then be referenced by webpack'd files, replicating the development context.

+7


source







All Articles