How to set up Karma and Jasmine with webpack
I’ve been meaning to get back to blogging, and I thought this was a good topic to cover.
After going through some React courses lately, webpack has quickly become an indispensible tool for me. It loads and transpiles everything, and makes a nice neat bundle. It’s my go-to tool for new projects now.
When setting up the unit tests for my GitHub Pull Request Dashboard, things weren’t quite as smooth as with some of my past projects. For promise-poller, I just installed Jasmine, wrote the specs, and ran them with the jasmine command. Nice and easy.
But with a React project, you might need to write some JSX in your tests. Or maybe you just want to use full ES2015 syntax. So setting up the test infrastructure in this case isn’t quite as simple (but still really easy).
I couldn’t figure out how to get just Jasmine running with webpack, but adding Karma did the trick nicely. Here’s a quick overview of how to get it up and running.
First, I’m going to assume you already have a webpack configuration file for your application. You’ll need to tap into this later in the process.
There are a few development dependencies you’ll need to install:
You can install most of these all in one go:
karma-cli is installed separately because it gets installed globally:
Now that all the packages are installed, generate a Karma configuration to start with:
This will ask you a bunch of questions about your Karma test setup. Make sure you tell it you’re using Jasmine. When you’re done, you should have something like this:
Now you just need to pull in your webpack configuration. At the top of the config file, require it in:
Next, add a webpack section to your Karma configuration:
This will expose your webpack configuration to Karma.
Finally, add a preprocessors section:
This lets Karma know that before your tests are run, they need to be preprocessed by running them through webpack.
The final Karma configuration should look like this:
The only thing that will differ in your Karma configuration is the reporter . I prefer karma-mocha-reporter , so that’s what I’m using in my configuration.
From here, you are good to go. Go forth and write your tests using JSX or ES2015. To run your tests, just start Karma from the command line:
Running Karma JS tests as part of a Maven CI build with Webjars
I spent several hours at work recently trying to solve this problem. The solution has a few moving parts to it, so I thought I’d jot down some notes to make it easier on myself and others in the future.
At my workplace we have an established Maven/Java workflow with tooling in place for executing CI builds and deploying built artefacts to our DEV/TEST/PROD environments etc. Some time ago we adopted Webjars as a way to manage static web resources via the Maven dependency management mechanism.
After evaluating some options we settled on the Karma test runner. This is a powerful test runner that supports cross-browser testing, multiple reporting formats and the ability to ‘watch’ project files and automatically re-run tests when a change is detected (very cool!). The easiest way to get Karma running is on NodeJS, which we didn’t have set up in our environment.
How do we integrate Karma-driven tests on a project that uses Webjar dependencies so that the tests can be run locally AND as part of a Maven CI build using our existing tooling?
If you want to skip ahead, a fully working project is available on github. Take a look at the README to get started.
First, some background on the various tools and libraries used for anyone not familiar with them.
Maven is a dependency management and project build management system. It has become a de-facto standard for Java projects. It supports a wide variety of plugins that can be used together to perform arbitrarily complex build processes.
Webjars is a convention for packaging client-side web libraries as JAR dependencies so they can be managed via Java-oriented tools such as Maven. They have the nice property that they can be included and managed as a project dependency in the same way that any regular JAR library can.
Karma is a test runner library for executing JS tests. The tests can be run across multiple browsers and on multiple devices.
If you don’t want to get into the details, the breakdown of the solution is as follows:
- Setup webjar dependencies
- Host webjars using the Jetty Maven Plugin when developing locally
- Setup Karma to run locally using the hosted Webjar dependencies
- Use the Maven Dependency plugin to unpack Webjar dependencies during a CI build
- Create a CI-specific Karma configuration that uses the unpacked dependencies rather than the hosted ones during a CI build
- Use the Frontend-Maven-Plugin to install NodeJS and Karma local to the project during a CI build and execute the Karma tests
- (Optional) Add JUnit style reporting so the Karma test results can be reported on by CI tooling such as Jenkins etc.
Setup your project to use Webjars
This part is easy. Webjar dependencies can be added to your POM just as any other dependency.
The Webjars documentation has instructions for getting Webjars running on a number of platforms. For ease of setup we’ll use a Servlet 3.0 container (see next section).
The easiest way to make the webjar dependencies available is via a Servlet 3.0 container such as Jetty. I used the Jetty Maven Plugin which allows you to launch a fully configured Jetty container using Maven.
File size: 7.17 mb.
Date: 27.07.2014 13:15
File version: 0.8.6.5
Product name: Karma
Parent title: Official Karma Community | Home | Karma
karma-0.8.6.5-setup.exe free download.
If you receive an error karma-0.8.6.5-setup.exe missing:
- You can try to download this file and paste it in directory where it missing
If you receive an error in file karma-0.8.6.5-setup.exe:
- Try to replace it with this one.
If you can't find windows directory try:
1. Press and hold Windows key on your keyboard, then press button R.
2. Enter the command "cmd" and press Enter
3. In a command window enter the command "set systemroot" and press Enter. It will display system directory.