Package detail

karma-webpack

webpack-contrib1.8mMIT5.0.1

Use webpack with karma

webpack

readme

npm node coverage

Karma Webpack

Use webpack to preprocess files in karma

Install

npm npm i -D karma-webpack

yarn yarn add -D karma-webpack

Usage

karma.conf.js

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

    // make sure to include webpack as a framework
    frameworks: ['mocha', 'webpack'],

    plugins: [
      'karma-webpack',
      'karma-mocha',
    ],

    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' ]
    },

    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_') + Math.floor(Math.random() * 1000000),
  },
  stats: {
    modules: false,
    colors: true,
  },
  watch: false,
  optimization: {
    runtimeChunk: 'single',
    splitChunks: {
      chunks: 'all',
      minSize: 0,
      cacheGroups: {
        commons: {
          name: 'commons',
          chunks: 'initial',
          minChunks: 1,
        },
      },
    },
  },
  plugins: [],
};

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'
}

Current Maintainers


Cody Mikol

Previous Maintainers

Previous maintainers of the karma-webpack plugin that have done such amazing work to get it to where it is today.


Ryan Clark

April Arcus

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

changelog

Change Log

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

5.0.1 (2024-02-01)

Fixes

  • security fixes

5.0.0 (2021-02-02)

No changes, just a new stable release.

5.0.0-alpha.6 (2021-01-30)

Bug Fixes

  • automatically fix missing webpack framework and report a warning (ea5dc8e)
  • fix an issue where multiple karma-webpack processes could not run in parallel (ea3dabe)
  • bump hotfix dependencies (98b3ec9)

5.0.0-alpha.5 (2020-12-06)

Bug Fixes

  • change the webpack peer dependency to webpack v5 (2e0ca74)

5.0.0-alpha.4 (2020-12-06)

Bug Fixes

  • fix compatibility issues for webpack v5 (8d7366f), closes #452
  • remove deprecation warning for .watch() (4fe1f60)

5.0.0-alpha.3.0 (2019-03-07)

Bug Fixes

5.0.0-alpha.2 (2019-02-13)

Bug Fixes

  • karma-webpack: normalize paths to be compatible with windows (b783e1c)

5.0.0-alpha.1 (2019-01-01)

