E2E tests test toàn bộ HTTP flow từ request đến response mà không cần real external services.
typescript
// test/users.e2e-spec.ts
describe('Users (e2e)', () => {
let app: INestApplication;
beforeAll(async () => {
const moduleFixture = await Test.createTestingModule({
imports: [AppModule],
})
.overrideProvider(UsersService) // Override real service
.useValue(mockUsersService)
.compile();
app = moduleFixture.createNestApplication();
app.useGlobalPipes(new ValidationPipe({ whitelist: true }));
app.useGlobalFilters(new HttpExceptionFilter());
await app.init();
});
afterAll(async () => {
await app.close();
});
it('GET /users — returns users list', () => {
return request(app.getHttpServer())
.get('/users')
.set('Authorization', `Bearer ${testToken}`)
.expect(200)
.expect(res => {
expect(res.body.data).toBeInstanceOf(Array);
});
});
});Best practices: dùng test database thực (SQLite in-memory hoặc test Postgres), seed data trong beforeAll, cleanup trong afterAll.
Chạy E2E riêng biệt với jest --testPathPattern=e2e.