setup.ts77 lines, 1.7 KB
| 1 | // Mock environment variables |
| 2 | process.env.NODE_ENV = "test"; |
| 3 | |
| 4 | // Mock prisma client |
| 5 | const mockPrismaClient = { |
| 6 | note: { |
| 7 | create: jest.fn(), |
| 8 | findUnique: jest.fn(), |
| 9 | findFirst: jest.fn(), |
| 10 | findMany: jest.fn(), |
| 11 | update: jest.fn(), |
| 12 | delete: jest.fn(), |
| 13 | count: jest.fn(), |
| 14 | }, |
| 15 | noteTag: { |
| 16 | create: jest.fn(), |
| 17 | findUnique: jest.fn(), |
| 18 | findMany: jest.fn(), |
| 19 | deleteMany: jest.fn(), |
| 20 | update: jest.fn(), |
| 21 | delete: jest.fn(), |
| 22 | }, |
| 23 | $disconnect: jest.fn(), |
| 24 | $connect: jest.fn(), |
| 25 | }; |
| 26 | |
| 27 | // Mock the database module |
| 28 | jest.mock("../src/database", () => mockPrismaClient); |
| 29 | |
| 30 | // mock test utils |
| 31 | global.mockPrisma = mockPrismaClient; |
| 32 | |
| 33 | //Reset all mocks before each test |
| 34 | beforeEach(() => { |
| 35 | jest.clearAllMocks(); |
| 36 | }); |
| 37 | |
| 38 | // global data for test |
| 39 | export const testNote = { |
| 40 | id: "test-note-id-123", |
| 41 | userId: "test-user-id-123", |
| 42 | title: "Test Note Title", |
| 43 | content: "This is test note content", |
| 44 | isDeleted: false, |
| 45 | createdAt: new Date("2024-01-01T00:00:00.000Z"), |
| 46 | updatedAt: new Date("2024-01-01T00:00:00.000Z"), |
| 47 | noteTags: [], |
| 48 | }; |
| 49 | |
| 50 | export const testCreateNoteRequest = { |
| 51 | title: "New Test Note", |
| 52 | content: "This is new test note content", |
| 53 | tagIds: ["tag-id-1", "tag-id-2"], |
| 54 | }; |
| 55 | |
| 56 | export const testUpdateNoteRequest = { |
| 57 | title: "Updated Test Note", |
| 58 | content: "This is updated test note content", |
| 59 | }; |
| 60 | |
| 61 | // helper function to reset mocks before each test |
| 62 | export function resetAllMocks() { |
| 63 | Object.values(mockPrismaClient.note).forEach((mock) => { |
| 64 | if (jest.isMockFunction(mock)) { |
| 65 | mock.mockReset(); |
| 66 | } |
| 67 | }); |
| 68 | Object.values(mockPrismaClient.noteTag).forEach((mock) => { |
| 69 | if (jest.isMockFunction(mock)) { |
| 70 | mock.mockReset(); |
| 71 | } |
| 72 | }); |
| 73 | } |
| 74 | |
| 75 | declare global { |
| 76 | var mockPrisma: typeof mockPrismaClient; |
| 77 | } |