My first Mocha test

OK I’m writing tests in Mocha now! Yay! But what a lot of boilerplate. I want to test a javascript file that works great in a browser. It’s named “lol-netlog-parse.js” and contains a single top level function named parseLolNetlog. Let’s just do a fake test for now.

require('chai').should();
var parse = require("../webapp/lol-netlog-parse")

describe('lol-netlog-parse', function() {
    describe('parseLolNetlog', function() {
        it('Should throw an error', function() {
            "foo".should.equal("bar");
        });
})});

This actually works. But only if I wrap my browser code in some require.js boilerplate to set exports. But then the exports object doesn’t exist in browsers, so I have to do extra work to provide a stub exports object in the browser so that it will still run.

It’s also 7 lines of code for a 1 line test. All test frameworks have their boilerplate, and I understand why this all helps with more complex text harnesses. Still sometimes I think Python doctest had the write idea.. The relative path in the require is a pretty ugly wart too, maybe if I knew more about Node and include paths I wouldn’t have to do that.

I’ve written this in the BDD style, where the function “suite” is named “describe” in some vague attempt at literate programming. I did this because it’s the default style Mocha wants and I don’t want to have to add the “-u” command line flag to use the TDD style. It’s very clever that the code supports 4+ different ways to write unit tests but that’s 3+ more than is necessary. One way to do things; it’s a virtue.

I’m fully embracing the BDD style and using chai’s scary Object prototype manipulation to see if I like it.