What is jest-dev-server?
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.
What are jest-dev-server's main functionalities?
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,
});
});
Other packages similar to jest-dev-server
start-server-and-test
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.
wait-on
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.
concurrently
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.
jest-dev-server
Shamelessly copy & pasted from jest-puppeteer
's jest-environment-puppeteer.
Starts a server before your Jest tests and tears it down after.
Obeys generally the same settings as as jest-environment-puppeteer
.
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.
Usage
First off, if you're writing tests with Puppeteer, use jest-puppeteer
instead.
jest-dev-server
exports setupServer
and teardownServer
functions.
setupServer
will read in settings from jest-dev-server-config.js
.
module.exports = require("jest-dev-server").setupServer;
module.exports = require("jest-dev-server").teardownServer;
module.exports = {
server: {
command: `node config/start.js --port=3000`,
port: 3000,
launchTimeout: 50000,
},
};