|
| 1 | +import * as urlLib from 'url'; |
| 2 | + |
1 | 3 | // Global test setup
|
2 | 4 | // Force mock mode so server-side handlers use local mock data instead of hitting GitHub APIs.
|
3 | 5 | process.env.NUXT_PUBLIC_IS_DATA_MOCKED = 'true';
|
4 | 6 |
|
| 7 | +function isGitHub(url: string): boolean { |
| 8 | + const host = urlLib.parse(url).host; |
| 9 | + return host === 'api.github.com' || host === 'api.github.com:443'; |
| 10 | +} |
| 11 | + |
5 | 12 | // Block accidental real GitHub API calls if mock mode logic fails.
|
6 | 13 | const originalFetch: typeof globalThis.fetch | undefined = globalThis.fetch;
|
7 | 14 | if (originalFetch) {
|
8 |
| - globalThis.fetch = (async (...args: Parameters<typeof fetch>): Promise<Response> => { |
9 |
| - const url = String(args[0]); |
10 |
| - if (url.startsWith('https://api.github.com')) { |
11 |
| - throw new Error(`Blocked external GitHub API call during tests: ${url}`); |
12 |
| - } |
13 |
| - return originalFetch(...args); |
14 |
| - }) as typeof fetch; |
| 15 | + globalThis.fetch = (async (...args: Parameters<typeof fetch>): Promise<Response> => { |
| 16 | + const url = String(args[0]); |
| 17 | + if (isGitHub(url)) { |
| 18 | + throw new Error(`Blocked external GitHub API call during tests: ${url}`); |
| 19 | + } |
| 20 | + return originalFetch(...args); |
| 21 | + }) as typeof fetch; |
15 | 22 | }
|
16 | 23 |
|
17 | 24 | // Stub $fetch (ofetch) similarly if present at runtime.
|
18 | 25 | if (typeof globalThis.$fetch === 'function') {
|
19 |
| - const original$fetch = globalThis.$fetch; |
20 |
| - const wrapped: typeof globalThis.$fetch = ((url: unknown, opts: unknown) => { |
21 |
| - const str = String(url); |
22 |
| - if (str.startsWith('https://api.github.com')) { |
23 |
| - return Promise.reject(new Error(`Blocked external GitHub API call during tests via $fetch: ${str}`)); |
24 |
| - } |
25 |
| - return original$fetch(url as never, opts as never); |
26 |
| - }) as typeof globalThis.$fetch; |
27 |
| - // Preserve special properties if present |
28 |
| - (wrapped as unknown as { raw?: unknown }).raw = (original$fetch as unknown as { raw?: () => unknown }).raw?.bind(original$fetch); |
29 |
| - (wrapped as unknown as { create?: unknown }).create = (original$fetch as unknown as { create?: () => unknown }).create?.bind(original$fetch); |
30 |
| - globalThis.$fetch = wrapped; |
| 26 | + const original$fetch = globalThis.$fetch; |
| 27 | + const wrapped: typeof globalThis.$fetch = ((url: unknown, opts: unknown) => { |
| 28 | + const str = String(url); |
| 29 | + if (isGitHub(str)) { |
| 30 | + return Promise.reject(new Error(`Blocked external GitHub API call during tests via $fetch: ${str}`)); |
| 31 | + } |
| 32 | + return original$fetch(url as never, opts as never); |
| 33 | + }) as typeof globalThis.$fetch; |
| 34 | + // Preserve special properties if present |
| 35 | + (wrapped as unknown as { raw?: unknown }).raw = (original$fetch as unknown as { raw?: () => unknown }).raw?.bind(original$fetch); |
| 36 | + (wrapped as unknown as { create?: unknown }).create = (original$fetch as unknown as { create?: () => unknown }).create?.bind(original$fetch); |
| 37 | + globalThis.$fetch = wrapped; |
31 | 38 | }
|
0 commit comments