Skip to content

Instantly share code, notes, and snippets.

@morganherlocker
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save morganherlocker/1eaa0cea6606569e072c to your computer and use it in GitHub Desktop.

Select an option

Save morganherlocker/1eaa0cea6606569e072c to your computer and use it in GitHub Desktop.

tape 101

tape is a tiny test runner and assertion library. It outputs a text format called TAP (Test Anything Protocol). TAP is cool because it is a standardized text format that can be easily parsed in just about any programming language. Unlike many other javascript test frameworks, tape is a simple npm module, so it can be run anywhere without weird hacks.

###Installation

npm install tape --save-dev

^ The save-dev flag automatically saves tape as a development dependency. This allows other developers to run your tests when they clone your project.

###Testing

//require tape
var test = require('tape');
//require our script
var add = require('./add.js');

// name your test and give test a callback to run
test('1+1', function(t){
  // t is our assertion object. It decides whether the tests passed or not.
  
  // setup your code and run the script
  var num1 = 5;
  var num2 = 7;
  var sum = add(num1, num2);
  
  // make some assertions about what the result should be. Carefully 
  // chosen assertions is the most important part of good testing
  t.ok(sum); 
  t.equal(sum, 12);
  
  // call t.end() to assert that the test is complete. this is required.
  t.end();
});

###Deep Assertions

t.equal is great for comparing simple numbers and strings, but it will not work with complex objects and arrays. We need to use t.deepEqual, which makes sure that all values match. This is great for testing things like a long list of geographic coordinates, or other complex test fixtures.

t.deepEqual(coordinates, [
          [
            [
              91.7578125,
              62.59334083012024
            ],
            [
              80.15625,
              58.44773280389084
            ],
            [
              99.84374999999999,
              46.31658418182218
            ],
            [
              109.3359375,
              52.696361078274485
            ],
            [
              91.7578125,
              62.59334083012024
            ]
          ]
        ]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment