We use Jest or Vitest to check if our Express app works correctly. They help us find mistakes early by running tests automatically.
Jest or Vitest setup for Express
npm install --save-dev jest supertest // or for Vitest: npm install --save-dev vitest supertest
Jest and Vitest are testing tools that run your tests and show results.
Supertest helps to test Express routes by simulating HTTP requests.
import request from 'supertest'; import app from './app'; test('GET / returns 200', async () => { const response = await request(app).get('/'); expect(response.status).toBe(200); });
import { describe, it, expect } from 'vitest'; import request from 'supertest'; import app from './app'; describe('GET /', () => { it('should return 200 OK', async () => { const res = await request(app).get('/'); expect(res.status).toBe(200); }); });
This is a simple Express app with one route. The test checks if the route returns status 200 and the text 'Hello World'.
// app.js import express from 'express'; const app = express(); app.get('/', (req, res) => { res.status(200).send('Hello World'); }); export default app; // app.test.js (for Jest) import request from 'supertest'; import app from './app'; test('GET / returns Hello World', async () => { const response = await request(app).get('/'); expect(response.status).toBe(200); expect(response.text).toBe('Hello World'); });
Always export your Express app without listening to a port for easier testing.
Use Supertest to simulate HTTP requests without starting the server.
Run tests with npm test after setting up scripts in package.json.
Jest and Vitest help test Express apps by running code that simulates requests.
Supertest works with both to make HTTP calls inside tests.
Keep your app export separate from server start for easier testing.
Practice
Solution
Step 1: Understand app export and server start roles
The Express app handles routes and middleware, while server start listens on a port.Step 2: Why separate for testing?
Separating lets tests import the app to simulate requests without starting a real server, avoiding conflicts and port issues.Final Answer:
It allows tests to import the app without starting the server. -> Option AQuick Check:
Separate app export = test-friendly import [OK]
- Thinking separation improves production speed
- Confusing server restart with test setup
- Assuming separation stops error logs
app?Solution
Step 1: Check import syntax for Supertest in ES modules
Supertest is imported as default export, so useimport request from 'supertest';.Step 2: Create request instance with app
Callrequest(app)to create a test client for the Express app.Final Answer:
import request from 'supertest'; const req = request(app); -> Option AQuick Check:
Supertest import + request(app) = correct [OK]
- Using require with ES module import style
- Calling supertest without import
- Incorrect named import from 'supertest'
import request from 'supertest';
import app from './app';
test('GET /hello returns 200 and message', async () => {
const response = await request(app).get('/hello');
expect(response.status).toBe(200);
expect(response.body.message).toBe('Hello World');
});Assuming the Express app has a route /hello that sends { message: 'Hello World' }, what happens?
Solution
Step 1: Confirm route and response
The app has/helloroute returning JSON with{ message: 'Hello World' }and status 200.Step 2: Analyze test expectations
The test checks status 200 and message string, which matches the route response.Final Answer:
Test passes because status is 200 and message matches. -> Option DQuick Check:
Matching route and response = test pass [OK]
- Assuming response.body is undefined without JSON middleware
- Expecting 404 if route exists
- Thinking import errors happen without evidence
import request from 'supertest';
import app from './app';
test('POST /data returns 201', async () => {
const res = await request(app).post('/data').send({ name: 'Test' });
expect(res.status).toBe(201);
});But the test fails with TypeError: request(app).post is not a function. What is the likely cause?
Solution
Step 1: Understand the error meaning
post is not a functionmeansrequest(app)is not returning a Supertest object.Step 2: Check import and installation
This usually happens if Supertest is not imported correctly or missing from dependencies.Final Answer:
You imported Supertest incorrectly or did not install it. -> Option BQuick Check:
post() missing = import or install issue [OK]
- Blaming missing route for method error
- Assuming app export causes this error
- Thinking Vitest can't handle async
Solution
Step 1: Understand server start impact on tests
Starting the server for each test slows tests and can cause port conflicts.Step 2: Use app export with Supertest
Exporting the app lets Supertest simulate requests without real server, making tests faster and isolated.Final Answer:
Export the Express app fromapp.jsand import it in tests; use Supertest to call routes directly. -> Option CQuick Check:
App export + Supertest = efficient test setup [OK]
- Starting server for every test causing slow tests
- Using fetch instead of Supertest in tests
- Restarting server unnecessarily before each test
Ready to prove it?
Pass the test and this pattern moves unknown
