Last Updated: February 25, 2016
·
937
· adamyanalunas

Mask heterogenous test suites with cake

Sometimes you can't fit all of your test suites into one runner. Client-side code should run in a real client, not in jsDom. Node code can't run in a browser. As soon as you ask coworkers or contributors to run two commands to test you fail.

What to do? Use the tooling at hand to make everyone's lives easier.

In this example I'll use cake and a Cakefile although you could just as easily use make and Makefile if you're not using CoffeeScript.

In your Cakefile make these stub tasks: test, test:frontend, and test:node. If you have other applicable test suites, make empty tasks for them as well. Test will look something like this:

task 'test', -> 
    testFrontEnd (err, stdout) ->
        process.exit(1) if err
        testNode (err, stdout) ->
            process.exit(1) if err

See what I did there? Serial test calls. You could easily make them parallel or use many cleaner patterns for organizing code flow. This is just an example.

You'll also notice I called two private functions: testFrontEnd and testNode. You'd be right to guess that test:frontEnd just exposes a call to testFrontEnd for Cake, as test:node does the similar thing.

The final pieces are actually running the tests. My testFrontEnd looks something like this:

testFrontEnd = (cb) ->
    console.log 'Running front-end tests'
    exec "PHANTOMJS_BIN=#{__dirname}/node_modules/phantomjs/lib/phantom/bin/phantomjs testacular start #{__dirname}/tests/testacular.conf.js --single-run", (err, stdout, stderr) ->
        console.error err if err
        console.log stdout
        cb?(err, stdout)

That mess with PHANTOMJS_BIN is purely for testacular's sake. It assumes PhantomJS is in one location. If it's not there you have to define it. Gross but necessary when you work on a team and can't force people to install PhantomJS in a certain location.

Now to run your test suites you can just

cake test

and all your suites will run and the babies will stop crying and oh the double rainbows you'll have.