![Introducing Dashboard Analytics](https://cdn.sanity.io/images/cgdhsj6q/production/af327c5bd94708db501516a75da6870aef894c64-2286x1522.png?w=400&fit=max&auto=format)
Product
Introducing Dashboard Analytics
We're introducing a new Analytics feature in the Socket dashboard so you can view changes in your organization's and repositories' alerts over time.
jest-dev-server
Advanced tools
The jest-dev-server package is a utility for running a development server while running Jest tests. It is particularly useful for end-to-end (E2E) testing where you need a server to be up and running before executing your tests.
Starting a development server
This feature allows you to start a development server before running your tests. The `setupDevServer` function takes an object with a `command` to start the server and a `port` to listen on.
const { setup: setupDevServer } = require('jest-dev-server');
beforeAll(async () => {
await setupDevServer({
command: 'npm run start',
port: 3000,
});
});
Stopping the development server
This feature allows you to stop the development server after your tests have completed. The `teardownDevServer` function ensures that the server is properly shut down.
const { teardown: teardownDevServer } = require('jest-dev-server');
afterAll(async () => {
await teardownDevServer();
});
Custom server options
This feature allows you to customize the server options such as `launchTimeout` and `debug` mode. This is useful for fine-tuning the server startup process to match your specific needs.
const { setup: setupDevServer } = require('jest-dev-server');
beforeAll(async () => {
await setupDevServer({
command: 'npm run start',
port: 3000,
launchTimeout: 50000,
debug: true,
});
});
The start-server-and-test package is another utility for starting a server and running tests. It is more versatile as it can be used with any test runner, not just Jest. It also provides more flexibility in terms of waiting for the server to be ready before running tests.
The wait-on package is a utility that waits for files, ports, sockets, and http(s) resources to become available. It can be used in conjunction with other tools to ensure that a server is up and running before executing tests. It is more general-purpose compared to jest-dev-server.
The concurrently package allows you to run multiple commands concurrently. It can be used to start a server and run tests in parallel. While it does not provide the same level of integration with Jest as jest-dev-server, it is a powerful tool for managing multiple processes.
Starts a server before your Jest tests and tears it down after.
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.
npm install --save-dev jest-dev-server
jest-dev-server
exports setup
,teardown
and getServers
functions.
// global-setup.js
const { setup: setupDevServer } = require('jest-dev-server')
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-dev-server')
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-dev-server')
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-dev-server')
module.exports = async function globalTeardown() {
await teardownDevServer()
// Your global teardown
}
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,
}
path
Type: string
, default to null
.
Path to resource to wait for activity on before considering the server running.
Must be used in conjunction with host
and port
.
module.exports = {
command: 'npm run start --port 3000',
host: 'customhost.com',
port: 3000,
path: 'thing',
}
protocol
Type: string
, (https
, http
, 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
.
module.exports = {
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.
module.exports = {
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 promptmodule.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 {}
.
delay
: optional initial delay in ms, default 0interval
: optional poll resource interval in ms, default 250mslog
: optional flag which outputs to stdout, remaining resources waited on and when complete or erroredreverse
: optional flag to reverse operation so checks are for resources being NOT available, default falsetimeout
: optional timeout in ms, default Infinity. Aborts with errortcpTimeout
: optional tcp timeout in ms, default 300msverbose
: optional flag which outputs debug output, default falsewindow
: optional stabilization time in ms, default 750ms. Waits this amount of time for file sizes to stabilize or other resource availability to remain unchangedNote: 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,
},
}
port
makes the terminal to ask for root password although the port is valid and accessible then use usePortAction: 'ignore'
.MIT
FAQs
Starts a server before your Jest tests and tears it down after.
The npm package jest-dev-server receives a total of 88,144 weekly downloads. As such, jest-dev-server popularity was classified as popular.
We found that jest-dev-server demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
We're introducing a new Analytics feature in the Socket dashboard so you can view changes in your organization's and repositories' alerts over time.
Security News
A new OpenSSF report uncovers critical gaps in secure software training, with 75% of new developers unfamiliar with secure practices, highlighting urgent educational needs.
Security News
The 2023 Python Developers Survey reveals key trends in packaging, web frameworks, and developer demographics, highlighting a shift toward innovative tools as the Python community diversifies and grows among less experienced developers.