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

npm node deps test coverage chat

Karma Webpack

Use webpack to preprocess files in karma

Install

npm i -D karma-webpack

Usage

karma.conf.js

module.exports = (config) => {
  config.set({
    // ... normal karma configuration

    // add webpack to your list of frameworks
    frameworks: ['mocha', 'webpack'],

    files: [
      // all files ending in ".test.js"
      // !!! use watched: false as we use webpacks watch
      { pattern: 'test/**/*.test.js', watched: false }
    ],

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

    webpack: {
      // karma watches the test entry points
      // Do NOT specify the entry option
      // webpack watches dependencies

      // webpack configuration
    },
  });
}

Default webpack configuration

This configuration will be merged with what gets provided via karma's config.webpack.

const defaultWebpackOptions = {
  mode: 'development',
  output: {
    filename: '[name].js',
    path: path.join(os.tmpdir(), '_karma_webpack_'),
  },
  stats: {
    modules: false,
    colors: true,
  },
  watch: false,
  optimization: {
    runtimeChunk: 'single',
    splitChunks: {
      chunks: 'all',
      minSize: 0,
      cacheGroups: {
        commons: {
          name: 'commons',
          chunks: 'initial',
          minChunks: 1,
        },
      },
    },
  },
  plugins: [],
  // Something like this will be auto added by this.configure()
  // entry: {
  //   'foo-one.test.js': 'path/to/test/foo-one.test.js',
  //   'foo-two.test.js': 'path/to/test/foo-two.test.js',
  // },
  // plugins: [
  //   new KarmaSyncPlugin()
  // ],
};

How it works

This project is a framework and preprocessor for Karma that combines test files and dependencies into 2 shared bundles and 1 chunk per test file. It relies on webpack to generate the bundles/chunks and to keep it updated during autoWatch=true.

The first preproccessor triggers the build of all the bundles/chunks and all following files just return the output of this one build process.

Webpack typescript support

By default karma-webpack forces *.js files so if you test *.ts files and use webpack to build typescript to javascript it works out of the box.

If you have a different need you can override by settig webpack.transformPath

// this is the by default applied transformPath
webpack: {
  transformPath: (filepath) => {
      // force *.js files by default
      const info = path.parse(filepath);
      return `${path.join(info.dir, info.name)}.js`;
    },
},

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

webpack

webpack configuration (webpack.config.js).

Maintainers


Mika Kalathil
Joshua Wiens Will Farley Daniela Valero
Jonathan Trang Carlos Morales

Current Tags

  • 5.0.0-alpha.3.0                                ...           dev (8 years ago)
  • 5.0.1                                ...           latest (3 years ago)
  • 5.0.0-alpha.6                                ...           next (6 years ago)

55 Versions

  • 5.0.1                                ...           3 years ago
  • 5.0.0                                ...           6 years ago
  • 5.0.0-alpha.6                                ...           6 years ago
  • 5.0.0-alpha.5                                ...           6 years ago
  • 5.0.0-alpha.4                                ...           6 years ago
  • 4.0.2                                ...           7 years ago
  • 4.0.1                                ...           7 years ago
  • 4.0.0                                ...           7 years ago
  • 5.0.0-alpha.3.0                                ...           8 years ago
  • 5.0.0-alpha.2                                ...           8 years ago
  • 4.0.0-rc.6                                ...           8 years ago
  • 5.0.0-alpha.1                                ...           8 years ago
  • 5.0.0-alpha.0                                ...           8 years ago
  • 4.0.0-rc.5                                ...           8 years ago
  • 4.0.0-rc.4                                ...           8 years ago
  • 4.0.0-rc.3                                ...           8 years ago
  • 3.0.5                                ...           8 years ago
  • 4.0.0-rc.2                                ...           8 years ago
  • 3.0.4                                ...           8 years ago
  • 3.0.3                                ...           8 years ago
  • 4.0.0-rc.1                                ...           8 years ago
  • 3.0.2                                ...           8 years ago
  • 4.0.0-rc.0                                ...           8 years ago
  • 3.0.1                                ...           8 years ago
  • 4.0.0-beta.0                                ...           9 years ago
  • 3.0.0                                ...           9 years ago
  • 2.0.13                                ...           9 years ago
  • 2.0.12                                ...           9 years ago
  • 2.0.11                                ...           9 years ago
  • 2.0.10                                ...           9 years ago
  • 2.0.9                                ...           9 years ago
  • 2.0.8                                ...           9 years ago
  • 2.0.7 [deprecated]           ...           9 years ago
  • 2.0.6                                ...           9 years ago
  • 2.0.5                                ...           9 years ago
  • 2.0.4                                ...           9 years ago
  • 2.0.3                                ...           10 years ago
  • 2.0.2                                ...           10 years ago
  • 2.0.1                                ...           10 years ago
  • 2.0.0                                ...           10 years ago
  • 1.8.1                                ...           10 years ago
  • 1.8.0                                ...           10 years ago
  • 1.7.0                                ...           11 years ago
  • 1.6.0                                ...           11 years ago
  • 1.5.1                                ...           12 years ago
  • 1.5.0                                ...           12 years ago
  • 1.4.0                                ...           12 years ago
  • 1.3.1                                ...           12 years ago
  • 1.3.0                                ...           12 years ago
  • 1.2.2                                ...           12 years ago
  • 1.2.1                                ...           13 years ago
  • 1.1.0                                ...           13 years ago
  • 1.0.2                                ...           13 years ago
  • 1.0.1                                ...           13 years ago
  • 1.0.0                                ...           13 years ago
Downloads
Total 154
Today 0
This Week 2
This Month 77
Last Day 0
Last Week 2
Last Month 0
Dependencies (3)
Dependents (2)

© 2010 - cnpmjs.org x YWFE | Home | YWFE