This is a boilerplate for unit testing Backbone applications in a continuous integration environment.
You will need Node.js and Mocha installed on your continuous integration environment.
Once you have Node and Mocha installed, clone Bitplate:
git clone git@github.com:flashbackzoo/bitplate.git
Install the dependencies:
npm install
Set up the Backbone test application:
bower install && grunt
Run tests from the bitplate/test directory with:
make test
Here are some things to watch out for.
If you're using third party libraries that depend on jQuery (jQuery plugins) you may have to convert the plugin to AMD and pass in jQuery by dependency reference.
For example a plugin might be structured like this:
(function (window, $) {
// Plugin code...
})(this, jQuery);
In the above example the plugin passes in this
(a reference to window
) and the jQuery object. In Node.js the plugin is loaded inside a closure which means this
does not refer to window
(Node.js has no concept of window or DOM anyway) and jQuery
is undefined.
The solution is:
define([jquery], function ($) {
// Plugin code...
});
Now jQuery is loaded by dependency reference. window
is not required because in the test runner we assign a global jsdom object to window
.
Testing singletons can be tricky. Because Node.js uses a different instance of RequireJS to the front-end application, a different instance of your singleton can be returned.
One way to get around this is attach a method that returns the singleton to the object you're testing.