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>
106 lines
2.9 KiB
TypeScript
106 lines
2.9 KiB
TypeScript
/**
|
|
* Tests unitaires pour lib/api-metrics.ts
|
|
*/
|
|
import { NextRequest, NextResponse } from 'next/server';
|
|
|
|
// Mock the metrics module
|
|
jest.mock('@/lib/metrics', () => ({
|
|
recordHttpRequest: jest.fn(),
|
|
}));
|
|
|
|
import { withMetrics } from '@/lib/api-metrics';
|
|
import { recordHttpRequest } from '@/lib/metrics';
|
|
|
|
describe('API Metrics - withMetrics', () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it('should be a function', () => {
|
|
expect(typeof withMetrics).toBe('function');
|
|
});
|
|
|
|
it('should return a function', () => {
|
|
const handler = jest.fn();
|
|
const wrappedHandler = withMetrics(handler);
|
|
expect(typeof wrappedHandler).toBe('function');
|
|
});
|
|
|
|
it('should call the original handler', async () => {
|
|
const mockResponse = NextResponse.json({ success: true });
|
|
const handler = jest.fn().mockResolvedValue(mockResponse);
|
|
const wrappedHandler = withMetrics(handler);
|
|
|
|
const mockRequest = new NextRequest('http://localhost:3000/api/test', {
|
|
method: 'GET',
|
|
});
|
|
|
|
await wrappedHandler(mockRequest);
|
|
|
|
expect(handler).toHaveBeenCalledWith(mockRequest, undefined);
|
|
});
|
|
|
|
it('should record metrics on successful response', async () => {
|
|
const mockResponse = NextResponse.json({ success: true }, { status: 200 });
|
|
const handler = jest.fn().mockResolvedValue(mockResponse);
|
|
const wrappedHandler = withMetrics(handler);
|
|
|
|
const mockRequest = new NextRequest('http://localhost:3000/api/test', {
|
|
method: 'GET',
|
|
});
|
|
|
|
await wrappedHandler(mockRequest);
|
|
|
|
expect(recordHttpRequest).toHaveBeenCalledWith(
|
|
'GET',
|
|
'/api/test',
|
|
200,
|
|
expect.any(Number)
|
|
);
|
|
});
|
|
|
|
it('should record 500 status on error', async () => {
|
|
const handler = jest.fn().mockRejectedValue(new Error('Test error'));
|
|
const wrappedHandler = withMetrics(handler);
|
|
|
|
const mockRequest = new NextRequest('http://localhost:3000/api/test', {
|
|
method: 'POST',
|
|
});
|
|
|
|
await expect(wrappedHandler(mockRequest)).rejects.toThrow('Test error');
|
|
|
|
expect(recordHttpRequest).toHaveBeenCalledWith(
|
|
'POST',
|
|
'/api/test',
|
|
500,
|
|
expect.any(Number)
|
|
);
|
|
});
|
|
|
|
it('should measure duration', async () => {
|
|
const mockResponse = NextResponse.json({ success: true });
|
|
const handler = jest.fn().mockImplementation(async () => {
|
|
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
return mockResponse;
|
|
});
|
|
const wrappedHandler = withMetrics(handler);
|
|
|
|
const mockRequest = new NextRequest('http://localhost:3000/api/test', {
|
|
method: 'GET',
|
|
});
|
|
|
|
await wrappedHandler(mockRequest);
|
|
|
|
expect(recordHttpRequest).toHaveBeenCalledWith(
|
|
expect.any(String),
|
|
expect.any(String),
|
|
expect.any(Number),
|
|
expect.any(Number)
|
|
);
|
|
|
|
// Duration should be >= 10ms
|
|
const [, , , duration] = (recordHttpRequest as jest.Mock).mock.calls[0];
|
|
expect(duration).toBeGreaterThanOrEqual(10);
|
|
});
|
|
});
|