Jazzed up Jasmine testing!
JavaScript Batchfile
Switch branches/tags
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



Jazzmine is an addition to Jasmine, adding and modifying it to work nicer with AMD modules and async code.


Install Jazzmine using npm

npm install jazzmine

Module loading

Jazzmine is made to test individual AMD modules. This is done by loading the model, and any other dependency, right in the describe. For example:

describe("a test with dependencies", ["myLittleModule"], function(myLittleModue){
  it("should be a module", function(){


This way you can load the module you want to test, and any other module needed for the test to run, right in the describe statement.

Jazzmine will wait until all of the dependencies have loaded before it runs the tests.


The because function is exactly the same as beforeEach. It is included as it works better with behaviour driven code; the because function describes what triggers what we want to test. For example:

describe("when a variable is set", [], function(){
  var variable;

    variable = "a value";

  it("should have set the variable", function(){
    expect(variable).toBe("a value");


Async tests

Jasmine has horrible support for async testing. Jasmine has adopted the same async support as Jazzmine has had, so continue using it like you always have. The beforeEach, afterEach, because and it functions support async by using a done parameter, like so:

it("should wait until the done function is called", function(done){


  }, 1000);



The module you want to test usually has dependencies to other modules, some of which you might want to replace during testing. Jazzmine lets you mock these dependencies during testing. The test files can mock dependencies independently of each other, which means everything is reset for your next test. This is done using moquire. For example:

describe("mock out a dependency", {
  "someModule": function(){ return 5; }
], function(
  it("should swap out the dependency", function(){


Global matchers

Matchers can be added to every test by calling jazzmine.addMatchers(). This method can either take an object of matchers, similar to Jasmine, or it can take a function with a callback for async registering matchers. These functions should be called before calling jazzmine.onReady().

  toBeSomething: function(){
    return {
      compare: function(actual){
        var result = actual === "something";
        var notText = result ? " not" : "";

        return {
          pass: result,
          message: "expected " + actual + notText + " to be something"

  require(['Matchers/toBeSomethingElse'], function(toBeSomethingElse){
      toBeSomethingElse: toBeSomethingElse

Setup with Karma

Jazzmine will work with any test runner, but here is an example of using it with Karma. Install Karma with npm:

npm install karma

Next add a karma.conf.js file with all the files karma should load and make available. Because we are using requireJS, we don't have to load the modules we are testing; they will be loaded by the tests that need them. But karma needs to be told to include them! Here is an example karma.conf.js:

module.exports = function(config){

    frameworks: [

    files: [
      //add other libraries needed for testing here (for example sinon)

      {pattern: 'source/**/*.js', included: false},
      {pattern: 'test/Mocks/**/*.js', included: false},
      {pattern: 'test/Matchers/**/*.js', included: false},
      {pattern: 'test/Given/**/*.js', included: false},

    exclude: [


    reporters: ['dots'],

    autoWatch: true


This file assumes your project is set up with a source and test folder next to the karma.conf.js file. We need to configure requireJS with the correct paths, baseUrl and any other options needed for your project. This config is usually pretty similar to your main.js config file. The test/specs-main.js contains this configuration. Here is an example of the specs-main.js file:

  // Karma serves files from '/base'
  baseUrl: '/base',

  paths: {
    "Mocks": "test/Mocks",
    "Given": "test/Given",
    "Matchers": "test/Matchers",


Notice the last line, which starts the karma test runner once all the tests have loaded.

To start the tests, run karma from the directory with the karma.conf.js file:

karma start