At SoftFinity we use unit testing to test the code we write.  In my humble opinion that should apply to unit testing for JavaScript code too.  Unit tests are a series of small functions that call the main functions of the original JavaScript code with different inputs and test to see if their outputs are what they should be.  The theory is that, if a change gets made to the original function, these tests can be run in isolation on that function and ensure that the rest of the code that relies on that function won’t trip up.  If the test throws an error, the original function should be updated to ensure that test passes.  My preferred unit testing framework in JavaScript is Jasmine as the tests written in its format are easy to read, to group together logically, and don’t require any other libraries to ensure they work correctly.


There is Grunt plug-in to running these Jasmine tests alongside your other tasks, which is simple to use and very powerful.  With its simplest configuration, you tell it the location of your JavaScript files to be tested and the unit test files you have written, and it runs them in turn, aborting the Grunt task if any single test fails to produce the required result.

The helpers ensure that all code is tested and fixed before committing back into your source code repository.  The tests themselves run in PhantomJS which is known as a ‘headless browser’.  It’s named as such since it runs on the command line and has no visible user interface, unlike most browsers.  This allows the JavaScript unit tests to be run within a simulated DOM environment without any visible output.  The output instead is fed back to Jasmine, which determines whether the tests passed or failed.

The Jasmine plugin supports mixins, which provide enhanced functionality.  A personal favorite of mine is the Istanbul mixing which studies the code your unit tests run on and produces a report detailing what percentage of your code is run during those tests.

http://pivotal.github.io/jasmine/
https://github.com/gruntjs/grunt-contrib-jasmine
http://phantomjs.org/