Home

Awesome

jest-process-manager

This project was forked because the package jest-dev-server is no longer maintained.

CI npm

Starts a server before your Jest tests and tears it down after.

Why

jest-playwright-preset or jest-puppeteer works great for running tests in Jest using your preferred end-to-end testing library. It's also useful for starting a local development server during the tests without letting Jest hang. This package extracts just the local development server spawning without any ties to Puppeteer.

Install

npm install --save-dev jest-process-manager

Usage

jest-process-manager exports setup,teardown and getServers functions.

// global-setup.js
const { setup: setupDevServer } = require('jest-process-manager')

module.exports = async function globalSetup() {
  await setupDevServer({
    command: `node config/start.js --port=3000`,
    launchTimeout: 50000,
    port: 3000,
  })
  // Your global setup
}

It is also possible to specify several servers:

// global-setup.js
const { setup: setupDevServer } = require('jest-process-manager')

module.exports = async function globalSetup() {
  await setupDevServer([
    {
      command: 'node server.js',
      port: 4444,
    },
    {
      command: 'node server2.js',
      port: 4445,
    },
  ])
  // Your global setup
}
// global-setup.js
const { setup: setupDevServer, getServers } = require('jest-process-manager')

module.exports = async function globalSetup() {
  await setupDevServer({
    command: `node config/start.js --port=3000`,
    launchTimeout: 50000,
    port: 3000,
  })
  getServers.then(servers => {
    // You can get to the servers and do whatever you want
  })
  // Your global setup
}
// global-teardown.js
const { teardown: teardownDevServer } = require('jest-process-manager')

module.exports = async function globalTeardown() {
  await teardownDevServer()
  // Your global teardown
}

Options

command

Type: string, required.

Command to execute to start the port. Directly passed to spawnd.

module.exports = {
  command: 'npm run start',
}

debug

Type: boolean, default to false.

Log server output, useful if server is crashing at start.

module.exports = {
  command: 'npm run start',
  debug: true,
}

launchTimeout

Type: number, default to 5000.

How many milliseconds to wait for the spawned server to be available before giving up. Defaults to wait-port's default.

module.exports = {
  command: 'npm run start',
  launchTimeout: 30000,
}

Following options are linked to spawnd.

host

Type: string, default to localhost.

Host to wait for activity on before considering the server running. Must be used in conjunction with port.

module.exports = {
  command: 'npm run start --port 3000',
  host: 'customhost.com',
  port: 3000,
}

protocol

Type: (https, http, http-get, https-get, tcp, socket) default to tcp.

To wait for an HTTP or TCP endpoint before considering the server running, include http or tcp as a protocol. Must be used in conjunction with port. This give you ability to define resource prefix for wait-on package.

module.exports = {
  command: 'npm run start --port 3000',
  protocol: 'http',
  port: 3000,
}

port

Type: number, default to 3000.

Port to wait for activity on before considering the server running. If not provided, the server is assumed to immediately be running.

module.exports = {
  command: 'npm run start --port 3000',
  port: 3000,
}

basePath

Type: string

Option for a basePath where server is running.

module.exports = {
  command: 'npm run start',
  basePath: '/myservice',
}

usedPortAction

Type: string (ask, error, ignore, kill) default to ask.

It defines the action to take if port is already used:

module.exports = {
  command: 'npm run start --port 3000',
  port: 3000,
  usedPortAction: 'kill',
}

waitOnScheme

jest-dev-server use the wait-on npm package to wait for resources to become available before calling callback.

Type: object, default to {}.

Note: http(s) specific options, see https://github.com/request/request#readme for specific details

module.exports = {
  command: 'npm run start --port 3000',
  port: 3000,
  usedPortAction: 'kill',
  waitOnScheme: {
    delay: 1000,
  },
}

options

Options which will be passed down to the spawn of the process. For example environment variables:

// global-setup.js
const { setup: setupDevServer, getServers } = require('jest-process-manager')

module.exports = async function globalSetup() {
  await setupDevServer({
    command: `node config/start.js --port=3000`,
    launchTimeout: 50000,
    port: 3000,
    options: {
      env: {
        "FOO": "bar",
      }
    }
  })
  getServers.then(servers => {
    // You can get to the servers and do whatever you want
  })
  // Your global setup
}

Troubleshooting

License

https://github.com/playwright-community/jest-process-manager/blob/master/LICENSE