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

tape-harness

v5.1.0

Published

A helper to run integration tests against an application

Downloads

17

Readme

tape-harness

A helper to run integration tests against an application

Motivation

When writing integration tests against a service you generally want to spawn up an instance of the application.

Writing tests against an application can be tedious without a helper to do common setup & teardown before & after every test.

The tape-harness package will setup your test harness / runner / application before each test block and spin them down after each test block.

This functionality is similar to beforeEach or afterEach that might exist in other testing libraries but is instead implemented as a standalone library that's compatible with vanilla tape and tap

Example

Your test file

// test/what.js
'use strict';

var tape = require('tape');
var request = require('request');

var MyTestHarness = require('./lib/test-harness.js');

MyTestHarness.test('a test', {
    port: 8000
}, function t(harness, assert) {
    request({
        url: 'http://localhost:' + harness.port + '/foo'
    }, function onResponse(err, resp, body) {
        assert.ifError(err);

        assert.equal(resp.statusCode, 200);
        assert.equal(resp.body, '/foo');

        assert.end();
    });
});

tape('a test without tape-harness', function t(assert) {
    var harness = new MyTestHarness({
        port: 8000
    });
    harness.bootstrap(function (err) {
        assert.ifError(err);
        request({
            url: 'http://localhost:' + harness.port + '/foo'
        }, function onResponse(err, resp, body) {
            assert.ifError(err);

            assert.equal(resp.statusCode, 200);
            assert.equal(resp.body, '/foo');

            harness.close(function (err) {
                assert.ifError(err);
                assert.end();
            });
        });
    });
});

Your actual test-harness.js

// test-harness.js
'use strict';

var tape = require('tape');
var http = require('http');
var tapeHarness = require('tape-harness');

MyTestHarness.test = tapeHarness(tape, MyTestHarness);

module.exports = MyTestHarness;

function MyTestHarness(opts) {
    if (!(this instanceof MyTestHarness)) {
        return new MyTestHarness(opts);
    }

    var self = this;

    self.assert = opts.assert;
    self.port = opts.port;
    self.server = http.createServer();

    self.server.on('request', onRequest);

    function onRequest(req, res) {
        res.end(req.url);
    }
}

MyTestHarness.prototype.bootstrap = function bootstrap(cb) {
    var self = this;

    self.server.once('listening', cb);
    self.server.listen(self.port);
};

MyTestHarness.prototype.close = function close(cb) {
    var self = this;

    self.server.close(cb);
};

ES6 example

const fetch = require('node-fetch')

const MyTestHarness = require('./lib/test-harness.js');

MyTestHarness.test('a test', {
    port: 8000
}, async function t(harness, assert) {
    const res = await fetch(`http://localhost:${harness.port}/foo`)

    const text = await res.text()
    assert.equal(res.status, 200)
    assert.equal(text, '/foo')
});
const tape = require('tape');
const http = require('http');
const tapeHarness = require('tape-harness');

class TestHarness {
    constructor(opts) {
        this.port = opts.port
        this.server = http.createServer()

        this.server.on('request', (req, res) => {
            res.end(req.url)
        })
    }

    async bootstrap() {
        return new Promise((resolve) => {
            this.server.once('listening', resolve)
            this.server.listen(this.port)
        })
    }

    async close() {
        return new Promise((resolve, reject) => {
            this.server.close((err) => {
                if (err) return reject(err)
                resolve()
            })
        })
    }
}

TestHarness.test = tapeHarness(tape, TestHarness)

Installation

npm install tape-harness

Tests

npm test

Contributors

  • Raynos

MIT Licensed