| name | nx-testing |
| description | Testing workflows with Vitest in Nx monorepo. Use when writing tests, running test suites, or debugging test failures. |
Nx Testing Workflows
Test Stack
- Runner: Vitest 4.x
- HTTP Testing: Supertest
- Containers: Testcontainers
- Coverage: Built-in Vitest
Running Tests
pnpm nx test auth-server
pnpm nx test auth-server --watch
pnpm nx test auth-server --coverage
pnpm nx run-many -t test --all
pnpm nx affected -t test
pnpm nx run-many -t test --all --parallel=3
pnpm test:ui
Test File Patterns
Co-locate tests with source:
libs/server/jwt/src/
├── jwt.service.ts
├── jwt.service.spec.ts
└── index.ts
Unit Test Example
import { describe, it, expect, beforeEach } from 'vitest';
import { createService } from './service';
describe('Service', () => {
let service: ReturnType<typeof createService>;
beforeEach(() => {
service = createService();
});
it('should do something', () => {
expect(service.method()).toBe(expected);
});
});
Integration Test with Fastify
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import { buildTestApp } from '@qauth-labs/shared-testing';
import type { FastifyInstance } from 'fastify';
describe('Route', () => {
let app: FastifyInstance;
beforeAll(async () => {
app = await buildTestApp();
});
afterAll(async () => {
await app.close();
});
it('should return 200', async () => {
const response = await app.inject({
method: 'GET',
url: '/health',
});
expect(response.statusCode).toBe(200);
});
});
Best Practices
- Test business logic in isolation
- Use
@qauth-labs/shared-testing utilities
- Use Testcontainers for real PostgreSQL
- Mock external services
- Name tests descriptively