MyGit

codymikol/karma-webpack

Fork: 222 Star: 830 (更新于 2024-11-27 22:09:15)

license: MIT

Language: JavaScript .

Karma webpack Middleware

最后发布版本: v5.0.1 ( 2024-02-02 01:00:38)

GitHub网址

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 setting 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

最近版本更新:(数据更新于 2024-10-03 15:26:14)

2024-02-02 01:00:38 v5.0.1

2021-02-02 19:56:00 v5.0.0

2021-01-31 03:55:52 v5.0.0-alpha.6

2020-12-06 22:21:25 v5.0.0-alpha.5

2020-12-06 22:20:16 v5.0.0-alpha.4

2019-06-08 22:56:27 v4.0.2

2019-06-08 01:24:36 v4.0.1

2019-06-08 00:05:54 v4.0.0

2018-11-21 03:02:45 v4.0.0-rc.3

2018-09-14 22:39:14 v3.0.5

主题(topics):

karma, webpack-plugin

codymikol/karma-webpack同语言 JavaScript最近更新仓库

2024-12-22 00:44:14 gorhill/uBlock

2024-12-21 12:14:02 layui/layui

2024-12-21 08:27:48 gethomepage/homepage

2024-12-21 03:44:01 emberjs/ember.js

2024-12-20 02:57:09 nodejs/node

2024-12-19 17:54:54 MHSanaei/3x-ui