-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
90 lines (81 loc) · 3.04 KB
/
Copy pathvitest.setup.ts
File metadata and controls
90 lines (81 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/// <reference types="vitest/globals" />
import '@testing-library/jest-dom';
import { vi } from 'vitest';
import { setupMocks } from './__tests__/setup/mocks';
// Mock the entire queue module to prevent worker initialization
vi.mock('@/lib/queue', async () => {
return {
// Mock only the parts that trigger worker initialization
subscriptionWorker: { on: vi.fn(), close: vi.fn() },
documentsWorker: { on: vi.fn(), close: vi.fn() },
emailWorker: { on: vi.fn(), close: vi.fn() },
// Mock queue functions
addToMarketingSegmentJob: vi.fn(() => Promise.resolve()),
removeFromMarketingSegmentJob: vi.fn(() => Promise.resolve()),
addIndexDocumentJob: vi.fn(() => Promise.resolve()),
addSubscriptionSyncJob: vi.fn(() => Promise.resolve()),
scheduleSubscriptionSync: vi.fn(() => Promise.resolve()),
scheduleAllJobs: vi.fn(() => Promise.resolve()),
};
});
// Mock Redis client FIRST before any imports
vi.mock('@/lib/redis', () => ({
redis: {
get: vi.fn(() => Promise.resolve(null)),
set: vi.fn(() => Promise.resolve('OK')),
del: vi.fn(() => Promise.resolve(1)),
quit: vi.fn(() => Promise.resolve('OK')),
on: vi.fn(),
},
closeRedis: vi.fn(() => Promise.resolve()),
}));
// Mock BullMQ to prevent worker initialization during tests
vi.mock('bullmq', () => ({
Queue: vi.fn().mockImplementation(() => ({
add: vi.fn(() => Promise.resolve({ id: 'mock-job-id' })),
addBulk: vi.fn(() => Promise.resolve([])),
getJob: vi.fn(() => Promise.resolve(null)),
getJobs: vi.fn(() => Promise.resolve([])),
close: vi.fn(() => Promise.resolve()),
on: vi.fn(),
})),
Worker: vi.fn().mockImplementation(() => ({
on: vi.fn(),
close: vi.fn(() => Promise.resolve()),
run: vi.fn(),
})),
QueueEvents: vi.fn().mockImplementation(() => ({
on: vi.fn(),
close: vi.fn(() => Promise.resolve()),
})),
}));
// Setup mocks before all tests
setupMocks();
// Mock IntersectionObserver which is not available in jsdom
if (typeof window !== 'undefined') {
window.IntersectionObserver = class MockIntersectionObserver implements IntersectionObserver {
readonly root: Element | null = null;
readonly rootMargin: string = '0px';
readonly thresholds: ReadonlyArray<number> = [0];
constructor() {}
observe(): void {}
unobserve(): void {}
disconnect(): void {}
takeRecords(): IntersectionObserverEntry[] {
return [];
}
};
}
// Suppress console errors/warnings in test output
beforeAll(() => {
console.error = vi.fn();
console.warn = vi.fn();
});
// Setup environment variables for testing
process.env.TEST_DATABASE_URL = 'postgresql://postgres:postgres@localhost:5432/kosuke_test';
process.env.POSTGRES_URL = 'postgresql://postgres:postgres@localhost:5432/kosuke_test';
process.env.NEXT_PUBLIC_APP_URL = 'http://localhost:3000';
process.env.BETTER_AUTH_SECRET = 'test-secret-key-for-testing-only';
process.env.GOOGLE_AI_API_KEY = 'test-google-ai-api-key';
process.env.RESEND_API_KEY = 'test-resend-api-key';
process.env.ENCRYPTION_KEY = 'test-encryption-key-minimum-32-chars-long';