Does NestJS Work With Cypress?
NestJS and Cypress work together seamlessly for end-to-end testing of NestJS applications.
Quick Facts
How NestJS Works With Cypress
NestJS and Cypress integrate naturally because NestJS is a backend framework that serves HTTP endpoints, and Cypress is an HTTP client that tests web applications. You run your NestJS server (typically on a local port during development), then configure Cypress to point to that base URL. Cypress handles all the browser automation and API testing, while NestJS provides the server being tested. The developer experience is excellent: you write Cypress tests that interact with your NestJS endpoints, verify responses, test authentication flows, and validate business logic end-to-end. For testing strategy, many teams use Cypress for integration and E2E tests that verify the full request-response cycle through NestJS middleware, guards, pipes, and services. You can also use Cypress's API testing capabilities (cy.request) to directly test NestJS endpoints without a UI, which is particularly useful for testing complex backend logic or microservice interactions. NestJS's modular architecture works well with Cypress because you can spin up a test database, seed data, and run the full server in test mode before your Cypress suite runs.
Best Use Cases
Quick Setup
npm install --save-dev cypress && npm install @nestjs/core @nestjs/common// cypress/e2e/auth.cy.ts
describe('NestJS Auth E2E', () => {
const API_URL = 'http://localhost:3000';
before(() => {
cy.request('POST', `${API_URL}/auth/register`, {
email: 'test@example.com',
password: 'Test123!'
}).then(response => {
expect(response.status).to.eq(201);
window.localStorage.setItem('token', response.body.access_token);
});
});
it('should access protected route with token', () => {
cy.request({
method: 'GET',
url: `${API_URL}/users/profile`,
headers: {
Authorization: `Bearer ${window.localStorage.getItem('token')}`
}
}).then(response => {
expect(response.status).to.eq(200);
expect(response.body.email).to.eq('test@example.com');
});
});
it('should reject request without token', () => {
cy.request({
method: 'GET',
url: `${API_URL}/users/profile`,
failOnStatusCode: false
}).then(response => {
expect(response.status).to.eq(401);
});
});
});Known Issues & Gotchas
NestJS server must be running before Cypress tests start; test database state can bleed between tests
Fix: Use npm scripts to start the server before Cypress, implement database cleanup between tests or use transactions that rollback
CORS issues if Cypress and NestJS run on different ports without proper configuration
Fix: Configure NestJS CORS middleware to allow requests from the Cypress port (typically localhost:3000 for app, localhost:3100 for Cypress)
Timing issues when NestJS takes time to start; Cypress may fail before server is ready
Fix: Use cy.request() with retries or wait-on package to poll the health endpoint before running tests
Environment variable conflicts between NestJS test environment and Cypress configuration
Fix: Use separate .env.test files and ensure Cypress loads correct environment before connecting
Alternatives
- •Jest + Supertest: Unit and integration testing directly in Node.js without browser overhead
- •Playwright + NestJS: Similar to Cypress but with cross-browser support and better TypeScript integration
- •NestJS Testing Module + Jest: Built-in testing utilities for isolated unit tests of controllers and services
Resources
Related Compatibility Guides
Explore more compatibility guides