Quick ephemeral URLs for your tests
Switch branches/tags
Clone or download
chentsulin and rauchg Added express, koa example (#25)
Those tips save user much time on figuring out how to use it with popular server frameworks.
Latest commit d3e289c Dec 2, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
test 1.1.0 (#26) Dec 1, 2017
.gitignore Only ignore deps Jan 26, 2017
.travis.yml Use JSON for Travis config Jan 26, 2017
README.md Added express, koa example (#25) Dec 2, 2018
index.js 1.1.0 (#26) Dec 1, 2017
package.json Release 1.1.0 Dec 1, 2017

README.md

test-listen

Build Status XO code style

URLs with ephemeral ports. async/await ready.

Usage

Install it:

npm install --save-dev test-listen

Pass a http.Server to test-listen and it will return an URL in the format http://localhost:{port}.

The second parameter can optionally be a hostname to return in the URL instead of localhost.

Useful for running HTTP server testsuites:

const http = require('http')
const listen = require('test-listen')

const srv = http.createServer((req, res) => res.end('1'))
const srv2 = http.createServer((req, res) => res.end('2'))

test('urls', async t => {
  let url = await listen(srv)
  t.ok(url == 'http://localhost:11401')
  let url = await listen(srv2)
  t.ok(url == 'http://localhost:42333')
})

It also works with Express:

const http = require('http')
const express = require('experss')
const listen = require('test-listen')

const srv = express()

test('urls', async t => {
  let url = await listen(http.createServer(srv))
  t.ok(url == 'http://localhost:11401')
})

Or Koa:

const http = require('http')
const Koa = require('koa')
const listen = require('test-listen')

const srv = new Koa();

test('urls', async t => {
  let url = await listen(http.createServer(srv.callback()))
  t.ok(url == 'http://localhost:11401')
})

Authors