Testing your JavaScript with tape

After writing my last post about using TAP and tape runner for writing JavaScript tests I felt that I wanted to flesh it out a bit more. I’ve previously used Mocha quite extensively since it seemed like the most popular testing framework and at the time I really like the BDD-styled syntax. While I still think Mocha is awesome I’ve come to appreciate a simpler approach for writing tests.

Why?

Besides the points brought up in “TAP & Tape, the awesome way to test JavaScript” I feel like going from a more verbose syntax to a more terse one means that I can write less code while focusing on the tests. Things like describe() and it() gets “replaced” with test() and the actual assertions. Being able to run your tests and independently decide which formatter to use by simply piping the output to it is also something that I find extremely useful.

A basic example

Here’s a basic test:

var test  = require('tape');
var hello = require('./');

test('A thing', function(t) {
  t.plan(1);
  t.equal(hello(), 'Hello world!');
});

To run it all you have to do is:

node test.js

Or if you want a different output formatter:

node test.js | tap-dot

You can also run tape directly on a whole directory of files:

tape test/*.js | tap-dot

While skimming the tests written for prova I saw that Azer Koçulu used assert instead of t, which I think is a nice idea:

var test  = require('tape');
var hello = require('./');

test('A thing', function(assert) {
  assert.plan(1);
  assert.equal(hello(), 'Hello world!');
});

One of the best parts of tape is the test are asynchronous by default, which means that as long as the t.plan() gets passed the right number of assertions it’ll wait for them all to finish.

comments powered by Disqus