/sync-dev-server

Starts a server before your tests and stops on completion. Inspired by jest-puppeteer's subpackage jest-dev-server. Setup and teardown operations are fully synchronous

Primary LanguageTypeScriptMIT LicenseMIT

Sync Dev Server

pipeline   codecov   Maintainability   Snyk Security   GitHub top language

NPM Version   install size   Depfu Dependencies   FOSSA Status   NPM License   GitHub issues

Quality Gate Status   Codacy Badge   DeepSource   codebeat badge   GitHub stars

Downloads Total   Downloads Yearly   Downloads Monthly   Downloads Weekly   Downloads Daily


Starts a server before your tests and stops on completion

Inspired by jest-puppeteer's subpackage jest-dev-server

Setup and teardown operations are fully synchronous

Try with Replit


1. Installation

npm install sync-dev-server

2. Usage

Try with Replit.

startServer(command, options);
stopServer(server, signal);

Full example, specifying all options:

// const { startServer, stopServer } = require('sync-dev-server');
import { startServer, stopServer } from 'sync-dev-server';

const options = {
  // Note: all fields below are optional with set defaults
  host: '127.0.0.1',
  port: 49152,
  timeout: 10000,
  debug: true,
  signal: 'SIGINT',
  usedPortAction: 'ignore', // alternatives are 'kill' and 'error'
  env: {
    secretKey: "bruno's fight club"
  },
}

const server = startServer('npm start', options);

// Send requests to the server or perform other operations
// ...
// ...
// ...

stopServer(server, 'SIGINT');

When using Jest, you can create and stop the server using Setup and Teardown, for example with beforeAll and afterAll. To start and stop the server for all test suites, look into setupFiles, setupFilesAfterEnv, globalSetup and globalTeardown.

2.1. startServer

2.1.1. command

Type: string

The command to start your server with. Below are a few examples:

npm start
npm run dev
yarn start
node src/app.js
ts-node src/server.ts

2.1.2. options

In src/types.ts, the options interface is defined as:

type UsedPortAction = 'error' | 'ignore' | 'kill';

interface Options {
  host?: string;
  port?: number;
  timeout?: number;
  debug?: boolean;
  signal?: string | number;
  usedPortAction?: UsedPortAction;
  env?: Record<string, string>;
}
2.1.2.1. host

Type: string, Default: undefined

Host to wait for activity before considering that the server is running.

If not specified, no address will be passed into node-netstat. This means that all local addresses will be checked, including localhost, 127.0.0.1, 0.0.0.0, etc.

2.1.2.2. port

Type: number, Default: 5000

Port to wait for activity before considering that the server is running.

2.1.2.3. timeout

Type: number, Default: 10000

The timeout for individual tasks (not total time), which are:

  1. The number of milliseconds to wait for the spawned server to be available before giving up
  2. The number of milliseconds to wait for an existing server to be killed if options.usedPortAction === 'kill'
2.1.2.4. debug

Type: boolean, Default: true

Logs the server output to stdout if true, ignore stdout otherwise.

2.1.2.5. signal

Type: number | string

Default: 'SIGTERM' (15)

The inter-process communication signals that will be used to kill any existing server, if options.usedPortAction === 'kill'.

2.1.2.6. usedPortAction

Type: 'error' | 'ignore' | 'kill'. Default: 'error'

A string that defines the action to take if the given port is already in use, whereby

  • error: Throws a generic Error object
  • ignore: Assumes that the server is already started. startServer will return null.
  • kill: the process occupying this port is automatically killed

2.1.2.7. env

Type: Record<string, string>. Default: {}

Any environment variables you want to pass into the spawned server. This will take precedence over existing variables in process.env.

2.2. stopServer

2.2.1. server

Type: ChildProcess | null

The server child process returned from startServer.

stopServer will do nothing if null is passed.

2.2.2. signal

Type: number | string. Default: 'SIGTERM' (15)

The inter-process communication signals that will be used to kill the server.

3. License

Massachusetts Institute of Technology (MIT)
Copyright (c) 2023 Khiet Tam Nguyen

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the “Software”),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

FOSSA Status

4. Limitations

There are currently no known limitations.

5. Caveats

sync-dev-server has been tested on Linux, Windows and MacOS.

It leverages node-netstat, kill-sync, dns-lookup-sync and slync which are all cross-platform.