|
| 1 | +import fs from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | + |
| 4 | +import { createFetch, isLocalhostSubdomain } from '../src/localhost-fetch.browser'; |
| 5 | + |
| 6 | +describe('browser build — static analysis', () => { |
| 7 | + const distDir = path.resolve(__dirname, '../dist'); |
| 8 | + |
| 9 | + const browserFiles = [ |
| 10 | + 'index.browser.js', |
| 11 | + 'localhost-fetch.browser.js', |
| 12 | + path.join('esm', 'index.browser.js'), |
| 13 | + path.join('esm', 'localhost-fetch.browser.js'), |
| 14 | + ]; |
| 15 | + |
| 16 | + it.each(browserFiles)('%s contains no node: scheme imports', (file) => { |
| 17 | + const filePath = path.join(distDir, file); |
| 18 | + if (!fs.existsSync(filePath)) { |
| 19 | + // In CI, missing artifacts must fail — otherwise this regression |
| 20 | + // check silently passes when tests run before the build step. |
| 21 | + if (process.env.CI) { |
| 22 | + throw new Error( |
| 23 | + `Built artifact missing: ${filePath} — run 'makage build' before tests in CI`, |
| 24 | + ); |
| 25 | + } |
| 26 | + console.warn(`SKIP: ${filePath} not found (run 'makage build' first)`); |
| 27 | + return; |
| 28 | + } |
| 29 | + const content = fs.readFileSync(filePath, 'utf8'); |
| 30 | + // Catch any node: URI scheme import (node:http, node:https, node:crypto, ...) |
| 31 | + expect(content).not.toMatch(/['"]node:[a-z]+['"]/); |
| 32 | + expect(content).not.toContain("require('node:"); |
| 33 | + expect(content).not.toContain('require("node:'); |
| 34 | + }); |
| 35 | +}); |
| 36 | + |
| 37 | +describe('browser build — createFetch behavior', () => { |
| 38 | + it('returns a function', () => { |
| 39 | + const fetch = createFetch(); |
| 40 | + expect(typeof fetch).toBe('function'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('returns the same instance on repeated calls', () => { |
| 44 | + const a = createFetch(); |
| 45 | + const b = createFetch(); |
| 46 | + expect(a).toBe(b); |
| 47 | + }); |
| 48 | + |
| 49 | + it('delegates to globalThis.fetch', async () => { |
| 50 | + // The shim caches the bound fetch at module scope, so to observe the spy |
| 51 | + // we must load a fresh module instance *after* installing the spy. |
| 52 | + const spy = jest |
| 53 | + .spyOn(globalThis, 'fetch') |
| 54 | + .mockResolvedValueOnce(new Response('ok')); |
| 55 | + try { |
| 56 | + await jest.isolateModulesAsync(async () => { |
| 57 | + const { createFetch: freshCreateFetch } = await import( |
| 58 | + '../src/localhost-fetch.browser' |
| 59 | + ); |
| 60 | + const fetch = freshCreateFetch(); |
| 61 | + const res = await fetch('https://example.com'); |
| 62 | + expect(spy).toHaveBeenCalledWith('https://example.com'); |
| 63 | + expect(await res.text()).toBe('ok'); |
| 64 | + }); |
| 65 | + } finally { |
| 66 | + spy.mockRestore(); |
| 67 | + } |
| 68 | + }); |
| 69 | +}); |
| 70 | + |
| 71 | +describe('browser build — isLocalhostSubdomain', () => { |
| 72 | + it('returns true for *.localhost', () => { |
| 73 | + expect(isLocalhostSubdomain('auth.localhost')).toBe(true); |
| 74 | + expect(isLocalhostSubdomain('api.localhost')).toBe(true); |
| 75 | + }); |
| 76 | + |
| 77 | + it('returns false for bare localhost', () => { |
| 78 | + expect(isLocalhostSubdomain('localhost')).toBe(false); |
| 79 | + }); |
| 80 | + |
| 81 | + it('returns false for non-localhost', () => { |
| 82 | + expect(isLocalhostSubdomain('example.com')).toBe(false); |
| 83 | + }); |
| 84 | +}); |
0 commit comments