testtp
v0.3.0
Published
simple library to test node http servers
Maintainers
Readme
Testtp
A library that simplifies testing with node http.Server's or listeners like express.
It exposes node-fetch (bitinn/node-fetch) to get data from the test server.
Install
npm install --save-dev testtpUsage
A sample jest test.
var http = require('http')
var express = require('express')
var fetch = require('node-fetch')
var createTestServer = require('testtp');
describe('tests', () => {
it('test', async () => {
// given
var app = express().get('/', (req, res) => res.send('OK'));
// returns a Testtp instance
var test = await createTestServer(app);
expect(test._server).toEqual(expect.any(http.Server));
expect(test.port).toEqual(expect.any(Number));
expect(test.address).toBe('127.0.0.1');
expect(test.url).toBe(['http://127.0.0.1', test.port].join(':'));
// when
var res = await test.get('/');
var text = await res.text();
// then
expect(res.status).toBe(200);
expect(text).toBe('OK');
// after
await test.close();
});
});
API
testtp(server [, cb]) returns a Promise instance to create Testtp instance
- arg
servera http.Server (or express etc.) instance - arg
cboptional callback withTesttpinstance
Class: Testtp
_serverStores the node http.Server instanceportStores the portaddressStores the address,127.0.0.1urlStores the full url,http://127.0.0.1:1337close([cb])Closes the connection (Callback or Promise)[HTTP_METHOD](path)Requests the test server (node-fetch)
License
MIT
