|
| 1 | +import '@testing-library/jest-dom'; |
| 2 | + |
| 3 | +// Node < 18 fallback (avoid overriding when native fetch exists) |
| 4 | +if (typeof globalThis.fetch === 'undefined') { |
| 5 | + globalThis.fetch = jest.fn() as unknown as typeof fetch; |
| 6 | +} |
| 7 | + |
| 8 | +// Minimal Request polyfill for tests that do `instanceof Request` |
| 9 | +if (typeof globalThis.Request === 'undefined') { |
| 10 | + globalThis.Request = class Request { |
| 11 | + url: string; |
| 12 | + constructor(input: string | { url: string }) { |
| 13 | + this.url = typeof input === 'string' ? input : input.url; |
| 14 | + } |
| 15 | + } as any; |
| 16 | +} |
| 17 | + |
| 18 | +// Some libs (and JSDOM) expect these to exist |
| 19 | +import { TextDecoder, TextEncoder } from 'util'; |
| 20 | +import { _testLocationHelpers } from './src/utils/location'; |
| 21 | + |
| 22 | +if (typeof globalThis.TextEncoder === 'undefined') { |
| 23 | + globalThis.TextEncoder = TextEncoder as any; |
| 24 | +} |
| 25 | + |
| 26 | +if (typeof globalThis.TextDecoder === 'undefined') { |
| 27 | + globalThis.TextDecoder = TextDecoder as any; |
| 28 | +} |
| 29 | + |
| 30 | +type LocationMocks = { |
| 31 | + assign: jest.Mock<void, [string]>; |
| 32 | + reload: jest.Mock<void, []>; |
| 33 | + replace: jest.Mock<void, [string]>; |
| 34 | +}; |
| 35 | + |
| 36 | +type LocationOverrides = { |
| 37 | + href: string; |
| 38 | + origin: string; |
| 39 | + protocol: string; |
| 40 | + host: string; |
| 41 | + hostname: string; |
| 42 | + port: string; |
| 43 | + pathname: string; |
| 44 | + search: string; |
| 45 | +}; |
| 46 | + |
| 47 | +const setMockLocation = (url: string): LocationMocks => { |
| 48 | + const parsed = new URL(url, 'http://localhost/'); |
| 49 | + |
| 50 | + const overrides: LocationOverrides = { |
| 51 | + href: parsed.href, |
| 52 | + origin: parsed.origin, |
| 53 | + protocol: parsed.protocol, |
| 54 | + host: parsed.host, |
| 55 | + hostname: parsed.hostname, |
| 56 | + port: parsed.port, |
| 57 | + pathname: parsed.pathname, |
| 58 | + search: parsed.search, |
| 59 | + }; |
| 60 | + |
| 61 | + _testLocationHelpers.setOverrides(overrides); |
| 62 | + |
| 63 | + // Use existing mocks if they already exist to prevent losing references in tests |
| 64 | + const existingMocks = _testLocationHelpers.getMocks(); |
| 65 | + if (existingMocks) { |
| 66 | + return existingMocks as unknown as LocationMocks; |
| 67 | + } |
| 68 | + |
| 69 | + const mocks: LocationMocks = { |
| 70 | + assign: jest.fn((nextUrl: string) => { |
| 71 | + setMockLocation(new URL(nextUrl, _testLocationHelpers.getOverrides()?.href || 'http://localhost/').href); |
| 72 | + }), |
| 73 | + replace: jest.fn((nextUrl: string) => { |
| 74 | + setMockLocation(new URL(nextUrl, _testLocationHelpers.getOverrides()?.href || 'http://localhost/').href); |
| 75 | + }), |
| 76 | + reload: jest.fn(), |
| 77 | + }; |
| 78 | + |
| 79 | + _testLocationHelpers.setMocks(mocks as any); |
| 80 | + |
| 81 | + return mocks; |
| 82 | +}; |
| 83 | + |
| 84 | +declare global { |
| 85 | + var setMockLocation: (url: string) => LocationMocks; |
| 86 | +} |
| 87 | + |
| 88 | +// @ts-ignore |
| 89 | +globalThis.setMockLocation = setMockLocation; |
| 90 | + |
| 91 | +// Note: Direct window.location redefinition is blocked in JSDOM (Jest 30). |
| 92 | +// We use the locationUtils abstraction instead (src/utils/location.ts). |
| 93 | + |
| 94 | +beforeEach(() => { |
| 95 | + _testLocationHelpers.setMocks(undefined); |
| 96 | + _testLocationHelpers.setOverrides(undefined); |
| 97 | + setMockLocation('http://localhost/'); |
| 98 | +}); |
| 99 | + |
| 100 | +// Mock import.meta.env for source code that hasn't been transformed by SWC |
| 101 | +// We define it on globalThis as well to ensure it's picked up |
| 102 | +Object.defineProperty(globalThis, 'importMetaEnv', { |
| 103 | + value: { DEV: true, MODE: 'test', VITE_BACKEND_PORT: '3011' }, |
| 104 | + writable: false, |
| 105 | +}); |
| 106 | + |
| 107 | +// Polyfill for source code using import.meta.env directly |
| 108 | +// @ts-ignore |
| 109 | +if (typeof global.importMeta === 'undefined') { |
| 110 | + // @ts-ignore |
| 111 | + global.importMeta = { env: { DEV: true, MODE: 'test' } }; |
| 112 | +} |
0 commit comments