-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.js
More file actions
87 lines (80 loc) · 2.21 KB
/
jest.setup.js
File metadata and controls
87 lines (80 loc) · 2.21 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
import '@testing-library/jest-dom'
// Mock Next.js router
jest.mock('next/router', () => ({
useRouter() {
return {
route: '/',
pathname: '/',
query: {},
asPath: '/',
push: jest.fn(),
replace: jest.fn(),
}
},
}))
// Mock TextEncoder and TextDecoder first (needed by crypto mocks)
global.TextEncoder = class TextEncoder {
encode(string) {
return new Uint8Array(Buffer.from(string, 'utf8'))
}
}
global.TextDecoder = class TextDecoder {
decode(uint8Array) {
return Buffer.from(uint8Array).toString('utf8')
}
}
// Mock crypto APIs including WebCrypto for encryption tests
const mockCrypto = {
randomUUID: () => 'test-uuid-12345',
getRandomValues: jest.fn((array) => {
// Fill with truly random values for each call
for (let i = 0; i < array.length; i++) {
array[i] = Math.floor(Math.random() * 256)
}
return array
}),
subtle: {
importKey: jest.fn(() => Promise.resolve({ type: 'secret', algorithm: 'PBKDF2' })),
deriveKey: jest.fn(() => Promise.resolve({ type: 'secret', algorithm: 'AES-GCM' })),
encrypt: jest.fn(() => {
// Return a deterministic encrypted result
const result = new ArrayBuffer(32) // 32 bytes for test
const view = new Uint8Array(result)
for (let i = 0; i < view.length; i++) {
view[i] = (i + 100) % 256 // Deterministic "encrypted" data
}
return Promise.resolve(result)
}),
decrypt: jest.fn((algorithm, key, data) => {
// Return "decrypted content" as ArrayBuffer
const encoder = new TextEncoder()
return Promise.resolve(encoder.encode('decrypted content').buffer)
})
}
}
// Set up crypto globally
Object.defineProperty(global, 'crypto', {
value: mockCrypto,
writable: true,
enumerable: true,
configurable: true
})
if (!global.fetch) {
global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
json: () => Promise.resolve({}),
})
)
}
// Mock Supabase
jest.mock('./lib/supabase', () => ({
supabase: {
from: jest.fn(() => ({
select: jest.fn(() => ({
eq: jest.fn(() => Promise.resolve({ data: [], error: null }))
})),
upsert: jest.fn(() => Promise.resolve({ data: {}, error: null }))
}))
}
}))