setup.ts79 lines, 2.1 KB
| 1 | import { JWTPayload } from "../../../shared/types"; |
| 2 | |
| 3 | // Mock environment variables |
| 4 | process.env.JWT_SECRET = "test-jwt-secret-key-for-testing-only"; |
| 5 | process.env.JWT_REFRESH_SECRET = "test-jwt-refresh-secret-key-for-testing-only"; |
| 6 | process.env.JWT_EXPIRES_IN = "15m"; |
| 7 | process.env.JWT_REFRESH_EXPIRES_IN = "7d"; |
| 8 | process.env.BCRYPT_ROUNDS = "4"; // Lower rounds for faster tests |
| 9 | process.env.NODE_ENV = "test"; |
| 10 | |
| 11 | // Mock prisma client |
| 12 | const mockPrismaClient = { |
| 13 | user: { |
| 14 | create: jest.fn(), |
| 15 | findUnique: jest.fn(), |
| 16 | findMany: jest.fn(), |
| 17 | update: jest.fn(), |
| 18 | delete: jest.fn(), |
| 19 | count: jest.fn(), |
| 20 | }, |
| 21 | refreshToken: { |
| 22 | create: jest.fn(), |
| 23 | findUnique: jest.fn(), |
| 24 | findMany: jest.fn(), |
| 25 | deleteMany: jest.fn(), |
| 26 | update: jest.fn(), |
| 27 | delete: jest.fn(), |
| 28 | }, |
| 29 | $disconnect: jest.fn(), |
| 30 | $connect: jest.fn(), |
| 31 | }; |
| 32 | |
| 33 | // Mock the database module |
| 34 | jest.mock("../src/database", () => mockPrismaClient); |
| 35 | |
| 36 | // mock test utils |
| 37 | global.mockPrisma = mockPrismaClient; |
| 38 | |
| 39 | // global data for test |
| 40 | export const testUser = { |
| 41 | id: "test-user-id", |
| 42 | email: "testuser123@domain.com", |
| 43 | password: "$2a$04$hashedpasswordhashedpasswordhashedpasswordhashedpassword", |
| 44 | createdAt: new Date("2025-07-01T00:00:00Z"), |
| 45 | updatedAt: new Date("2025-07-01T00:00:00Z"), |
| 46 | }; |
| 47 | |
| 48 | export const testRefreshToken = { |
| 49 | id: "test-refresh-token-id", |
| 50 | userId: testUser.id, |
| 51 | token: "test-refresh-token", |
| 52 | expiresAt: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000), // 7 days from now |
| 53 | createdAt: new Date(), |
| 54 | }; |
| 55 | |
| 56 | export const testJwtPayload: JWTPayload = { |
| 57 | userId: "test-user-id", |
| 58 | email: testUser.email, |
| 59 | iat: Math.floor(Date.now() / 1000), |
| 60 | exp: Math.floor(Date.now() / 1000) + 60 * 15, // 15 minutes from now |
| 61 | }; |
| 62 | |
| 63 | // helper function to reset mocks before each test |
| 64 | export function resetAllMocks() { |
| 65 | Object.values(mockPrismaClient.user).forEach((mock) => { |
| 66 | if (jest.isMockFunction(mock)) { |
| 67 | mock.mockReset(); |
| 68 | } |
| 69 | }); |
| 70 | Object.values(mockPrismaClient.refreshToken).forEach((mock) => { |
| 71 | if (jest.isMockFunction(mock)) { |
| 72 | mock.mockReset(); |
| 73 | } |
| 74 | }); |
| 75 | } |
| 76 | |
| 77 | declare global { |
| 78 | var mockPrisma: typeof mockPrismaClient; |
| 79 | } |