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

Kaur deep Permalink
December 04, 2014, 03:31

Excellent and knowledgeable links and post.thanks for sharing.
Now, The Time has come for the Happy New Year 2015. It wil bring a lot fo happinesin our life.
Happy New Year 2015
And I am sure, You wil anjoy this new year day with yoru fiends and wil send some images wishes,
New Year 2015

Happy New Year 2015 Wishes
Greetings to yoru close ones, Your friends, You lover, etc..
Happy New Year 2015 Images
Even I am waititng for that day, because it just changes our life.
Happy New Year 2015 Greetings

Happy New Year 2015 SMS
So, That's why I am commenting here to wish you a very happy new year.
Happy New Year 2015 Messages
I hope this New year 2015 wil bring success to your website. But before that Merry Christmas is coming.
Merry Christmas Quotes
It is also a big event for everyone.
Merry Christmas Pictures
Everyone knows that Christmas is a day when People seems to be very happy, they just leave all sadness behind.
Merry Christmas Wallpapers
And I am sure, you also enjoy this Merry Christmas day of 2014.
Christmas day Images

Christmas stocking
But if you are not looking out to enjoy this christmas, then you are missing out on a big thing, because it really brings happiness.
Merry Christmas Images

Merry Christmas 2014
So, what you are waiting for?
Merry Christmas 2015
Enjoy This Merry Christmas day. Cheers :).

Diana Permalink
December 17, 2014, 15:18

This article is quite helpful and informative too. I enjoyed a lot. Thanks for sharing such a great article.

christmas quotes
christmas wishes quotes
merry christmas quotes
funny christmas quotes
inspirational christmas quotes
christmas eve quotes
best christmas quotes
famous christmas quotes
happy christmas quotes
christmas blessing quotes
christmas greeting quotes
quotes about christmas
christmas inspirational quotes

merry christmas message for Whatsapp
christmas message for Whatsapp
Christmas Messages for Whatsapp
christmas message
christmas greetings message
christmas greeting messages
christmas greetings message
christmas greeting message
christmas messages
christmas card messages
christmas greeting card messages
christmas messages for cards
christmas sms messages
christmas message for friends
merry christmas message
christmas message for family
christmas wishes messages
christmas cards messages
funny christmas messages
top christmas messages
best christmas messages
besutiful christmas messages

christmas greeting
christmas greeting cards
christmas greetings for cards
christmas greeting words
christmas wishes greetings
christmas greetings wishes
free christmas greetings
merry christmas greeting
christmas cards greetings
christmas card greetings
christmas greetings
christmas greeting card sayings
christmas greetings sayings

christmas wishes
christmas wishes text
merry christmas wishes
christmas wishes messages
christmas wishes for cards
best christmas wishes
funny christmas wishes
christmas wishes for friends
christmas wishes words
christmas wishes sms
christmas wish
merry christmas wishes messages

christmas wishes sayings
christmas greeting card sayings
christmas greetings sayings
What to Write in Christmas Card

christmas wallpaper
free christmas wallpaper
christmas wallpapers
merry christmas wallpaper
christmas wallpaper hd
christmas desktop wallpaper
free christmas wallpapers
merry christmas wallpapers
christmas tree wallpaper
animated christmas wallpapers
hd christmas wallpapers
wallpaper christmas

christmas tree decoration
christmas tree decorating ideas
christmas tree decorations
christmas tree decoration ideas
christmas tree decorating
christmas tree decorations ideas

Thanks for sharing such a great article.

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 "cloud_387": (just to make sure you're a human)

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

Advertisements