3.2.1 • Published 5 years ago

tape-cluster v3.2.1

Weekly downloads
33
License
-
Repository
github
Last release
5 years ago

tape-cluster

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 or spawn up a cluster of applications.

Writing tests against such a cluster can be tedious without a helper to setup your cluster before and after every test.

The tape-cluster 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 MyTestCluster = require('./lib/test-cluster.js');

MyTestCluster.test('a test', {
    port: 8000
}, function t(cluster, assert) {
    request({
        url: 'http://localhost:' + cluster.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-cluster', function t(assert) {
    var cluster = new MyTestCluster({
        port: 8000
    });
    cluster.bootstrap(function (err) {
        assert.ifError(err);
        request({
            url: 'http://localhost:' + cluster.port + '/foo'
        }, function onResponse(err, resp, body) {
            assert.ifError(err);

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

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

Your actual test-cluster.js

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

var tape = require('tape');
var http = require('http');
var tapeCluster = require('tape-cluster');

MyTestCluster.test = tapeCluster(tape, MyTestCluster);

module.exports = MyTestCluster;

function MyTestCluster(opts) {
    if (!(this instanceof MyTestCluster)) {
        return new MyTestCluster(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);
    }
}

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

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

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

    self.server.close(cb);
};

ES6 example

const fetch = require('node-fetch')

const MyTestCluster = require('./lib/test-cluster.js');

MyTestCluster.test('a test', {
    port: 8000
}, async function t(cluster, assert) {
    const res = await fetch(`http://localhost:${cluster.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 tapeCluster = require('tape-cluster');

class TestCluster {
    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()
            })
        })
    }
}

TestCluster.test = tapeCluster(tape, TestCluster)

Installation

npm install tape-cluster

Tests

npm test

Contributors

  • Raynos

MIT Licensed

3.2.1

5 years ago

3.2.0

5 years ago

3.1.0

5 years ago

3.0.0

5 years ago

2.1.2

8 years ago

2.1.1

8 years ago

2.1.0

9 years ago

2.0.1

9 years ago

2.0.0

9 years ago