the-tip-top-frontend/__tests__/lib/metrics.test.ts
soufiane 1e9f16fded test: add Jest testing setup and unit tests
Frontend Tests Added:
- Jest configuration with TypeScript support
- Jest setup with Next.js mocks
- Unit tests for lib/metrics.ts (normalizePath, registry)
- Unit tests for lib/api-metrics.ts (withMetrics wrapper)
- Unit tests for middleware (auth routes, token detection)
- Unit tests for API track route (payload validation)

Dependencies added:
- jest, @testing-library/react, @testing-library/jest-dom
- ts-jest, jest-environment-jsdom, @types/jest

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-27 11:24:46 +01:00

74 lines
2.3 KiB
TypeScript

/**
* Tests unitaires pour lib/metrics.ts
*/
import { normalizePath } from '@/lib/metrics';
describe('Metrics - normalizePath', () => {
it('should replace UUIDs with :id', () => {
const path = '/api/users/123e4567-e89b-12d3-a456-426614174000';
const normalized = normalizePath(path);
expect(normalized).toBe('/api/users/:id');
});
it('should replace numeric IDs with :id', () => {
const path = '/api/tickets/12345';
const normalized = normalizePath(path);
expect(normalized).toBe('/api/tickets/:id');
});
it('should replace ticket codes with :code', () => {
const path = '/api/game/ticket/TTP2025ABC';
const normalized = normalizePath(path);
expect(normalized).toBe('/api/game/ticket/:code');
});
it('should handle multiple replacements', () => {
const path = '/api/users/123e4567-e89b-12d3-a456-426614174000/tickets/12345';
const normalized = normalizePath(path);
expect(normalized).toBe('/api/users/:id/tickets/:id');
});
it('should not modify paths without IDs', () => {
const path = '/api/auth/login';
const normalized = normalizePath(path);
expect(normalized).toBe('/api/auth/login');
});
it('should handle root path', () => {
const path = '/';
const normalized = normalizePath(path);
expect(normalized).toBe('/');
});
it('should handle paths with query strings', () => {
const path = '/api/users/123';
const normalized = normalizePath(path);
expect(normalized).toBe('/api/users/:id');
});
});
describe('Metrics - recordHttpRequest', () => {
// Note: These tests would require mocking prom-client
// In a real scenario, we would mock the Counter and Histogram
it('should be a function', async () => {
const { recordHttpRequest } = await import('@/lib/metrics');
expect(typeof recordHttpRequest).toBe('function');
});
});
describe('Metrics - getRegistry', () => {
it('should return a registry', async () => {
const { getRegistry } = await import('@/lib/metrics');
const registry = getRegistry();
expect(registry).toBeDefined();
});
it('should return the same registry instance', async () => {
const { getRegistry } = await import('@/lib/metrics');
const registry1 = getRegistry();
const registry2 = getRegistry();
expect(registry1).toBe(registry2);
});
});