Giter VIP home page Giter VIP logo

redtape's Introduction

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();
});

redtape's People

Contributors

eugeneware avatar

Stargazers

dmitry avatar Linus Miller avatar Anton Petrov avatar Makeev Vitaliy avatar Maciej Adamczak avatar Andrew Waterman avatar Cesar Andreu avatar  avatar  avatar Mikey avatar Adam Lynch avatar Michael Rhodes avatar Tim Kevin Oxley avatar Ilya Radchenko avatar Mark Bolusmjak avatar

Watchers

Adam Lynch avatar  avatar

redtape's Issues

afterEach doesn't work (and isn't actually tested)

first of all, thanks for the lib :)

in test/index.js, the first test doesn't actually test afterEach

breaking test:

var afterEachCalled
var redtest = redtape(null, function afterEach () {
  afterEachCalled = true
});

redtest('dummy test', function (t) {
  t.plan(1)
  t.equal(0, 0)
})

setTimeout(function () {
  assert.equal(afterEachCalled, true)
}, 200)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.