-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
69 lines (62 loc) · 1.65 KB
/
Copy pathvitest.setup.ts
File metadata and controls
69 lines (62 loc) · 1.65 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
import "@testing-library/jest-dom/vitest";
import { cleanup } from "@testing-library/react";
import { afterEach, vi } from "vitest";
vi.mock("@features/utils/logger", () => ({
logger: {
trace: vi.fn(),
debug: vi.fn(),
info: vi.fn(),
log: vi.fn(),
warn: vi.fn(),
error: vi.fn()
}
}));
afterEach(() => {
cleanup();
});
const mockCryptoKey = {
type: "secret",
extractable: false,
algorithm: { name: "HMAC", hash: { name: "SHA-256" } },
usages: ["sign", "verify"]
};
Object.defineProperty(globalThis, "crypto", {
value: {
randomUUID: () => `test-uuid-${Math.random().toString(36).substring(2, 9)}`,
getRandomValues: <T extends ArrayBufferView>(array: T): T => {
if (array instanceof Uint8Array) {
for (let i = 0; i < array.length; i++) {
array[i] = Math.floor(Math.random() * 256);
}
}
return array;
},
subtle: {
importKey: vi.fn().mockResolvedValue(mockCryptoKey),
sign: vi.fn().mockImplementation(async (_algorithm, _key, data) => {
// Return a deterministic hash based on the data for testing
const dataArray = new Uint8Array(data);
const hash = new Uint8Array(32);
for (let i = 0; i < 32; i++) {
hash[i] = dataArray[i % dataArray.length] ^ (i * 7);
}
return hash.buffer;
}),
verify: vi.fn().mockResolvedValue(true)
}
}
});
const mockGeolocation = {
getCurrentPosition: vi.fn(),
watchPosition: vi.fn(),
clearWatch: vi.fn()
};
Object.defineProperty(globalThis, "navigator", {
value: {
geolocation: mockGeolocation,
permissions: {
query: vi.fn()
}
},
writable: true
});