# tape-cluster

> A helper to run tests against a cluster

Latest version **3.2.1** (published 2019-06-27) · 0 weekly downloads

## Install

```sh
npm install tape-cluster
pnpm add tape-cluster
yarn add tape-cluster
bun add tape-cluster
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.2.1 |
| Published | 2019-06-27 |
| First published | 2015-05-24 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 45.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 8 |
| Author | Raynos |
| Maintainers | raynos |

## Links

- npm: https://www.npmjs.com/package/tape-cluster
- Repository: https://github.com/Raynos/tape-cluster
- Issues: https://github.com/Raynos/tape-cluster/issues
- npm.io page: https://npm.io/package/tape-cluster

## Recent versions

- 3.2.1 (latest) — 2019-06-27
- 3.2.0 — 2019-06-20
- 3.1.0 — 2019-04-10
- 3.0.0 — 2019-03-11
- 2.1.2 — 2016-04-09
- 2.1.1 — 2016-04-09
- 2.1.0 — 2015-06-05
- 2.0.1 — 2015-06-01
- 2.0.0 — 2015-05-24

## README

# tape-cluster

<!--
    [![build status][build-png]][build]
    [![Coverage Status][cover-png]][cover]
    [![Davis Dependency status][dep-png]][dep]
-->

<!-- [![NPM][npm-png]][npm] -->

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

```js
// 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`

```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

```js
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')
});
```

```js
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

  [build-png]: https://secure.travis-ci.org/Raynos/tape-cluster.png
  [build]: https://travis-ci.org/Raynos/tape-cluster
  [cover-png]: https://coveralls.io/repos/Raynos/tape-cluster/badge.png
  [cover]: https://coveralls.io/r/Raynos/tape-cluster
  [dep-png]: https://david-dm.org/Raynos/tape-cluster.png
  [dep]: https://david-dm.org/Raynos/tape-cluster
  [npm-png]: https://nodei.co/npm/tape-cluster.png?stars&downloads
  [npm]: https://nodei.co/npm/tape-cluster

---
_Source: https://npm.io/package/tape-cluster · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
