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
muslnode 18–226 runs
build_error
glibcnode 18–226 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');
});
});
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.
fixAdd `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).
fixConfigure `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.
fixDouble-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.
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.