npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

server-listening

v1.2.4

Published

Simple promise to wait for server ready inside a mocha specification

Downloads

29

Readme

server-listening

Simple promise to wait for server ready or DOM ready inside a mocha specification

License:MIT npm Build

server-listening is a lightweight helper utility to reduce the amount of boilerplate code needed to startup servers when running mocha specifications.

A) Setup

Install package:

$ npm install --save-dev server-listening

Import package:

import { serverListening } from 'server-listening';

B) Usage

Three primary tools:

  • serverListening.ready(server) Waits for your node server application to start up
  • serverListening.startWebServer(options) Starts and waits for static web server (express), see: start-web-server.spec.js
  • serverListening.loadWebPage(url, options) Uses JSDOM to load and wait for a web page, see: load-web-page.spec.js

(for similar functionality using Puppeteer instead, see the puppeteer-browser-ready project).

1. Mocha specification file

import { server } from '../server.js';
before(() => serverListening.ready(server));
after(() =>  serverListening.close(server));

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

NOTE: Mocha's default timeout is 2,000 milliseconds which often is not enough time for a node server to shutdown.  Use the --timeout flag to help avoid this problem:

"scripts": {
   "test": "mocha *.spec.js --timeout 7000"
}

2. setPort() options

The setPort(options) function is just a handy way to set the environment variable for the HTTP port.  This function is for convenience and is not required.

serverListening.setPort({ port: 9000 });

| Option | Meaning | Default | | --------- | --------------------------------------------------------- | -------- | | port | Port number for server (0 means choose an unused port). | 0 | | name | Environment variable name to store port number. | 'port' |

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. Example for serverListening.loadWebPage(url)

// Mocha Specification Suite

// Imports
import { assertDeepStrictEqual } from 'assert-deep-strict-equal';
import { serverListening } from 'server-listening';

// Setup
const url = 'https://pretty-print-json.js.org/';
let web;  //fields: url, dom, window, document, title, html, verbose
const loadWebPage =  () => serverListening.loadWebPage(url).then(webInst => web = webInst);
const closeWebPage = () => serverListening.closeWebPage(web);

////////////////////////////////////////////////////////////////////////////////
describe('The web page', () => {
   const getTags = (elems) => [...elems].map(elem => elem.nodeName.toLowerCase());
   before(loadWebPage);
   after(closeWebPage);

   it('has the correct URL', () => {
      const actual =   { url: web.window.location.href };
      const expected = { url: url };
      assertDeepStrictEqual(actual, expected);
      });

   it('body has exactly one header, main, and footer', () => {
      const actual =   getTags(web.document.querySelectorAll('body >*'));
      const expected = ['header', 'main', 'footer'];
      assertDeepStrictEqual(actual, expected);
      });

   });

////////////////////////////////////////////////////////////////////////////////
describe('The document content', () => {
   before(loadWebPage);
   after(closeWebPage);

   it('has a 🚀 traveling to 🪐!', () => {
      const html =     web.document.body.outerHTML;
      const actual =   { '🚀': !!html.match(/🚀/g), '🪐': !!html.match(/🪐/g) };
      const expected = { '🚀': true,                '🪐': true };
      assertDeepStrictEqual(actual, expected);
      });

   });

Above mocha test will output:

  The web page
    ✓ has the correct URL -> https://pretty-print-json.js.org/
    ✓ body has exactly one header, main, and footer

  The document content
    ✓ has a 🚀 traveling to 🪐!

Example of loading a web page into jsdom from a local node server: https://github.com/dna-engine/data-dashboard/blob/main/spec/spec.js

5. TypeScript declarations

See the TypeScript declarations at the top of the server-listening.ts file.

The declarations provide type information about the API, such as the options for calling serverListening.setPort():

type ServerListeningOptions = {
   port?:  number,  //0 = find unused port
   name?:  string,  //environment variable to pass port number
   };

C) Hello World Example

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

$ git clone https://github.com/center-key/server-listening.git
$ 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.