jest-dev-server
Starts a server before your Jest tests and tears it down after.
Why
jest-puppeteer
works great for running tests in Jest using Puppeteer.
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-dev-server
Usage
jest-dev-server
exports setup
,teardown
and getServers
functions.
// global-setup.jsconst setup: setupDevServer = module { await // Your global setup}
It is also possible to specify several servers:
// global-setup.jsconst setup: setupDevServer = module { await // Your global setup}
// global-setup.jsconst setup: setupDevServer getServers = module { await getServers // Your global setup}
// global-teardown.jsconst teardown: teardownDevServer = module { await // Your global teardown}
Options
command
Type: string
, required.
Command to execute to start the port.
Directly passed to spawnd
.
moduleexports = command: 'npm run start'
debug
Type: boolean
, default to false
.
Log server output, useful if server is crashing at start.
moduleexports = 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.
moduleexports = 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
.
moduleexports = command: 'npm run start --port 3000' host: 'customhost.com' port: 3000
protocol
Type: string
, default to null
.
To wait for an HTTP endpoint before considering the server running, include http
as a protocol.
Must be used in conjunction with port
.
moduleexports = command: 'npm run start --port 3000' protocol: 'http' port: 3000
port
Type: number
, default to null
.
Port to wait for activity on before considering the server running. If not provided, the server is assumed to immediately be running.
moduleexports = command: 'npm run start --port 3000' port: 3000
usedPortAction
Type: string
(ask
, error
, ignore
, kill
) default to ask
.
It defines the action to take if port is already used:
ask
: a prompt is shown to decide if you want to kill the process or noterror
: an errow is thrownignore
: your test are executed, we assume that the server is already startedkill
: the process is automatically killed without a prompt
moduleexports = command: 'npm run start --port 3000' port: 3000 usedPortAction: 'kill'
Troubleshooting
- If using
port
makes the terminal to ask for root password although the port is valid and accessible then useusePortAction: 'ignore'
.
License
MIT