diff --git a/.changeset/big-cobras-sip.md b/.changeset/big-cobras-sip.md new file mode 100644 index 000000000000..4436c6b4e4e2 --- /dev/null +++ b/.changeset/big-cobras-sip.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +Disable illegal import detection when running unit tests diff --git a/documentation/docs/30-advanced/50-server-only-modules.md b/documentation/docs/30-advanced/50-server-only-modules.md index c14a4f21d52d..e6c424003ab6 100644 --- a/documentation/docs/30-advanced/50-server-only-modules.md +++ b/documentation/docs/30-advanced/50-server-only-modules.md @@ -51,4 +51,6 @@ Cannot import $lib/server/secrets.js into public-facing code: Even though the public-facing code — `src/routes/+page.svelte` — only uses the `add` export and not the secret `atlantisCoordinates` export, the secret code could end up in JavaScript that the browser downloads, and so the import chain is considered unsafe. -This feature also works with dynamic imports, even interpolated ones like ``await import(`./${foo}.js`)``, with one small caveat: during development, if there are two or more dynamic imports between the public-facing code and the server-only module, the illegal import will not be detected the first time the code is loaded. \ No newline at end of file +This feature also works with dynamic imports, even interpolated ones like ``await import(`./${foo}.js`)``, with one small caveat: during development, if there are two or more dynamic imports between the public-facing code and the server-only module, the illegal import will not be detected the first time the code is loaded. + +> Unit testing frameworks like Vitest do not distinguish between server-only and public-facing code. For this reason, illegal import detection is disabled when running tests, as determined by `process.env.TEST === 'true'`. \ No newline at end of file diff --git a/packages/kit/src/exports/vite/index.js b/packages/kit/src/exports/vite/index.js index 0acd4dabd2f8..03956dd3b2d2 100644 --- a/packages/kit/src/exports/vite/index.js +++ b/packages/kit/src/exports/vite/index.js @@ -301,7 +301,7 @@ function kit({ svelte_config }) { }, async load(id, options) { - if (options?.ssr === false) { + if (options?.ssr === false && process.env.TEST !== 'true') { const normalized_cwd = vite.normalizePath(cwd); const normalized_lib = vite.normalizePath(svelte_config.kit.files.lib); if (