|
| 1 | +// Test setup file to provide necessary polyfills for starknet library |
| 2 | +import { vi } from "vitest" |
| 3 | + |
| 4 | +// Mock WebSocket for Node.js environment |
| 5 | +global.WebSocket = vi.fn().mockImplementation(() => ({ |
| 6 | + addEventListener: vi.fn(), |
| 7 | + removeEventListener: vi.fn(), |
| 8 | + send: vi.fn(), |
| 9 | + close: vi.fn(), |
| 10 | + readyState: 1, // OPEN |
| 11 | + CONNECTING: 0, |
| 12 | + OPEN: 1, |
| 13 | + CLOSING: 2, |
| 14 | + CLOSED: 3, |
| 15 | +})) as any |
| 16 | + |
| 17 | +// Mock fetch if not available |
| 18 | +if (!global.fetch) { |
| 19 | + global.fetch = vi.fn() |
| 20 | +} |
| 21 | + |
| 22 | +// Mock crypto if not available |
| 23 | +if (!global.crypto) { |
| 24 | + global.crypto = { |
| 25 | + getRandomValues: vi.fn((arr) => { |
| 26 | + for (let i = 0; i < arr.length; i++) { |
| 27 | + arr[i] = Math.floor(Math.random() * 256) |
| 28 | + } |
| 29 | + return arr |
| 30 | + }), |
| 31 | + subtle: { |
| 32 | + generateKey: vi.fn(), |
| 33 | + importKey: vi.fn(), |
| 34 | + exportKey: vi.fn(), |
| 35 | + sign: vi.fn(), |
| 36 | + verify: vi.fn(), |
| 37 | + digest: vi.fn(), |
| 38 | + }, |
| 39 | + } as any |
| 40 | +} |
| 41 | + |
| 42 | +// Mock TextEncoder/TextDecoder if not available |
| 43 | +if (!global.TextEncoder) { |
| 44 | + global.TextEncoder = class TextEncoder { |
| 45 | + encode(input?: string): Uint8Array { |
| 46 | + return new Uint8Array(Buffer.from(input || "", "utf8")) |
| 47 | + } |
| 48 | + } as any |
| 49 | +} |
| 50 | + |
| 51 | +if (!global.TextDecoder) { |
| 52 | + global.TextDecoder = class TextDecoder { |
| 53 | + decode(input?: Uint8Array): string { |
| 54 | + return Buffer.from(input || new Uint8Array()).toString("utf8") |
| 55 | + } |
| 56 | + } as any |
| 57 | +} |
| 58 | + |
| 59 | +// Mock performance if not available |
| 60 | +if (!global.performance) { |
| 61 | + global.performance = { |
| 62 | + now: vi.fn(() => Date.now()), |
| 63 | + } as any |
| 64 | +} |
| 65 | + |
| 66 | +// Mock localStorage if not available |
| 67 | +if (!global.localStorage) { |
| 68 | + global.localStorage = { |
| 69 | + getItem: vi.fn(), |
| 70 | + setItem: vi.fn(), |
| 71 | + removeItem: vi.fn(), |
| 72 | + clear: vi.fn(), |
| 73 | + key: vi.fn(), |
| 74 | + length: 0, |
| 75 | + } as any |
| 76 | +} |
| 77 | + |
| 78 | +// Mock sessionStorage if not available |
| 79 | +if (!global.sessionStorage) { |
| 80 | + global.sessionStorage = { |
| 81 | + getItem: vi.fn(), |
| 82 | + setItem: vi.fn(), |
| 83 | + removeItem: vi.fn(), |
| 84 | + clear: vi.fn(), |
| 85 | + key: vi.fn(), |
| 86 | + length: 0, |
| 87 | + } as any |
| 88 | +} |
0 commit comments