Unit Testing a Require.JS App

Require.JS is an open source JavaScript file and module loader. In this blog post, Ben Wilhelm explains how to add unit tests to a Require.JS application without spending a large effort in refactoring the apps.

The blog post start by explaining the difficulty to add unit tests to an existing application, more particularly the issues with mocking the dependencies in your the subjects. The blog post explains how the New York Times development team added unit tests to a Backbone.js javascript application. It contains a lot of code examples. It deals more specifically with the usage of the Spire.js library to sole the mocking problem. Spire.js is an open source dependency injector that can be used to test Require.js modules.

The conclusion is that “Squire provides an easy-to-use, compact API for mocking RequireJS modules. Since all of our application components are RequireJS modules, Squire allowed us to make all of our dependencies injectable at once, without making any changes to our source code. This spared us a considerable amount of refactoring in order to make our application testable.”

Read the complete blog post on http://open.blogs.nytimes.com/2015/01/15/how-to-unit-test-a-requirejs-application/