If you ever want to start unit testing your project, here's a straightforward framework for doing so. First, create a simple Mocha unit test to access your home page. I'm going to use mocha, chai, and zombie. Note that I'm embedding-- sorry about this-- the target URL, but the location for it will be localhost for the time being. Chai adds some nice features to Mocha, such as the 'should' interface.
chai = require('chai')
chai.should()
zombie = require('zombie')
browser = new zombie.Browser()
describe 'Initialization', ->
it 'should load the home page', (done) ->
browser.visit 'http://127.0.0.1:8081/index.html', (error, browser, status) ->
if error then console.log(error)
browser.text('title').should.be.equal('Priority / Ignore')
done()
Secondly, add a script to your grunt.js to call this from within Mocha. I put my tests in the path test/, so in my gruntfile I define the following task:
mocha: {
src: ['test/*_mocha.coffee']
},
/* ... */
grunt.registerHelper('mocha', function(command, test, done) {
var args = {
cmd: 'mocha',
args: ['--compilers', 'coffee:coffee-script', '-R', 'xunit', '-C', test]
};
grunt.utils.spawn(args, function(err, result) {
if (err) {
console.log(err.stderr)
done();
return;
}
fs.appendFileSync('tmp/results.xml', result.stdout);
done();
});
});
grunt.registerTask('mocha', 'Run Mocha Tests', function() {
var done = this.async(),
task = this.name,
sources = grunt.file.expandFiles(grunt.config([this.name, 'src'])),
dest = grunt.config([this.name, 'dest']);
sources.sort();
grunt.file.mkdir('tmp');
fs.writeFileSync('tmp/results.xml', '');
async.forEachSeries(
sources,
function(path, cb) {
grunt.helper('mocha', grunt.config([task, 'cmd']), path, cb);
},
done);
});
My mocha scripts are in coffeescript, so I run mocha with the "use coffeescript" argument, as well as xUnit and "no color" output.
It's now possible to run this script:
grunt server mocha
will run the grunt server, which you should have configured to point to your distribution directory, and then run the mocha script which will attempt to access the page, interpret it with zombie, and in this case verify that the title is what I expect it to be. Grunt's static server will terminate automatically when the tests are completed.
I do it this way because my web applications are often amalgamations of many different sources: Less files processed with Recess, Coffeescript, Require.JS optimized sources, HAML, even HAML-to-HTML-to a JS template function wrapped in Require.JS's define(). Much modern development is like this now, and having a solid set of unit tests that can assert your pages contain what you expect and behave as you anticipate is critical. Zombie is no substitute for in-browser testing, but it's a hell of a good start.
Still, I can't help but feel that it would have been quicker to write the grunt layer in a Makefile.