Git Product home page Git Product logo

karma-webpack's Introduction

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

Maintainers


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

karma-webpack's People

Contributors

8bitdesigner avatar darsee avatar dignifiedquire avatar goldhand avatar gyandeeps avatar ibash avatar jambonrose avatar joshwiens avatar kidkarolis avatar kpdecker avatar lendar avatar limonte avatar mattlewis92 avatar michael-ciniawsky avatar michaelanthonymain avatar mikaak avatar mkawalec avatar nemtsov avatar pine avatar rodmax avatar rogeriochaves avatar samwalshnz avatar silviu-marian avatar simenb avatar sokra avatar tb avatar tjenkinson avatar tschaub avatar vinteo avatar zuzusik avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.