Does NestJS Work With Playwright?
NestJS and Playwright work together seamlessly for end-to-end testing of NestJS applications, with Playwright handling browser automation while NestJS runs your backend server.
Quick Facts
How NestJS Works With Playwright
NestJS and Playwright integrate naturally because they operate at different layers: NestJS runs your backend server while Playwright automates browser interactions against it. You typically start your NestJS application in a test environment, then use Playwright to test the full user journey through your web frontend. This is ideal for testing full-stack features like authentication flows, real-time updates, and complex user interactions that pure backend unit tests can't verify.
The developer experience is straightforward—set up Playwright as a dev dependency, create test files that spin up your NestJS server (or connect to a running instance), and write browser automation tests. Most teams use a shared test configuration that starts NestJS on a specific port before running Playwright tests, then tears it down after. NestJS's modular architecture actually makes this cleaner since you can create dedicated test modules that use in-memory databases and mocked services, keeping your E2E tests fast and deterministic.
Best Use Cases
Quick Setup
npm install --save-dev @playwright/test// tests/e2e/auth.spec.ts
import { test, expect } from '@playwright/test';
test.beforeAll(async () => {
// Server starts on :3000 before tests run
await fetch('http://localhost:3000/health');
});
test('user can login and access dashboard', async ({ page }) => {
await page.goto('http://localhost:3000/login');
await page.fill('input[name="email"]', 'test@example.com');
await page.fill('input[name="password"]', 'password123');
await page.click('button[type="submit"]');
await page.waitForURL('http://localhost:3000/dashboard');
await expect(page.locator('h1')).toContainText('Welcome');
});
// In package.json
// "scripts": { "test:e2e": "nest start --watch & playwright test" }Known Issues & Gotchas
Port conflicts when running NestJS and Playwright tests in parallel CI/CD pipelines
Fix: Use dynamic port allocation or environment variables to assign different ports per test worker. NestJS can read from process.env.PORT
Database state pollution between test runs if using shared test database
Fix: Either use in-memory SQLite for tests, reset database between test suites, or use database transactions that roll back after each test
Timeout issues when NestJS takes time to bootstrap modules and initialize services
Fix: Increase Playwright's timeout and use a health check endpoint that Playwright polls before starting tests
CORS or cookie issues if NestJS backend isn't configured for localhost test environment
Fix: Create a separate NestJS environment configuration for testing that allows localhost origins and sets appropriate cookie policies
Alternatives
- •Cypress + NestJS: Similar functionality with different test API and better time-travel debugging, but slower and larger bundle
- •Puppeteer + NestJS: Lower-level browser control with faster startup, but less API abstraction and cross-browser support than Playwright
- •Selenium + NestJS: Industry standard with multi-language support, but significantly slower and more complex setup than modern alternatives
Resources
Related Compatibility Guides
Explore more compatibility guides