https://github.com/janstuemmel/testtp
simple library to test node http servers
https://github.com/janstuemmel/testtp
node nodejs supertest test testing
Last synced: 5 months ago
JSON representation
simple library to test node http servers
- Host: GitHub
- URL: https://github.com/janstuemmel/testtp
- Owner: janstuemmel
- License: mit
- Created: 2017-06-02T16:27:52.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-06-06T10:19:27.000Z (about 9 years ago)
- Last Synced: 2025-06-16T20:47:13.636Z (about 1 year ago)
- Topics: node, nodejs, supertest, test, testing
- Language: JavaScript
- Size: 12.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Testtp
[](https://travis-ci.org/janstuemmel/testtp)
[](https://codeclimate.com/github/janstuemmel/testtp/coverage)
[](https://badge.fury.io/js/testtp)
A library that simplifies testing with node http.Server's or listeners like express.
It exposes `node-fetch` ([bitinn/node-fetch](https://github.com/bitinn/node-fetch)) to get data from the test server.
## Install
```sh
npm install --save-dev testtp
```
## Usage
A sample `jest` test.
```js
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 `server` a http.Server (or express etc.) instance
* arg `cb` optional callback with `Testtp` instance
`Class: Testtp`
* `_server` Stores the node http.Server instance
* `port` Stores the port
* `address` Stores the address, `127.0.0.1`
* `url` Stores the full url, `http://127.0.0.1:1337`
* `close([cb])` Closes the connection (Callback or Promise)
* `[HTTP_METHOD](path)` Requests the test server (`node-fetch`)
## License
MIT