This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

Initialize a Webpack Project with Karma for Testing

5:22 Tools lesson by

There are several steps involved with setting up Karma to work on your webpack project. In this first step we'll initialize our karma configuration and setup our package.json scripts.

Get the Code Now
click to level up comment guidelines


There are several steps involved with setting up Karma to work on your webpack project. In this first step we'll initialize our karma configuration and setup our package.json scripts.


Maybe it's a good idea to clarify that Karma is for browser testing, while i had a fun time setting up until the vid 15, where i found out it can't be used to test node.js (guess i'll have to test with Narma to run karma inside NW.js environment, tough i would like it to use Electron better).

Actually never mind, i managed to get it working by using karma-nodewebkit-launcher, it's a weird workaround but it works.

In reply to
Kent C.

Hey Carlos,
If you want to write tests for code that can run in Node (React included actually), then I highly recommend using Jest. I have a handful of lessons on this topic as well: Good luck!

In reply to Carlos

To set up our project to use Karma for testing, we're going to need to do a few things. We'll start out by installing a couple dependencies. We'll NPM install as dev dependencies, Karma, karma-chrome-launcher to launch Chrome to run our tests, and then Mocha for our testing framework, and then karma-mocha to make testing with Mocha and Karma a little easier.

We'll go ahead and install these dependencies. With these dependencies installed, we can verify that here in our package.json. We have those there. We're now going to initialize Karma. To do this, we're going to run in the node_modules directory in the bin, Karma.

As we just installed Karma, the Karma binary is now available in our node_modules directory, and we'll run the init script. Here, we're going to be using the Mocha framework. We will not be using RequireJS. We will be using Chrome.

It needs to know where we're going to be putting our test files. I'll go ahead and create a test file right now, controller.test.js, right next to the controller.js file. Our test files will be in source/js/***.test.js.

We'll use that glob so that it goes through the entire JS folder recursively. We don't need to exclude any patterns. That will capture only the patterns we want to include. We'll go ahead and ask Karma to not watch all the files, and run tests on change. We'll do that ourselves elsewhere.

We can look at our Karma config file that was generated for us. I'm going to go ahead and clean this up a little bit. With that cleaned up, let me go ahead and explain a little bit what's going on here. We have a function that is being exported from this module.

I just call it set Karma config. It accepts a config, and it sets this object on this config. This object has a base path. That just means that the files, exclude, and preprocessors, those values will be relative to that base path. We just left it at the default of an empty string.

The framework we chose is Mocha, and so it's going to use the karma-mocha plugin to load Mocha for our testing framework. The files that we want to load into the browser are files that match this glob. We don't need to exclude anything. I'll just remove that.

We will need preprocessors eventually, but we'll add that a little bit later. I'll remove that. Our reporters, we're just going to use the default progress reporter. The port is the default. We'll leave the colors the same, and the log level.

Auto-watch is false, and we'll add a watch mode a little bit later. The browsers is Chrome. We could add Firefox, and Internet Explorer, and different browsers here, but we'll just stick with Chrome. Single-run, we're actually going to set to true here.

We'll set up a watch mode here in a little bit. Concurrency is infinity. That's the default there. Let's go ahead and add a little test here. I'll just go ahead and paste that in. With that, we're going to want to add a test script to our package.json script so that we can run the test when we like.

We'll add test here, and it will simply be Karma start. We'll default to using this Karma cont file, so it will load our options that we specified in there. If we run NPM t, which is short for NPM test, it goes ahead and runs all of our tests.

It executed the one test that we have successfully, and we're good to go. Let's go ahead and just add a watch mode as well, watch test. This is great for doing test-driven development. You run the test every single time you make a change to the code base.

Here, we'll run NPM test, the exact same as this script, but we're going to pass along a couple arguments, auto-watch, and no single-run. With that, we can run NPM run watch test. We can start developing.

It's run one test. If we add another here, it runs our test again. We have two successful tests. If we go ahead and throw a new error, failure, then we're going to get that one of our tests failed. We're good to go with our watch mode.

Let's go ahead and review really quickly what we had to do to set this up. First, we installed a couple dependencies, Karma, karma-chrome-launcher, and karma-mocha, and then the Mocha testing framework.

We ran Karma init. In the node_modules bin, we have this Karma binary right here. We utilized that in our command line to run the init script. Karma generated this for us. We made a few slight modifications, simply changing single-run, as well as removing a couple unneeded properties.

We created our controller test file, where we added a couple of tests. We added our test and watch test scripts so that we could run these regularly as we're developing our code. One last thing that we're going to do while we're here is we have this validate script that runs as part of our pre-commit hooks.

We're going to go ahead and add the test script to the things that are run every single time we commit, that way we don't push any commits that are going to break our test. That's how you initialize your project Karma config.

Joel's Head
Why are we asking?