Quickie: get host of SuperTest instance

Have you ever needed to get the port and hostname of a SuperTest instance? For example, this can be useful if you’re trying to also use nock.disableNetConnect() in your tests to prevent external requests.

Normally, when creating SuperTest by just passing it the express app, it uses an ephemeral port that you do not know in advance. This means that if you call something like nock.disableNetConnect() and use SuperTest, it will throw an error that’s something like this:

NetConnectNotAllowedError: Nock: Not allow net connect for ""

As a workaround, you can create the http server yourself and pass it to SuperTest, but I’ve recently found a decent way to get the full host (hostname and port) from a SuperTest instance without doing that.

In short, you can do a fake “request” which then allows you to retrieve the actual host.

const request = superTest(app); // your express app
const superTestHostname = request.get('').url; //

Here’s how it could look when used with nock:

const nock = require('nock');
const url = require('url');
const express = require('express');
const superTest = require('supertest');

const app = express();
// ... your app

const request = superTest(app);

function disableOutsideConnections() {
  const superTestHostname = request.get('').url;
  const { host: superTestHost } = url.parse(superTestHostname);


Here’s a working example on RunKit so you can play around.