3

I have webpack config with multiple entries:

entry: {
  'js/app': ['./css/app/style.scss', './js/app/index.js'],
  'js/vendor': ['./js/vendor/index.js', './css/vendor/index.css'],
  'js/snippets': './js/snippets/index.js'
},

and I don't want to hardcode all this paths in karma config. Just include webpack config and connect all the entries from there:

var webpackConfig = require('./webpack.config');
...
...
files: webpackConfig.entry,

but file option expect to have an array of strings. Is there any ready solution for it? Not to convert object to array manually.

Stepan Suvorov
  • 25,118
  • 26
  • 108
  • 176

1 Answers1

3

I have a similar setup, here's what worked for me. In the karma config file, for the files just config your tests. For me it was something like this:

files: [
  './scripts/vendor/jquery.min.js',
  // any other global dependency
  './scripts/**/*.spec.js'
]

edit

Because of how webpack works, you don't need to add your project files in the karma configuration. Just by the fact that your test will import/require the test subject file, webpack will bundle it and the test will run.

endedit

Then in the preprocessors section:

preprocessors: {
  './scripts/**/*.spec.js': ['webpack']
},

Finally, I have imported the webpack configuration:

const webpackConfig = require('./webpack.config.js');

And using the karma-webpack plugin I added a webpack section as follows:

webpack: {
  devtool: webpackConfig.devtool,
  resolve: webpackConfig.resolve,
  module: webpackConfig.module,
  externals: Object.assign({}, webpackConfig.externals, {
    chai: 'chai'
  })
},
thitemple
  • 5,833
  • 4
  • 42
  • 67
  • and how do you include project files/build? – Stepan Suvorov Apr 21 '16 at 13:08
  • I imagine that in your tests you're importing/requiring the project files, so webpack will take care of that. – thitemple Apr 21 '16 at 13:13
  • could I also ask you to show example of dependancies that you import into your test file? | because before when I test with karma I specified all project files in karma config – Stepan Suvorov Apr 21 '16 at 13:29
  • If you have external dependencies, you can add them to your files config, for instance I have a global dependency, on jquery, so I add that as well. I'll update the example. But that is as simple as adding the dependencies in the correct order into the files array – thitemple Apr 21 '16 at 13:41
  • looking forward for you update. your answer already helped a lot! thank you – Stepan Suvorov Apr 21 '16 at 16:11