npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dv-redtape

v1.0.1-0

Published

Simple setup/teardowns and assertion extensions for the tape testing framework

Downloads

12

Readme

redtape

Simple setup/teardowns and assertion extensions for the tape testing framework

tape is a very simple, minimalist testing framework. Sometimes, however, you'd like there to be a little bit more functionality in the way of setup and teardown functions (or beforeEach and afterEach functions in BDD speak).

build status

Installation

This module is installed via npm, and requires tape as a peerDependency:

$ npm install tape redtape

Example Usage (options syntax)

There are two ways to use redtape, one uses an options object, while the other uses function parameters:

Setup and Teardown (beforeEach, afterEach)

You can pass in an optional beforeEach/afterEach on an options object to the redtape function which will be called before every test:

var redtape = require('redtape'),
    fs = require('fs');

var test = redtape({
  beforeEach: function (cb) {
    fs.writeFile('/tmp/myfile.txt', 'my data', cb);
  },
  afterEach: function (cb) {
    fs.unlink('/tmp/myfile.txt', cb);
  }
});

test('I should be able to read a file', function (t) {
  t.plan(1);
  fs.readFile('/tmp/myfile.txt', { encoding: 'utf8' }, function (err, data) {
    if (err) return t.error(err);
    t.equal(data, 'my data');
  });
});

// You can also pass the test plan assertion in as the second argument
// ie. This is equivalent to the test above
test('I should be able to read a file', 1, function (t) {
  fs.readFile('/tmp/myfile.txt', { encoding: 'utf8' }, function (err, data) {
    if (err) return t.error(err);
    t.equal(data, 'my data');
  });
});

Add custom assertions to the t object

You can extend the test t object by passing through an asserts key on the options object to the redtape function with an object that will be used to extend the built in t object:

var redtape = require('redtape'),
    fs = require('fs');

var test = redtape({
  beforeEach: function (cb) {
    fs.writeFile('/tmp/myfile.txt', 'my data', cb);
  },
  afterEach: function (cb) {
    fs.unlink('/tmp/myfile.txt', cb);
  },
  // add the `like` function to the `t` test object
  asserts: {
    like: function (str, reg, msg) {
      this.ok(reg.test(str), msg);
    }
  }
});

test('I should be able to read a file', function (t) {
  t.plan(1);
  fs.readFile('/tmp/myfile.txt', { encoding: 'utf8' }, function (err, data) {
    if (err) return t.error(err);
    t.like(data, /data/);
  });
});

Pass variables from beforeEach through to tests and afterEach

If you pass through addition data in the beforeEach callback, this data will get passed as additional parameters to the test, as well as to the afterEach function for cleanup:

var redtape = require('redtape'),
    level = require('level'),
    rimraf = require('rimraf');

var test = redtape({
  beforeEach: function (cb) {
    rimraf.sync('/tmp/mytestdb');
    // the level callback returns a handle to the open database for it's first
    // non error argument
    level('/tmp/mytestdb', cb);
  },
  // because the beforeEach function returns a variable, the afterEach function
  // will also get a handle to the data so it can clean up.
  afterEach: function (db, cb) {
    db.close(cb);
  }
});

test('I should be able to read a file', function (t, db) {
  t.plan(3);
  db.put('my key', 'my value', function (err) {
    t.error(err);
    db.get('my key', function (err, data) {
      t.error(err);
      t.equal(data, 'my value');
    });
  });
});

Example Usage (non options syntax)

Setup and Teardown (beforeEach, afterEach)

You can pass in an optional beforeEach/afterEach to the redtape function which will be called before every test:

var redtape = require('redtape'),
    fs = require('fs');

var test = redtape(beforeEach, afterEach);

function beforeEach(cb) {
  fs.writeFile('/tmp/myfile.txt', 'my data', cb);
}

function afterEach(cb) {
  fs.unlink('/tmp/myfile.txt', cb);
}

test('I should be able to read a file', function (t) {
  t.plan(1);
  fs.readFile('/tmp/myfile.txt', { encoding: 'utf8' }, function (err, data) {
    if (err) return t.error(err);
    t.equal(data, 'my data');
  });
});

Add custom assertions to the t object

You can extend the test t object by passing through a third argument to the redtape function with an object that will be used to extend the built in t object:

var redtape = require('redtape'),
    fs = require('fs');

// add the `like` function to the `t` test object
var assertions = {
  like: function (str, reg, msg) {
    this.ok(reg.test(str), msg);
  }
};

var test = redtape(beforeEach, afterEach, assertions);

function beforeEach(cb) {
  fs.writeFile('/tmp/myfile.txt', 'my data', cb);
}

function afterEach(cb) {
  fs.unlink('/tmp/myfile.txt', cb);
}

test('I should be able to read a file', function (t) {
  t.plan(1);
  fs.readFile('/tmp/myfile.txt', { encoding: 'utf8' }, function (err, data) {
    if (err) return t.error(err);
    t.like(data, /data/);
  });
});

Pass variables from beforeEach through to tests and afterEach

If you pass through addition data in the beforeEach callback, this data will get passed as additional parameters to the test, as well as to the afterEach function for cleanup:

var redtape = require('redtape'),
    level = require('level'),
    rimraf = require('rimraf');

var test = redtape(beforeEach, afterEach);

function beforeEach(cb) {
  rimraf.sync('/tmp/mytestdb');
  // the level callback returns a handle to the open database for it's first
  // non error argument
  level('/tmp/mytestdb', cb);
}

// because the beforeEach function returns a variable, the afterEach function
// will also get a handle to the data so it can clean up.
function afterEach(db, cb) {
  db.close(cb);
}

test('I should be able to read a file', function (t, db) {
  t.plan(3);
  db.put('my key', 'my value', function (err) {
    t.error(err);
    db.get('my key', function (err, data) {
      t.error(err);
      t.equal(data, 'my value');
    });
  });
});

Run only a single test

Using the .only variant will only run that particular test case:

var redtape = require('redtape');

var testCount = 0;
var test = redtape();

test('this test will not run', function (t) {
  testCount++;
  t.fail('this test should never run');
  t.end();
});

// only this test will run
test.only('can use a simple after each function with 1 callback', function (t) {
  testCount++;
  t.equal(testCount, 1, 'only test should be run');
  t.end();
});

test('this test will also not run', function (t) {
  testCount++;
  t.fail('this test should also never run');
  t.end();
});

Ignore a single test

Similarly, you can ignore a test by adding the .ignore suffix:

var redtape = require('redtape');

var testCount = 0;
var test = redtape();

test.ignore('this test will not run', function (t) {
  testCount++;
  t.fail('this test should never run');
  t.end();
});

test('this test case will run', function (t) {
  testCount++;
  t.equal(testCount, 1, 'only one test should run');
  t.end();
});