5

I've got a working Karma setup, using Webpack. Whenever I modify either a test file or any of its dependencies, all of the tests run. This is all good, but I'd really like it to run only the minimum number of tests. If I change a test file, it should run only that test file. If I change a source file, it should run whatever test files have that source file as a dependency (via require).

Here's the relevant portion of my karma.config.js:

files: [
  {
    pattern: '**/*.test.js',
    watched: true,
    included: true,
    served: true
  },
],

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

webpack: {
  resolve: {
    root: path.join(__dirname, '..', 'webpack'),
    extensions: ['', '.js', '.jsx'],
  },

  module: {
    loaders: [
      {
        test: /.jsx?$/,
        loader: 'babel-loader',
        exclude: [/node_modules/],
        query: {
          presets: ['es2015', 'react']
        }
      },
    ]
  },
},

The complete karma.config.js file is in a gist.

I'm familiar with solutions mentioned in other questions, involving process.env.npm_config_single_file, but that's not what I'm trying to do.

Based on my config above, I'm expecting it to handle each file as a separate webpack, which is also implied by the documentation at the top of webpack/karma-webpack. Instead, what I seem to get is a single webpack with two chunks, per the log:

                      Asset     Size  Chunks             Chunk Names
employee_list_entry.test.js  1.07 MB       0  [emitted]  employee_list_entry.test.js
employer_list_entry.test.js  1.07 MB       1  [emitted]  employer_list_entry.test.js
chunk    {0} employee_list_entry.test.js (employee_list_entry.test.js) 1.01 MB

It always rebuilds both chunks even when I only save one test file.

Community
  • 1
  • 1
Jim Stewart
  • 16,964
  • 5
  • 69
  • 89
  • 3
    There is an open issue here: https://github.com/karma-runner/karma/issues/1507 I was surprised to find that running all tests when one file changed is just how Karma works. I thought for the longest time we just had some strange configuration issue. Go figure! – meta-meta Jan 05 '17 at 13:16

1 Answers1

0

Instead of using the watch mode of Karma that seems to be restricted to executing all files, you might want to apply the watch mode of Webpack:

https://webpack.js.org/configuration/watch/

Also see following related questions for Gulp and Grunt:

Karma run single test

Getting grunt karma to run one unit test

Stefan
  • 10,010
  • 7
  • 61
  • 117