Skip to content

Commit bfabca6

Browse files
committed
fix test setup
1 parent bca89fa commit bfabca6

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

tests/test.setup.ts

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,38 @@
1+
import * as urlLib from 'url';
2+
13
// Global test setup
24
// Force mock mode so server-side handlers use local mock data instead of hitting GitHub APIs.
35
process.env.NUXT_PUBLIC_IS_DATA_MOCKED = 'true';
46

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+
512
// Block accidental real GitHub API calls if mock mode logic fails.
613
const originalFetch: typeof globalThis.fetch | undefined = globalThis.fetch;
714
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;
1522
}
1623

1724
// Stub $fetch (ofetch) similarly if present at runtime.
1825
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;
3138
}

vitest.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default defineVitestConfig({
55
test: {
66
exclude: ['**/node_modules/**', '**/e2e-tests/**'],
77
environment: 'nuxt',
8-
globals: true, // Use describe, test/expect, etc. without importing
9-
setupFiles: ['./tests/test.setup.ts']
8+
globals: true, // Use describe, test/expect, etc. without importing
9+
setupFiles: ['./tests/test.setup.ts']
1010
}
1111
})

0 commit comments

Comments
 (0)