Bug Fixes

  • karma-webpack: add dynamic imports to main bundle (#384) (288a8c2)
  • karma-webpack: disable karma watch; use webpack watch only (#386) (1696bfd)

5.0.0-alpha.0 (2018-12-13)

Features

  • karma-webpack: Add webpack as framework and use shared bundles (#380) (2ab7ad5), closes #379

BREAKING CHANGES

  • karma-webpack: webpack needs to be added to frameworks
// old:
frameworks: ['mocha'],

// new:
frameworks: ['mocha', 'webpack'],
  • karma-webpack: old alternative usage is no longer recommended
  • karma-webpack: webpack-dev-middleware removed
  • karma-webpack: default webpack configuration changed drastically

4.0.0-rc.5 (2018-11-30)

Bug Fixes

  • karma-webpack: Do not unify "colors" property if webpack "stats" is a string (#376) (840dea2), closes #375

4.0.0-rc.4 (2018-11-28)

Bug Fixes

  • karma-webpack: Fix publicPath to be Windows-compatible (#373) (fca13b9), closes #362
  • package: restore prepare script in package.json so that npm install from github works (#367) (3e1f3e4)

Features

  • karma-webpack: unify webpack and Karma colour config (#356) (9559306), closes #332

4.0.0-rc.3 (2018-11-20)

Bug Fixes

  • karma-webpack: handle multiple outputs correctly (#361) (41edac8)

4.0.0-rc.2 (2018-09-07)

Bug Fixes

  • karma-webpack: don't include the os.tmpdir (output.publicPath) (#338) (66f4cd7)
  • karma-webpack: normalize paths (windows) (#351) (2145ec2)
  • package: 64 vulnerabilities (2 low, 62 moderate) (audit) (9816152)

4.0.0-rc.1 (2018-09-03)

Bug Fixes

  • karma-webpack: correctly map entries to outputted assets (config.output) (#347) (ab4dde9)

4.0.0-rc.0 (2018-09-01)

Bug Fixes

  • karma-webpack: compilation hangs when adding a file (#345) (ddf161d)
  • karma-webpack: allow filename and chunkFilename to be overridden (config.output) (#336) (c256d87)
  • karma-webpack: disable optimization by default (webpack >= v4.0.0) (#325) (51bdcaa)
  • mocha-env-loader: add webpack >= v4.0.0 support (#341) (39ff49c)

Chores

  • package: update webpack-dev-middleware v2.0.6...3.0.1 (dependencies) (1741bca)

Code Refactoring

  • karma-webpack: upgrade plugin system (tapable) (f275d40)

BREAKING CHANGES

  • package: requires webpack >= v4.0.0
  • karma-webpack: requires webpack >= v4.0.0

4.0.0-beta.0 (2018-03-19)

Chores

  • package: update webpack-dev-middleware v2.0.6...3.0.1 (dependencies) (ffa0a9c)

Code Refactoring

  • karma-webpack: upgrade plugin system (tapable) (395eab4)

BREAKING CHANGES

  • package: requires webpack >= v4.0.0
  • karma-webpack: requires webpack >= v4.0.0

3.0.0 (2018-03-19)

Chores

  • package: update webpack-dev-middleware v1.12.0...2.0.6 (dependencies) (#318) (0c78eaf)

BREAKING CHANGES

  • package: requires webpack >= v2.0.0
  • package: requires node >= 6.9.0

2.0.13 (2018-02-27)

Bug Fixes

  • package: use babel-runtime is a dependency (dependencies) (#312) (c45cdf3)

2.0.12 (2018-02-26)

Bug Fixes

  • package: add babel-runtime (devDependencies) (#310) (6d5fa1c)
  • package: downgrade source-map v0.7.0...0.5.6 (#309) (070e04e)

2.0.11 (2018-02-25)

Bug Fixes

2.0.10 (2018-02-25)

Bug Fixes

  • karma-webpack: correctly coalesce idx ({Number}) in filepaths (#287) (5fa2409)
  • correctly escape {Regex} in custom file handler (#293) (906ed52)
  • disable webpack rebuilds on karma --single-run (41d1912)
  • package: add webpack >= v4.0.0 (peerDependencies) (#301) (99ddad8)

2.0.9 (2017-12-14)

Bug Fixes

  • ensure webpack paths include a trailing slash (#285) (be2b0e8), closes #284

2.0.8 (2017-12-14)

Bug Fixes

  • use os.tmpdir() to safely store _karma_webpack_ (#279) (0616dda)

2.0.7 (2017-12-14)

DEPRECATED due to publishing issue

2.0.6 (2017-11-10)

Bug Fixes

2.0.5 (2017-10-08)

Bug Fixes

  • pass on error to callback for better error messages (#262) (466581f)
  • use file.originalPath instead of file.path (#218) (0d075b0)

2.0.4 (2017-07-07)

2.0.3 (2017-03-15)

Bug Fixes

2.0.2 (2017-01-19)

Bug Fixes

  • readFile: handle path doesn't exist error (#208) (907ed72)

2.0.1 (2017-01-11)

Chores

  • release: patch version for release issue. No code changes.

2.0.0 (2017-01-11)

Chores

  • package: update webpack peerDependencies. (9fd5fdf)

Bug Fixes

  • config: webpack rc4 schema enforcment (fixes #193) (e6a3cb7)

BREAKING CHANGES

  • config: Remove entry:{} from test configurations

When updating to "webpack": "2.2.0-rc.4" & "karma-webpack": "1.8.2" you have to pull the entry property if it's set to an empty object so it defaults to a function within karma-webpack

1.8.1 (2016-12-27)

Bug Fixes

1.8.0 (2016-08-07)

Bug Fixes

  • build: Removes dist from scm (#158) (9ea6921)
  • config: webpack rc4 schema enforcment (fixes #193) (e6a3cb7)
  • provider: no provider for variable name Fix #146 (43f18d3)

Features

  • webpack: add support for webpack2.1.0-beta (bdd8c80)
  • webpack: add webpack blocker (03f6495)
  • karma: karma execution blocker (d776068)
  • webpack: support chunks without errors (7334dbc)