Test Anything JS

A Test-Anything-Protocol library for Node.js

npm install tap
tap test/*.js

Mocha-like DSL

If you prefer to use a BDD-style DSL like mocha instead of the traditional t.whatever(), tap lets you do that!

You can do this by using the methods on the tap.mocha object, or dump them into the global namespace using tap.mochaGlobals().

So, instead of this:

// tap.js
var t = require('tap')
t.test('Array.indexOf', function (t) {
  var array = [1, 2, 3]
  t.test('when item is not found', function (t) {
    t.test('does not throw an error', function (t) {
      array.indexOf(4)
      t.end()
    })
    t.equal(array.indexOf(4), -1, 'returns -1')
    t.end()
  })
  t.end()
})

You can do this:

// bdd.js
require('tap').mochaGlobals()
var should = require('should')
describe('Array.indexOf', function () {
  var array = [1, 2, 3]
  context('when item is not found', function () {
    it('does not throw an error', function () {
      array.indexOf(4)
    })
    it('returns -1', function () {
      array.indexOf(4).should.equal(-1)
    })
  })
})

Running these with the spec reporter results in this output:

$ tap -Rspec tap.js bdd.js

tap.js
  Array.indexOf
    when item is not found
      ✓ does not throw an error
      ✓ returns -1

bdd.js
  Array.indexOf
    when item is not found
      ✓ does not throw an error
      ✓ returns -1


  4 passing (527.355ms)

The following functions are provided:

Using the mocha-like BDD interface defines tests hanging off of the root tap object, so tests defined in this way will always start at the “top level”, even if they are defined within a t.test(...) function.