This guide covers the ins-and-outs of tape, a simple TAP-producing test library for node and browsers. The tape API is a small superset of the node core assert module.

This guide was written for Testling. Testling lets you write continuous integration cross-browser tests that run on every git push! Once the tests run you get a badge you can put in your readme with the current browser test status. Here's how it looks:


Click the badge to learn how to write Testling tests!

For an exaustive list of all the methods that tape supports, consult the tape readme.

Simple equality with a plan

Most of the time, you'll just need to plan out a few simple asserts with t.equal(), which uses === internally:

var test = require('tape');

test('basic arithmetic', function (t) {
    t.plan(2);

    t.equal(2 + 3, 5);
    t.equal(7 * 8 + 9, 65);
});

If you want you can leave off the test name and just do:

var test = require('tape');

test(function (t) {
    t.plan(2);

    t.equal(2 + 3, 5);
    t.equal(7 * 8 + 9, 65);
});

Simple equality with an end

If you have an indefinite number of assertions, sometimes it's easier to call t.end() instead:

var test = require('tape');

test('basic arithmetic', function (t) {
    t.equal(2 + 3, 5);
    t.equal(7 * 8 + 9, 65);

    t.end();
});

Deep equality

To compare array and object references deeply, you can use t.deepEqual():

var test = require('tape');

test('deep equality', function (t) {
    t.plan(2);

    t.deepEqual([ 3, 4, 5 ], [ 3, 4, 2+3 ]);
    t.deepEqual(
        { a: 7, b: [ 8, 9 ] },
        { a : 3+4, b: [ 4*2 ].concat(3*3) }
    );
});

Comparing booleans

Just use .ok() to assert truthiness:

var test = require('tape');

test('comparing booleans', function (t) {
    t.plan(1);

    t.ok(3 > 4 || 5 > 2);
});

Negations

For each kind of assertion, prepend (and camel-case) a "not" to turn it into a negative assertion:

var test = require('tape');

test('negatives', function (t) {
    t.plan(3);
    t.notEqual(1+2, 5);
    t.notDeepEqual([1,2], [12]);
    t.notOk(false);
});

Pass/fail

If you need a test to just fail, you can call t.fail():

var test = require('tape');

test('empty map', function (t) {
    [].map(function (x) {
        t.fail('this callback should never fire');
    });

    t.end();
});

Conversely, there is a t.pass() which always succeeds:

var test = require('tape');

test('map with elements', function (t) {
    t.plan(2);

    [2,3].map(function (x) {
        t.pass();
    });
});

More info

You can always add an assertion description as the last argument:

var test = require('tape');

test('more info', function (t) {
    t.plan(2);

    t.equal(1+2, 3, 'basic arithmetic still works');
    t.ok(3+4>5, 'inequalities are as we might expect');
});

Asynchronous

Since we either plan out the number of assertions explicitly with t.plan(n) or end the test explicitly with t.end(), we don't need to do anything differently when our tests make asynchronous calls:

var test = require('tape');

test('asynchronous results', function (t) {
    t.plan(2);

    t.equal(2+3, 5);

    setTimeout(function () {
        t.equal(5+5, 10);
    }, 500);
});

Multiple serial tests

var test = require('tape');

test('first', function (t) {
    t.plan(1);
    setTimeout(function () { t.ok(true) }, 200);
});

test('second', function (t) {
    t.plan(1);
    setTimeout(function () { t.ok(true) }, 100);
});

The 'first' test will run, then the 'second'.

Nesting tests

You probably shouldn't do this very often, but you can have nested tests too:

var test = require('tape');

test('nested', function (t) {
    t.test(function (st) {
        st.plan(1);
        st.equal(1+2, 3);
    });

    t.test(function (st) {
        st.plan(1);
        setTimeout(function () {
            st.pass();
        }, 100);
    });
});

Running a tape test in node

Just run your test file directly with node:

$ node test/def.js 
TAP version 13
# defined-or
ok 1 empty arguments
ok 2 1 undefined
ok 3 2 undefined
ok 4 4 undefineds
ok 5 false[0]
ok 6 false[1]
ok 7 zero[0]
ok 8 zero[1]
ok 9 first arg
ok 10 second arg
ok 11 third arg

1..11
# tests 11
# pass  11

# ok

Running a directory full of tape tests in node

If you npm install -g tape, you get a test runner for running directories full of tests all at once:

