Last active

Embed URL


SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Browser Unit Testing with Backbone Mocha Chai and RequireJS

View SpecRunner.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
baseUrl: '/backbone-tests/',
paths: {
'jquery' : '/app/libs/jquery',
'underscore' : '/app/libs/underscore',
'backbone' : '/app/libs/backbone',
'mocha' : 'libs/mocha',
'chai' : 'libs/chai',
'chai-jquery' : 'libs/chai-jquery',
'models' : '/app/models'
shim: {
'chai-jquery': ['jquery', 'chai']
urlArgs: 'bust=' + (new Date()).getTime()
define(function(require) {
var chai = require('chai');
var mocha = require('mocha');
// Chai
var should = chai.should();
], function(require) {;
View SpecRunner.js
1 2 3 4 5 6 7 8 9 10 11
<meta charset="utf-8"/>
<title>Backbone Tests</title>
<link rel="stylesheet" href="libs/mocha.css"/>
<div id="mocha"></div>
<script data-main="SpecRunner.js" src="/app/libs/require.js"></script>
View SpecRunner.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
define(function(require) {
var models = require('models');
describe('Models', function() {
describe('Sample Model', function() {
it('should default "urlRoot" property to "/api/samples"', function() {
var sample = new models.Sample();
View SpecRunner.js
1 2 3 4 5 6 7 8 9 10 11 12
define(function(require) {
var Backbone = require('backbone');
var models = {};
models.Sample = Backbone.Model.extend({
urlRoot: '/api/samples'
return models;

Thanks for this! are there any differences for using 'expect' over 'should' in chai? or is it down to personal preference?

Thanks for sharing. I'm using a similar structure which works fine inside the browser but it fails when using mocha-phantomjs ( Did you ever test your solution with mocha-phantomjs? (

Thanks for this gist, very useful.

I had to add a line after setup, to let mocha continue run tests after first fail.


@craigrich : Check out this page for the differences between expect & should.

+1 for this gist. Awesome!

Thanks for sharing. I have searched for hours for such a clear example. Found lots. This one really helped.

@AlexZeitler To run with mocha-phantomjs:



    if (window.mochaPhantomJS) {;
    else {;

in SpecRunner.js

to use with expect i had to add th following line to SpecRunner.js

expect = chai.expect;  // note no 'var'


var should = chai.should();

(actually, I replaced that line.

@rwestgeest, thanks so much! I was searching for a solution to @AlexZeitler's same problem for a couple hours.

Thank you, very useful gist.
Is it possible to run this tests with mocha command in terminal without phantomjs?

Very useful, thanks a lot

Thanks for this great gist!

I also had to add shims for mocha and chai to get this to work via grunt test:

shim: {
    mocha: {
         exports: 'mocha'
    chai: {
         exports: 'chai'

Thanks @rwestgeest got tripped by same issue head banging for a little while there..

Coolness, thank you.

Would someone be kind enough to explain how var should = chai.should() works when using RequireJS like in this example? Using "var" will scope "should" to within that single require function, therefore making it not available in the test fixtures. When I follow the example, it does not make "should" available in my test fixtures. Instead, i have to use the alias Should(), and I have to assign it to the window object like this: window.Should = chai.Should()
How are all of you able to get "should" to work in your tests without doing what i just described?
When i do this then, in my tests, i have to use Should, instead of should...
Here's a little explanation of why you have to do this, if you're making "should" global:
and if you're NOT making it global, then how is it available in your tests?

@michaelcox, great work, thank you - very useful.
Just one correction. The filename model-test.js doesn't match the actual filename (model-tests.js).

In specRunner.js it should read:

Also, one tiny tiny little thing (not really a bug but more for your information), your require call, requires require :-) Clearly if require is already in scope (its global) then your require call, really shouldn't be requiring itself.
This would be more correct:
require(['chai', 'chai-jquery', 'mocha'], function(chai, chaiJquery, mocha){

// Init Chai
chai.should(); //initializes chai.should()

/*globals mocha */

], function(require) {;

we require mocha in the block above, but what is passed in is wrong. This can be corrected by adding this to the shim section:
shim: {
'mocha': { exports: 'mocha'},

And finally....
The line:
// Chai
var should = chai.should();

Is indeed misleading and I can't explain the rational behind it, but I can tell you that the magic occurs because the first time you call chai.should() the should() function is defined and initialised (it always helps to read/debug through these libraries - treating any JS library like a blackbox is a mistake - in almost all cases.
For me things worked after I changed the filename bug, and I simply changed the chai.should() line above to be a tiny bit more helpful to me:
// Initialize the Chai.should() function

Still though, it is weird and understandable that it caused confusion.


Thanks @steeren - it's been a while since I wrote this, but I fixed the two things you pointed out. Yes I wouldn't need to require "require", and the link to model-tests.js was incorrect.

For the "mocha" require, however, I think it should work without a shim. The latest version of mocha at least has support for requirejs.


A link to the original blog post as well as some additional thoughts on unit testing in backbone can be found here:

Very useful, thanks a lot

I've been looking for something like this for weeks, thank you so much!

I'm having difficulty loading mocha (it says: "Uncaught TypeError: Cannot read property 'setup' of undefined"). The reason is because the variable "mocha" is undefined. I downloaded mocha.js from but for some reason that doesn't seem to be the right code. I looked online and everyone says to use npm and nobody mentions copying. Can someone clarify?

sfahlberg - take a look and see if my gist helps you. I changed a few things from this one because I had a some problems on account of the fact that I didn't want my index.html to consist of the test code, but I remember I also had problems like the one you're describing and they are gone now.

Let me know if this helps.

And michaelcox, I just wanted to thank you again for this gist, I have been testing successfully for weeks now and honestly don't know if I would have gotten to this point without it. You added some much-needed clarity to a hair-pulling process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.