Registry / testing / jest-dev-server

jest-dev-server

JSON →
library11.0.0jsnpmunverified

jest-dev-server is a utility package designed to manage the lifecycle of a development server during Jest test runs. It enables users to automatically start a server process before their test suites execute and reliably tear it down afterward, ensuring a clean and consistent testing environment. While often used within the `jest-puppeteer` ecosystem for end-to-end testing, this package operates independently, capable of managing any server process. The current stable version is 11.0.0, with minor and patch updates released frequently and major versions typically aligning with Node.js LTS updates or significant Jest/Puppeteer compatibility requirements. Key differentiators include its focus on robust server lifecycle management, flexible configuration options for server commands, ports, and wait conditions, and its ability to integrate seamlessly with Jest's `globalSetup` and `globalTeardown` hooks for both JavaScript and TypeScript projects.

npm install jest-dev-server
INSTALL
IMPORT
SIG · JEST-DEV-SERVER
J
jest-dev-server
testingjavascriptv11.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

setup
import { setup } from 'jest-dev-server';
const { setup } = require('jest-dev-server'); // CommonJS syntax, still valid but ESM preferred for modern Jest configs.
The `setup` function is primarily used in Jest's `globalSetup` configuration to start your development server before tests. It is an async function.
teardown
import { teardown } from 'jest-dev-server';
const { teardown } = require('jest-dev-server'); // CommonJS syntax, still valid but ESM preferred for modern Jest configs.
The `teardown` function is primarily used in Jest's `globalTeardown` configuration to stop your development server after tests. It is an async function.
Config
import type { Config } from 'jest-dev-server';
TypeScript type definition for the configuration object passed to `setup`. Useful for type-checking your `jest-dev-server` options.

Demonstrates how to configure Jest with `jest-dev-server` to start and stop a basic HTTP server using TypeScript for the configuration and global hooks, and then run an example test against it.

