Skip to content

Testing Patterns

Testing patterns for VeloxTS applications.

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');
});
});
import { veloxApp } from '@veloxts/velox';
describe('API Integration', () => {
let app: ReturnType<typeof velox>;
beforeAll(async () => {
app = veloxApp();
app.procedures(userProcedures);
await app.ready();
});
afterAll(() => app.close());
test('GET /api/users returns users', async () => {
const response = await app.inject({
method: 'GET',
url: '/api/users',
});
expect(response.statusCode).toBe(200);
expect(JSON.parse(response.body)).toBeInstanceOf(Array);
});
});
import { vi } from 'vitest';
vi.mock('@/database', () => ({
db: {
user: {
findMany: vi.fn().mockResolvedValue([]),
create: vi.fn().mockImplementation((data) => ({
id: '1',
...data.data,
})),
},
},
}));
import { expectTypeOf } from 'vitest';
import type { User } from '@/schemas/user';
test('User type has correct shape', () => {
expectTypeOf<User>().toHaveProperty('id');
expectTypeOf<User>().toHaveProperty('email');
});