In week 3 of Intro to Programming, we learned about Behavior-Driven Development. We used BDD to break our code down to small, easy-to-test behaviors, also known as specs. Now it’s time to test our back end code using JavaScript-specific unit-testing tools like Jasmine.

Introduction to Jasmine

There are many JavaScript testing frameworks; two of the most popular are Jasmine and Mocha. These libraries have many similarities, and after learning one, you can quickly learn the other.

Both Jasmine and Mocha are used for writing unit tests but they do not actually run these tests! We need a separate type of tool called a test runner for that.

One option is to download the Jasmine package, which comes with a SpecRunner.html file to load test results in the browser. But we’ll actually take a different approach. After familiarizing ourselves with Jasmine, we’ll learn to use a test-runner called Karma to run our automated tests.


First we’ll use npm to install a package called jasmine-core. After we install this package we can write tests using Jasmine code.

As always, we'll create a package.json file by running $ npm init. Make sure to install webpack, webpack-cli, eslint, eslint-loader and any other packages you would like to use in this project (webpack-dev-server, clean-webpack-plugin, css-loader, style-loader, html-webpack-plugin, etc.). Don't forget to configure webpack in a webpack.config.js file and to update the scripts section of your package.json file appropriately.

Now we can install the Node module for Jasmine:

$ npm install [email protected] --save-dev

Next we need to install a helper package for Jasmine to use commands in the CLI to create tests with Jasmine.

$ npm install [email protected] --save-dev

Note that this command installs the latest version of jasmine-core if jasmine-core is not already added to package.json. This is why we needed to install jasmine-core first. If we wanted the latest version of jasmine and jasmine-core we would only need to run $ npm install jasmine --save-dev. However, since we want a specific version of jasmine-core we need to install it separately before installing a specific version of jasmine.

Next, we'll initialize Jasmine:

$ ./node_modules/.bin/jasmine init

Last, we’ll make a small update in our package.json file. Open it with Atom and make this change:

"scripts": {
  "test": "jasmine"

Now if we run the command $ npm test our output shouldn't contain any errors and instead we should see something like this:

> [email protected] test
> jasmine


No specs found
Finished in 0.002 seconds