$ tape test/*.js
TAP version 13
# defined-or
ok 1 empty arguments
ok 2 1 undefined
ok 3 2 undefined
ok 4 4 undefineds
ok 5 false[0]
ok 6 false[1]
ok 7 zero[0]
ok 8 zero[1]
ok 9 first arg
ok 10 second arg
ok 11 third arg
# (anonymous)
ok 12 should be equal

1..12
# tests 12
# pass  12

# ok

You could also use the test runner from the tap module for more terse output. First npm install -g tap, then do:

$ tap test/*.js
ok test/def.js ........................................ 12/12
ok test/falsy.js ........................................ 2/2
total ................................................. 14/14

ok

In the test runner scripts for both tap and tape you will get lots of output including line numbers when an assertion fails.

Running a tape test in a browser

First npm install -g browserify, then you can do:

$ browserify test.js > bundle.js
$ echo '<script src="bundle.js"></script>' > test.html

Then open test.html in a browser and look at the test output in the debugger. tape writes all its output to console.log() by default.

If you want to run your tests in a real headless browser locally, npm install -g testling then do:

$ browserify test.js | testling
TAP version 13
# defined-or
ok 1 empty arguments
ok 2 1 undefined
ok 3 2 undefined
ok 4 4 undefineds
ok 5 false[0]
ok 6 false[1]
ok 7 zero[0]
ok 8 zero[1]
ok 9 first arg
ok 10 second arg
ok 11 third arg

1..11
# tests 11
# pass  11

# ok

and your test will run in chrome or firefox headlessly, depending which you have installed on your system. The console.log() output is proxied from the browser to your stdout and the testling command generates an exit code by parsing the TAP output.

Running a directory full of tape tests in a browser

To run multiple tests, just use a file glob:

$ browserify test/*.js | testling
TAP version 13
# defined-or
ok 1 empty arguments
ok 2 1 undefined
ok 3 2 undefined
ok 4 4 undefineds
ok 5 false[0]
ok 6 false[1]
ok 7 zero[0]
ok 8 zero[1]
ok 9 first arg
ok 10 second arg
ok 11 third arg
# (anonymous)
ok 12 should be equal

1..12
# tests 12
# pass  12

# ok

Running tape tests on Testling

To get your tape tests running on testling simply add the "testling" field to package.json

"testling" : {
    "files" : "test/*.js",
    "browsers" : {
        "ie" : [ 6, 7, 8, 9, 10 ],
        "ff" : [ 3.5, 10, 15.0, 16.0, 17.0 ],
        "chrome" : [ 10, 20, 21, 22, 23 ],
        "safari" : [ 5.1 ],
        "opera" : [ 10, 11, 12 ]
    }
}

and setup a github hook to point to git.testling.com. Now every time you push to github the tape tests will run in all the specified browsers.

To learn more about how to write testling tests, see this article.

Conclusion

If you need more complicated abstractions in your tests, just npm install them and require() them in your tests. Just be careful that the libraries you're importing also work in browsers.

Tape shows that you don't actually need very much API to test your libraries in a powerful and flexible way that works in node and in browsers.

Comments

Mark Volkmann Permalink
July 04, 2013, 13:24

If the first assert in a test function fails, does it continue running the code in that function or move on to the next test?

July 16, 2013, 06:25

Cool post
My website
http://dekstar.cu.cc

July 16, 2013, 17:04

Nice Post! really informative.

July 16, 2013, 17:08

Good Job! Keep it up...

July 18, 2013, 03:17

Outstanding post, I believe people should acquire a lot from this site.

July 19, 2013, 10:10

All these languages are technical and mostly understood by IT person/student. but in this blog you explain in such easy way i am also able to understand this.

July 19, 2013, 13:52

Thanks a lot.The info would help me with my blog

July 21, 2013, 16:25

I think this post is more valuable for web developer.
thanks for share this post.
Tips for weight loss
thank

July 22, 2013, 00:22

It is a very good blog.Thank you for sharing it.

July 23, 2013, 05:19

Learning a technical language is not an easy thing you must aware about the base and atleast bookish knowledge of technical things...but after reading this blog i not fully understood about testling but get over all knowledge about this.

July 23, 2013, 17:33

thanks for sharing it. it is helpful to learn the codes.

July 24, 2013, 12:46

FOR FREE TURKISH JAVASCRIPT DOCUMENTS: Bildirimler.com

November 18, 2013, 08:23

This is Awesome Post . This is highly Impretive Post,
Generally , we Provide Commodity Market News.

April 23, 2014, 14:22

Writing javascript tests with tape is a great post.

April 24, 2014, 15:24

thanks for sharing java-script programs, its really help me a lot in web development

July 17, 2014, 08:35

Ok it will be practical

August 11, 2014, 10:42

http://www.foothillstattoo.com.au/blog/post/2011/10/ryan_gets_to_do_a_guest_spot_at_westside_tattoo_in_brisbane/
http://hrnetwerk.nl/hrm_kennis.php?dossierId=_Mogelijke_kernkwadranten
http://thehairpin.com/2013/08/whos-the-squarest-jawed-of-them-all#comment-reply
http://www.chicagojournal.com/blogs/12-18-2012/Goodbye_Chicago_Journal!
http://www.prensadefrente.org/pdfb2/index.php/new/2009/01/08/p4361?blog=6&p=4361&c=1&page=1&more=1&tb=1&pb=1&disp=single
http://staceydavid.com/node/5152?page=9
http://www.rochesterrattlers.com/news/8236/rattlers-dominate-in-season-finale-defeat-boston-2314/majorleaguelacrosse.com#_comments
http://www.dlog.pl/spip.php?article8#forum3771
http://joker.com.ar/content/articulo/desaf-o-letras#comment-6988
http://ml.mainelymediallc.com/news/2013-12-27/Things_to_Do/Things_to_Do.html#comment-3705
http://www.suburban-news.org/Conferences/2012RevenueSummit/tabid/307/articleType/ArticleView/articleId/318/Innovation-Mission-2013-Kicks-Off-with-McClatchy.aspx#Comment6600
http://www.localmedia.org/News/tabid/158/articleType/ArticleView/articleId/325/Late-July-Date-Added-For-Goggle-AdWords-Training.aspx#Comment6602
http://gearz.tv/articles/tips/quick-tip-drilling-holes-painted-panels-5150#comment-4003
http://www.zoomcanal.com.co/Producciones/Proyecci%C3%B3nuniversitaria/tabid/533/page/8/type/MostRecent/Default.aspx
http://www.holyground.topeka.net/content/st-josephs-church-sacred-heart-st-joseph-catholic-parish#comment-form

Leave a new comment

(why do I need your e-mail?)

(Your twitter name, if you have one. (I'm @pkrumins, btw.))

Type the word "network": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.

Advertisements