Files
pos-system/services/_template_nodejs/src/__tests__/setupTests.ts
2026-02-23 11:25:27 +00:00

103 lines
2.9 KiB
TypeScript

import { jest } from '@jest/globals';
// EN: Extend global types for test utilities
// VI: Mở rộng global types cho test utilities
declare global {
var testUtils: {
createMockReq: (overrides?: any) => any;
createMockRes: () => any;
createMockNext: () => jest.Mock;
};
}
// EN: Mock environment variables for tests
// VI: Mock biến môi trường cho tests
process.env.NODE_ENV = 'test';
process.env.DATABASE_URL = 'postgresql://test:test@localhost:5432/test_db';
process.env.REDIS_URL = 'redis://localhost:6379/1';
process.env.PORT = '3001';
process.env.SERVICE_NAME = 'test-service';
process.env.API_VERSION = 'v1';
// EN: Mock external services to avoid real network calls
// VI: Mock các service bên ngoài để tránh gọi mạng thật
jest.mock('@goodgo/logger', () => ({
logger: {
info: jest.fn(),
error: jest.fn(),
warn: jest.fn(),
debug: jest.fn(),
},
}), { virtual: true });
// EN: Auth SDK mocking is handled in individual test files
// VI: Auth SDK mocking được xử lý trong từng test file riêng biệt
jest.mock('@goodgo/tracing', () => ({
initTracing: jest.fn(),
}), { virtual: true });
// EN: Mock Redis client to avoid real Redis connections
// VI: Mock Redis client để tránh kết nối Redis thật
jest.mock('../config/redis.config', () => ({
getRedisClient: jest.fn().mockReturnValue({
call: jest.fn(),
connect: jest.fn(),
disconnect: jest.fn(),
}),
}));
// EN: Mock Prometheus registry to avoid global state issues in tests
// VI: Mock Prometheus registry để tránh vấn đề global state trong tests
jest.mock('prom-client', () => {
const mockRegistry = {
registerMetric: jest.fn(),
getMetricsAsJSON: jest.fn().mockReturnValue({}),
metrics: jest.fn().mockReturnValue(''),
};
return {
Registry: jest.fn().mockImplementation(() => mockRegistry),
register: mockRegistry,
collectDefaultMetrics: jest.fn(),
Gauge: jest.fn().mockImplementation(() => ({
set: jest.fn(),
inc: jest.fn(),
dec: jest.fn(),
})),
Counter: jest.fn().mockImplementation(() => ({
inc: jest.fn(),
add: jest.fn(),
})),
Histogram: jest.fn().mockImplementation(() => ({
observe: jest.fn(),
startTimer: jest.fn().mockReturnValue(jest.fn()),
})),
};
});
// EN: Global test utilities
// VI: Utilities test toàn cục
global.testUtils = {
// EN: Helper to create mock request/response objects
// VI: Helper để tạo mock request/response objects
createMockReq: (overrides = {}) => ({
body: {},
params: {},
query: {},
headers: {},
...overrides,
}),
createMockRes: () => {
const res: any = {};
res.status = jest.fn().mockReturnValue(res);
res.json = jest.fn().mockReturnValue(res);
res.send = jest.fn().mockReturnValue(res);
return res;
},
// EN: Helper to create mock next function
// VI: Helper để tạo mock next function
createMockNext: () => jest.fn(),
};