karma-webpack
Use webpack with karma
Last updated 5 years ago by sokra .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install karma-webpack 
SYNC missed versions from official npm registry.

npm node deps tests coverage chat size

karma-webpack

Use webpack to preprocess files in karma.

Getting Started

To begin, you'll need to install karma-webpack:

npm i --save-dev karma-webpack

Then add config:

karma.conf.js

module.exports = (config) => {
  config.set({
    // ... normal karma configuration
    files: [
      // all files ending in "_test"
      { pattern: 'test/*_test.js', watched: false },
      { pattern: 'test/**/*_test.js', watched: false },
      // each file acts as entry point for the webpack configuration
    ],

    preprocessors: {
      // add webpack as preprocessor
      'test/*_test.js': ['webpack'],
      'test/**/*_test.js': ['webpack'],
    },

    webpack: {
      // karma watches the test entry points
      // (you don't need to specify the entry option)
      // webpack watches dependencies
      // webpack configuration
    },

    webpackMiddleware: {
      // webpack-dev-middleware configuration
      // i. e.
      stats: 'errors-only',
    },
  });
};

Alternative Usage

This configuration is more performant, but you cannot run single test anymore (only the complete suite).

The above configuration generates a webpack bundle for each test. For many test cases this can result in many big files. The alternative configuration creates a single bundle with all test cases.

karma.conf.js

files: [
  // only specify one entry point
  // and require all tests in there
  'test/index_test.js'
],

preprocessors: {
  // add webpack as preprocessor
  'test/index_test.js': [ 'webpack' ]
},

test/index_test.js

// require all modules ending in "_test" from the
// current directory and all subdirectories
const testsContext = require.context('.', true, /_test$/);

testsContext.keys().forEach(testsContext);

Every test file is required using the require.context and compiled with webpack into one test bundle.

Source Maps

You can use the karma-sourcemap-loader to get the source maps generated for your test bundle.

npm i -D karma-sourcemap-loader

And then add it to your preprocessors.

karma.conf.js

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

And tell webpack to generate sourcemaps.

webpack.config.js

webpack: {
  // ...
  devtool: 'inline-source-map';
}

Options

This is the full list of options you can specify in your karma.conf.js

Name Type Default Description
webpack {Object} {} Pass webpack.config.js to karma
webpackMiddleware {Object} {} Pass webpack-dev-middleware configuration to karma
beforeMiddleware {Object} {} Pass custom middleware configuration to karma, before any karma middleware runs

webpack

webpack configuration (webpack.config.js).

webpackMiddleware

Configuration for webpack-dev-middleware.

beforeMiddleware

beforeMiddleware is a webpack option that allows injecting middleware before karma's own middleware runs. This loader provides a webpackBlocker middleware that will block tests from running until code recompiles. That is, given this scenario

  1. Have a browser open on the karma debug page (http://localhost:9876/debug.html)
  2. Make a code change
  3. Refresh

Without the webpackBlocker middleware karma will serve files from before the code change. With the webpackBlocker middleware the loader will not serve the files until the code has finished recompiling.

⚠️ The beforeMiddleware option is only supported in karma >= v1.0.0

Contributing

Please take a moment to read our contributing guidelines if you haven't yet done so.

CONTRIBUTING

License

MIT

Current Tags

  • 5.0.0-alpha.3.0                                ...           dev (6 months ago)
  • 4.0.2                                ...           latest (3 months ago)
  • 4.0.0-rc.6                                ...           next (7 months ago)

50 Versions

  • 4.0.2                                ...           3 months ago
  • 4.0.1                                ...           3 months ago
  • 4.0.0                                ...           3 months ago
  • 5.0.0-alpha.3.0                                ...           6 months ago
  • 5.0.0-alpha.2                                ...           7 months ago
  • 4.0.0-rc.6                                ...           7 months ago
  • 5.0.0-alpha.1                                ...           8 months ago
  • 5.0.0-alpha.0                                ...           9 months ago
  • 4.0.0-rc.5                                ...           9 months ago
  • 4.0.0-rc.4                                ...           10 months ago
  • 4.0.0-rc.3                                ...           10 months ago
  • 3.0.5                                ...           a year ago
  • 4.0.0-rc.2                                ...           a year ago
  • 3.0.4                                ...           a year ago
  • 3.0.3                                ...           a year ago
  • 4.0.0-rc.1                                ...           a year ago
  • 3.0.2                                ...           a year ago
  • 4.0.0-rc.0                                ...           a year ago
  • 3.0.1                                ...           a year ago
  • 4.0.0-beta.0                                ...           a year ago
  • 3.0.0                                ...           a year ago
  • 2.0.13                                ...           2 years ago
  • 2.0.12                                ...           2 years ago
  • 2.0.11                                ...           2 years ago
  • 2.0.10                                ...           2 years ago
  • 2.0.9                                ...           2 years ago
  • 2.0.8                                ...           2 years ago
  • 2.0.7 [deprecated]           ...           2 years ago
  • 2.0.6                                ...           2 years ago
  • 2.0.5                                ...           2 years ago
  • 2.0.4                                ...           2 years ago
  • 2.0.3                                ...           3 years ago
  • 2.0.2                                ...           3 years ago
  • 2.0.1                                ...           3 years ago
  • 2.0.0                                ...           3 years ago
  • 1.8.1                                ...           3 years ago
  • 1.8.0                                ...           3 years ago
  • 1.7.0                                ...           4 years ago
  • 1.6.0                                ...           4 years ago
  • 1.5.1                                ...           4 years ago
  • 1.5.0                                ...           5 years ago
  • 1.4.0                                ...           5 years ago
  • 1.3.1                                ...           5 years ago
  • 1.3.0                                ...           5 years ago
  • 1.2.2                                ...           5 years ago
  • 1.2.1                                ...           5 years ago
  • 1.1.0                                ...           5 years ago
  • 1.0.2                                ...           5 years ago
  • 1.0.1                                ...           6 years ago
  • 1.0.0                                ...           6 years ago
Downloads
Today 62
This Week 4,416
This Month 10,391
Last Day 153
Last Week 5,751
Last Month 29,246
Dependencies (1)
Dev Dependencies (3)
Dependents (519)

Copyright 2014 - 2016 © taobao.org |