Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix
  • Loading branch information
schiller-manuel committed May 23, 2026
commit c5c48ed7b16524b7aecc1be9474c62d580c95fcb
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@ test.describe('transformAssets with CDN prefix', () => {
const html = await getSSRHtml(page)

// All script preload links should point to the CDN origin
const scriptPreloads = html.match(/rel="modulepreload"[^>]*href="([^"]+)"/g)
const scriptPreloads = Array.from(
html.matchAll(
/<link\b(?=[^>]*\brel="modulepreload")(?=[^>]*\bhref="([^"]+)")[^>]*>/g,
),
)
expect(scriptPreloads).toBeTruthy()
expect(scriptPreloads!.length).toBeGreaterThan(0)
expect(scriptPreloads.length).toBeGreaterThan(0)

for (const match of scriptPreloads!) {
const href = match.match(/href="([^"]+)"/)?.[1]
for (const match of scriptPreloads) {
const href = match[1]
expect(href).toBeTruthy()
expect(href).toMatch(/^http:\/\/localhost:\d+\//)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,19 @@ function __report(action, accessPath) {
`
: ''

const diagGetTraps = hasDiag
? `
const primitiveGetTraps = `
if (prop === Symbol.toPrimitive) {
return () => {
__report('toPrimitive', name);
${hasDiag ? `__report('toPrimitive', name);` : ''}
return '[import-protection mock]';
};
}
if (prop === 'toString' || prop === 'valueOf' || prop === 'toJSON') {
return () => {
__report(String(prop), name);
${hasDiag ? `__report(String(prop), name);` : ''}
return '[import-protection mock]';
};
}`
: ''

const applyBody = hasDiag
? `__report('call', name + '()');
Expand Down Expand Up @@ -151,7 +149,7 @@ function ${fnName}(name) {
if (prop === 'caller') return null;
if (prop === 'then') return (f) => Promise.resolve(f(proxy));
if (prop === 'catch') return () => Promise.resolve(proxy);
if (prop === 'finally') return (f) => { f(); return Promise.resolve(proxy); };${diagGetTraps}
if (prop === 'finally') return (f) => { f(); return Promise.resolve(proxy); };${primitiveGetTraps}
if (typeof prop === 'symbol') return undefined;
if (!(prop in children)) {
children[prop] = ${fnName}(name + '.' + prop);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@ import {
} from '../../src/vite/import-protection-plugin/virtualModules'
import type { ViolationInfo } from '../../src/import-protection/trace'

type MockValue = {
toString: () => string
valueOf: () => string
toJSON: () => string
}

type MockFunction = {
(): MockValue
}

function evaluateGeneratedModule<T>(code: string): T {
const exports: Record<string, unknown> = {}
const runnableCode = code
.replace(/export default mock;?/g, 'exports.default = mock;')
.replace(/export const ([A-Za-z_$][\w$]*) = ([^;]+);/g, 'exports.$1 = $2;')

new Function('exports', runnableCode)(exports)
return exports as T
}

describe('loadSilentMockModule', () => {
test('returns mock code', () => {
const result = loadSilentMockModule()
Expand All @@ -23,6 +43,17 @@ describe('loadSilentMockModule', () => {
expect(result.code).toContain('@__NO_SIDE_EFFECTS__')
expect(result.code).toContain('@__PURE__')
})

test('supports primitive conversion for called mocks', async () => {
const result = loadSilentMockModule()
const mod = evaluateGeneratedModule<{ default: MockFunction }>(result.code)
const value = mod.default()

expect(String(value)).toBe('[import-protection mock]')
expect(value.toString()).toBe('[import-protection mock]')
expect(value.valueOf()).toBe('[import-protection mock]')
expect(JSON.stringify(value)).toBe('"[import-protection mock]"')
})
})

describe('loadMarkerModule', () => {
Expand Down Expand Up @@ -268,7 +299,16 @@ describe('generateSelfContainedMockModule', () => {
test('is self-contained (no imports)', () => {
const result = generateSelfContainedMockModule(['foo'])
// Should not import from any other module
expect(result.code).not.toMatch(/\bimport\b/)
expect(result.code).not.toMatch(/^\s*import\s/m)
})

test('supports primitive conversion for named export calls', async () => {
const result = generateSelfContainedMockModule(['getSecret'])
const mod = evaluateGeneratedModule<{ getSecret: MockFunction }>(
result.code,
)

expect(String(mod.getSecret())).toBe('[import-protection mock]')
})
})

Expand Down