How can I test an Express.js application with Jest and Supertest without starting the HTTP server?
01:19 12 Aug 2026

I have an Express.js application that I want to test using Jest and Supertest.

Initially, my server.js both created the Express app and started the HTTP server:

const express = require("express");

const app = express();

app.get("/health", (req, res) => {
    res.json({ status: "ok" });
});

app.listen(3000, () => {
    console.log("Server running");
});

module.exports = app;

When Jest imports server.js, the HTTP server also starts.

I changed the structure so that server.js only creates and exports the Express app:

const express = require("express");

const app = express();

app.get("/health", (req, res) => {
    res.json({ status: "ok" });
});

module.exports = app;

And start.js handles the server startup:

const app = require("./server");

app.listen(3000, () => {
    console.log("Server running");
});

This allows Supertest to import the app without starting the server.

Is this a recommended pattern for testing Express applications with Jest and Supertest?

node.js express jestjs supertest