Testing Patterns
This page covers unit testing procedures, integration testing with a real database, and patterns for mocking context and dependencies.
Unit Testing Procedures
Section titled “Unit Testing Procedures”import { setupTestContext } from '@veloxts/core';import { userProcedures } from '@/procedures/users';
describe('userProcedures', () => { test('createUser creates a user', async () => { const ctx = await setupTestContext();
const result = await userProcedures.procedures.createUser.handler({ input: { name: 'Alice', email: 'alice@example.com' }, ctx, });
expect(result.name).toBe('Alice'); });});Integration Testing
Section titled “Integration Testing”import { veloxApp, rest, type VeloxApp } from '@veloxts/velox';import { userProcedures } from '@/procedures/users';
describe('API Integration', () => { let app: VeloxApp;
beforeAll(async () => { app = await velox({ port: 0 }); // Port 0 = random available port app.routes(rest([userProcedures], { prefix: '/api' })); });
afterAll(() => app.stop());
test('GET /api/users returns users', async () => { const response = await app.server.inject({ method: 'GET', url: '/api/users', });
expect(response.statusCode).toBe(200); expect(JSON.parse(response.body)).toBeInstanceOf(Array); });});Mocking
Section titled “Mocking”import { vi } from 'vitest';
vi.mock('@/database', () => ({ db: { user: { findMany: vi.fn().mockResolvedValue([]), create: vi.fn().mockImplementation((data) => ({ id: '1', ...data.data, })), }, },}));Type Testing
Section titled “Type Testing”import { expectTypeOf } from 'vitest';import type { User } from '@/schemas/user';
test('User type has correct shape', () => { expectTypeOf<User>().toHaveProperty('id'); expectTypeOf<User>().toHaveProperty('email');});Related Content
Section titled “Related Content”- Database Testing - DB patterns
- Procedures - Procedure API