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

bandage-ts

v0.5.0

Published

Simple generator based test library

Downloads

6

Readme

bandage

npm status build status coverage status dependency status

Generator best test harness and test runner.

Usage

Use like if tape supported generator functions. Just always pass a generator function to test and stop calling end/done. The test is always done when the function is done.

var test = require('bandage');

test('yielding test', function *(t) {
  t.equal(5, 2+3, 'addition works');
  var v = yield Promise.resolve(true);
  t.ok(v, 'promise resolved correctly');
});

test('plain test', function *(t) {
  t.equal(6, 2*3, 'multiplication works');
});

Which you can run with the bundled (vowel-free) bndg executable:

$ bndg test.js
TAP version 13
# yielding test
ok 1 addition works
ok 2 promise resolved correctly
# plain test
ok 3 multiplication works

1..3
# tests 3
# pass  3
# fail  0

Error handling

By default, bandage catches errors and reports it as a test failure with a stack trace if you do not catch it, or use t.throws. Here's an example:

var test = require('bandage');

var rejecting = function () {
  return new Promise(function (resolve, reject) {
    setTimeout(function () {
      reject(new Error('async reject'));
    });
  });
};

test('error is caught', function *T(t) {
  yield rejecting();
  t.fail('we do not reach this');
});

Which will output:

TAP version 13
# error is caught
not ok 1 Error: async reject
  ---
    operator: error
    expected: undefined
    actual: [Error: async reject]
    at: null._onTimeout (/path/to/test.js:6:14)
    stack:
      Error: async reject
        at null._onTimeout (/path/to/test.js:6:14)
        at Timer.listOnTimeout (timers.js:92:15)
  ...

1..1
# tests 1
# pass  0
# fail  1

If you would like to test that errors are correctly passed through, just catch them yourself:

var test = require('bandage');

var rejecting = function () {
  return new Promise(function (resolve, reject) {
    setTimeout(function () {
      reject(new Error('async reject'));
    });
  });
};

test('error is caught', function *T(t) {
  t.plan(2);
  try {
    yield rejecting();
  }
  catch (err) {
    t.equal(err.message, 'async reject', 'caught async reject');
  }
  t.pass('we do reach this');
});

Which will output:

TAP version 13
# error is caught
ok 1 caught async reject
ok 2 we do reach this

1..2
# tests 2
# pass  2
# fail  0

If something unexpectedly throws in some callback stack that isn't matched by anything, that will still provide a TAP breaking stack trace. That is still a failed test though - there's only so much we can do at this point.

Subtests

Subtests work with bandage. The only difference is you need to yield it:

var test = require('bandage');

var slow = function () {
  return new Promise(function (res) {
    setTimeout(function () {
      res(true);
    }, 50);
  });
};

test('nested tests', function *(t) {
  var a = yield slow();
  t.ok(a, 'waited for slow');

  yield t.test('subtest', function *(st) {
    var b = yield slow();
    st.ok(b, 'waited for slow in subtest');
  });

  t.pass('pass after waiting for subtest')
});

Which outputs

TAP version 13
# nested tests
ok 1 waited for slow
ok 2 subtest
ok 3 waited for slow in subtest
ok 4 pass after waiting for subtest

1..4
# tests 4
# pass  4
# fail  0

Setup and Teardown

Because tests are executed sequentially in the order of the file, you can create setup tests at the top of your file, and teardown tests at the bottom.