// jest.config.ts import type { Config } from 'jest'; const config: Config = { globalSetup: '<rootDir>/test/globalSetup.ts', globalTeardown: '<rootDir>/test/globalTeardown.ts', testEnvironment: 'node', testMatch: ['<rootDir>/test/**/*.test.ts'], roots: ['<rootDir>/src', '<rootDir>/test'], transform: { '^.+\.tsx?$': 'ts-jest', }, // Required for ESM imports in globalSetup/teardown when using ts-node // See warnings/problems for more details // globalSetup and globalTeardown are run outside the Jest environment // and might need ts-node/register to process TypeScript files. }; export default config; // test/globalSetup.ts import { setup as setupDevServer } from 'jest-dev-server'; // Workaround for TypeScript globalSetup/teardown if 'ts-node/register' isn't configured globally // (e.g., if Jest is not running with --require ts-node/register) require('ts-node/register'); const globalSetup = async (): Promise<void> => { await setupDevServer({ command: `node ${__dirname}/server.js --port=8080`, port: 8080, launchTimeout: 30000, debug: true, usedPortAction: 'kill', }); console.log('Development server started on port 8080.'); }; export default globalSetup; // test/globalTeardown.ts import { teardown as teardownDevServer } from 'jest-dev-server'; // Workaround for TypeScript globalSetup/teardown if 'ts-node/register' isn't configured globally require('ts-node/register'); const globalTeardown = async (): Promise<void> => { await teardownDevServer(); console.log('Development server stopped.'); }; export default globalTeardown; // test/server.js (simple HTTP server for demonstration) const http = require('http'); const port = process.env.PORT || 8080; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello from Jest Dev Server!\n'); }); server.listen(port, () => { // console.log(`Server running at http://localhost:${port}/`); // Commented to avoid noise during tests }); process.on('SIGTERM', () => { // console.log('Server shutting down...'); // Commented to avoid noise during tests server.close(() => { // console.log('Server closed.'); // Commented to avoid noise during tests process.exit(0); }); }); // test/example.test.ts import axios from 'axios'; describe('HTTP Server', () => { it('should respond with "Hello from Jest Dev Server!"', async () => { const response = await axios.get('http://localhost:8080'); expect(response.status).toBe(200); expect(response.data).toBe('Hello from Jest Dev Server!\n'); }); });
Debug
Known issues
breakingNode.js v16 support has been dropped with the release of v10.0.0 and v11.0.0. Projects using Node.js v16 or older will need to upgrade their Node.js environment to at least v18.
fix
Upgrade your Node.js version to 18 or higher (e.g., `nvm install 18 && nvm use 18`).
affects: >=10.0.0
breakingIn an earlier major version, the default `host` option for `jest-dev-server` changed from 'localhost' to `undefined`. This might affect how the server binds to network interfaces if not explicitly configured.
fix
Explicitly set the `host` option in your `jest-dev-server` configuration to 'localhost' or '0.0.0.0' if a specific binding is required.
affects: <9.0.0
gotchaWhen using `globalSetup` and `globalTeardown` with TypeScript files (e.g., `globalSetup.ts`), Jest might fail to load these files if `ts-node/register` is not properly configured or required within the setup/teardown scripts themselves, leading to `SyntaxError: Cannot use import statement outside a module` or `TypeError`.
fix
Ensure `ts-node` is installed and either configure Jest to pre-load `ts-node/register` (e.g., `jest --require ts-node/register`) or add `require('ts-node/register');` at the top of your `globalSetup.ts` and `globalTeardown.ts` files.
affects: >=1.0.0
gotchaThe `usedPortAction` option dictates how `jest-dev-server` behaves if the specified port is already in use. The default action is 'ask', which can cause tests to hang in CI/CD environments. Setting it to 'kill' can force termination of existing processes, but 'error' or 'ignore' might be more appropriate depending on your setup.
fix
Always explicitly define `usedPortAction` in your `jest-dev-server` configuration, especially in CI/CD. For example, `usedPortAction: 'kill'` or `usedPortAction: 'error'` to prevent hangs or ensure clean state.
affects: >=1.0.0
Errors
Common errors & fixes
SyntaxError: Cannot use import statement outside a module
Jest's `globalSetup` or `globalTeardown` files, written in TypeScript, are being loaded without `ts-node/register` or a similar loader to transpile them at runtime.
fix
Add `require('ts-node/register');` to the very top of your `globalSetup.ts` and `globalTeardown.ts` files, or ensure your Jest configuration includes `--require ts-node/register` or `module.exports = require('ts-node').register({ transpileOnly: true });` in your setup files.
Error: listen EADDRINUSE: address already in use :::<port>
The port specified for your development server is already occupied by another process, and `jest-dev-server` is configured to throw an error or implicitly 'ask' (which hangs).
fix
Configure `usedPortAction: 'kill'` in your `jest-dev-server` options to automatically terminate processes occupying the port, or ensure no other applications are running on the designated port. Alternatively, set `usedPortAction: 'error'` to fail fast or `usedPortAction: 'ignore'` to proceed if the server is expected to be already running.
TypeError: (0 , jest_dev_server_1.setup) is not a function
This error often occurs when Jest tries to load an ESM `globalSetup` or `globalTeardown` file using CommonJS `require` semantics, or vice-versa, due to incorrect module resolution or transpilation in a mixed environment.
fix
Double-check your `jest.config.ts` or `jest.config.js` to ensure module type (`type: 'module'` in `package.json`) and loader configurations are consistent with how your `globalSetup` and `globalTeardown` files are written (ESM `import` vs. CJS `require`). Ensure `ts-node` is correctly configured for TypeScript files.
Upgrade
Version history
11.0.0latest on npm
Audit
Dependencies
jestrequiredIntegrates directly with Jest's global setup and teardown hooks. This package is typically a peer dependency of a testing setup using Jest.
Agent activity
4 hits · last 30 days
node
4
Resources