setup.ts78 lines, 1.9 KB
| 1 | import axios from "axios"; |
| 2 | |
| 3 | // Mock environment variables |
| 4 | process.env.AUTH_SERVICE_URL = "http://localhost:3001"; |
| 5 | process.env.NODE_ENV = "test"; |
| 6 | |
| 7 | // Mock prisma client |
| 8 | const mockPrismaClient = { |
| 9 | userProfile: { |
| 10 | create: jest.fn(), |
| 11 | findUnique: jest.fn(), |
| 12 | findMany: jest.fn(), |
| 13 | update: jest.fn(), |
| 14 | delete: jest.fn(), |
| 15 | count: jest.fn(), |
| 16 | }, |
| 17 | $disconnect: jest.fn(), |
| 18 | $connect: jest.fn(), |
| 19 | }; |
| 20 | |
| 21 | // Mock the database module |
| 22 | jest.mock("../src/database", () => mockPrismaClient); |
| 23 | |
| 24 | // Mock axios for Authclient |
| 25 | jest.mock("axios"); |
| 26 | const mockedAxios = axios as jest.Mocked<typeof axios>; |
| 27 | |
| 28 | // Global test utils |
| 29 | global.mockPrisma = mockPrismaClient; |
| 30 | global.mockAxios = mockedAxios; |
| 31 | |
| 32 | // Reset all mocks before each test |
| 33 | beforeEach(() => { |
| 34 | jest.clearAllMocks(); |
| 35 | }); |
| 36 | |
| 37 | // global data for test |
| 38 | export const testUserProfile = { |
| 39 | id: "test-user-id-123", |
| 40 | userId: "test-user-id", |
| 41 | firstName: "Test", |
| 42 | lastName: "User", |
| 43 | bio: "This is a test user profile.", |
| 44 | avatarUrl: "http://example.com/avatar.jpg", |
| 45 | preferences: { theme: "dark", notifications: true }, |
| 46 | createdAt: new Date("2025-07-01T00:00:00Z"), |
| 47 | updatedAt: new Date("2025-07-01T00:00:00Z"), |
| 48 | }; |
| 49 | |
| 50 | export const testUpdateProfileRequest = { |
| 51 | firstName: "Updated", |
| 52 | lastName: "User", |
| 53 | bio: "This is an updated test user profile.", |
| 54 | avatarUrl: "http://example.com/updated-avatar.jpg", |
| 55 | preferences: { theme: "light", notifications: false }, |
| 56 | }; |
| 57 | |
| 58 | export const testJwtPayload = { |
| 59 | userId: "test-user-id", |
| 60 | email: "test@example.com", |
| 61 | iat: Math.floor(Date.now() / 1000), |
| 62 | exp: Math.floor(Date.now() / 1000) + 60 * 15, // 15 minutes from now |
| 63 | }; |
| 64 | |
| 65 | // helper function to reset mocks before each test |
| 66 | export function resetAllMocks() { |
| 67 | Object.values(mockPrismaClient.userProfile).forEach((mock) => { |
| 68 | if (jest.isMockFunction(mock)) { |
| 69 | mock.mockReset(); |
| 70 | } |
| 71 | }); |
| 72 | mockedAxios.post.mockReset(); |
| 73 | } |
| 74 | |
| 75 | declare global { |
| 76 | var mockPrisma: typeof mockPrismaClient; |
| 77 | var mockAxios: jest.Mocked<typeof axios>; |
| 78 | } |