Skip to content
🧪 Simple promise to wait for server ready inside a mocha specification
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.



Simple promise to wait for server ready inside a mocha specification

License:MIT npm Dependencies Vulnerabilities Build

server-listening is just a little helper utility to reduce the amount of boilerplate code needed to startup servers when running multiple mocha files.

A) Setup

Install package:

$ npm install --save-dev server-listening

Import package:

const serverListening = require('server-listening');

B) Usage

1. Mocha specification file

const server = require('../server');
before(() => serverListening.ready(server));
after(() =>  serverListening.close(server));

Example usage: hello-world/mocha-spec.js

2. setPort() options

The setPort(options) function is just a handy way to set the environment variable for the HTTP port and to flush the server package so the mocha test gets a fresh server.  This function is just for convenience and is not required.

serverListening.setPort({ flush: require.resolve('../server') });
Option Meaning Default
port Port number for server (0 means choose an unused port). 0
name Environment variable name to store port number. 'port'
flush Flush cache to get fresh server (use require.resolve()) null

3. Leveraging promises

The ready(server) and close(server) functions return a promise, enabling chaining of operations.

For example, a port variable could be set after the server is ready using:

let port;
before(() => serverListening.ready(server).then(() => port = server.address().port));

4. jsdom support

The two helper functions jsdomOnLoad() and jsdomCloseWindow() can be used with the JSDOM.fromURL() function to load a web page before the mocha tests run and then close the window when the tests are finished.

// Imports
const assert =          require('assert').strict;
const { JSDOM } =       require('jsdom');
const serverListening = require('server-listening');

// Setup
const url = '';
let dom;
before(() => JSDOM.fromURL(url, { resources: 'usable', runScripts: 'dangerously' })
   .then((jsdom) => dom = jsdom)
after(() => serverListening.jsdomCloseWindow(dom));

describe('The web page', () => {

   it('has the correct URL -> ' + url, () => {
      const actual =   { url: dom.window.location.href };
      const expected = { url: url };
      assert.deepEqual(actual, expected);

   it('has exactly one header, main, and footer', () => {
      const actual =   {
         header: dom.window.$('body >header').length,
         main:   dom.window.$('body >main').length,
         footer: dom.window.$('body >footer').length
      const expected = { header: 1, main: 1, footer: 1 };
      assert.deepEqual(actual, expected);


Above mocha test will output:

  The web page
    ✓ has the correct URL ->
    ✓ has exactly one header, main, and footer

Example of loading a page into jsdom from a local node server:

C) Hello World example

To try out server-listening locally, enter the following terminal commands:

$ git clone
$ cd server-listening/hello-world
$ npm install
$ npm test


You can also run the server locally:

$ npm start

and then use a browser to view the 'Hello, World!' message at: http://localhost:3300

server-listening is open source under the MIT License.

You can’t perform that action at this time.