Use Karma for Unit Testing with Webpack

Kent C. Dodds
InstructorKent C. Dodds

Share this video with your friends

Send Tweet

When writing tests run by Karma for an application that’s bundled with webpack, it’s easiest to integrate webpack and Karma directly together. In this lesson we’ll see how to utilize the karma-webpack plugin and reuse our existing webpack configuration to preprocess our test files with webpack.

samuel
samuel
~ 5 years ago

Any suggestions on how to handle the CommonsChunkPlugin? I am getting an error webpackJsonp in my testing file.

Thanks

Kent C. Dodds
Kent C. Doddsinstructor
~ 5 years ago

Hey! Actually, you might like my other lessons about Webpack. Specifically this one. Basically you disable CommonsChunkPlugin in test mode. I also recommend you give this a look to help make that easier :)

Good luck!

Carlos
Carlos
~ 4 years ago

Things to note if dealing with typescript like me:

  1. Take account for the Mime property in karma.conf.ts for typescript files or the test won't even run (because the karma browser doesn't recognize the mime type), for example like:
const karmaConfig = (config) => {
  config.set({
    ...
    mime : {
      'text/x-typescript': [
        'ts',
        'tsx'
      ]
    },
    ...
  1. Remember when using the globs, that typescript files have ts extension.