Performance: Jest or Vitest setup for Express
This affects the development workflow speed and test execution time, impacting how quickly developers get feedback.
import express from 'express'; import request from 'supertest'; import { describe, test, expect } from 'vitest'; // Import only the route handler or use a lightweight app instance const app = express(); app.get('/', (req, res) => res.send('OK')); describe('Express route test', () => { test('GET / endpoint', async () => { const response = await request(app).get('/'); expect(response.status).toBe(200); }); });
import express from 'express'; import request from 'supertest'; // Full app import with all middleware and database connections import app from './app'; describe('Express app tests', () => { test('GET / endpoint', async () => { const response = await request(app).get('/'); expect(response.status).toBe(200); }); });
| Pattern | Module Loading Time | Memory Usage | Test Startup Delay | Verdict |
|---|---|---|---|---|
| Full app import with all middleware | High (300-500ms) | High | High (500ms+) | [X] Bad |
| Minimal Express instance or isolated routes | Low (under 100ms) | Low | Low (under 100ms) | [OK] Good |
app?import request from 'supertest';.request(app) to create a test client for the Express app.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?
/hello route returning JSON with { message: 'Hello World' } and status 200.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?
post is not a function means request(app) is not returning a Supertest object.app.js and import it in tests; use Supertest to call routes directly. -> Option CPass the test and this pattern moves unknown