Let’s say we have an express app that doesn't have any DB connections
npm init -y
npx tsc --init
{
"compilerOptions": {
"target": "ESNext",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"isolatedModules": true,
"outDir": "dist"
}
}
npm install -D ts-jest @jest/globals @types/express supertest @types/supertest
npm install express

{
"name": "sample_node_app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "module",
"devDependencies": {
"@jest/globals": "^30.2.0",
"@types/express": "^5.0.5",
"@types/supertest": "^6.0.3",
"supertest": "^7.1.4",
"ts-jest": "^29.4.5"
},
"dependencies": {
"express": "^5.1.0"
}
}
npx ts-jest config:init
update jest.config.js
export default {
preset: "ts-jest/presets/default-esm",
testEnvironment: "node",
extensionsToTreatAsEsm: [".ts"],
moduleNameMapper: {
"^(\\\\.{1,2}/.*)\\\\.js$": "$1"
},
transform: {
"^.+\\\\.ts$": [
"ts-jest",
{
useESM: true
}
]
}
};
src/index.tsimport express from "express";
export const app = express();
app.use(express.json());
app.post("/sum", (req, res) => {
const a = req.body.a;
const b = req.body.b;
const answer = a + b;
res.json({
answer
})
});
tests/sum.test.ts