Skip to content

Commit ac082cf

Browse files
authored
[fix] disable illegal import detection when running tests (#8365)
Fixes #8180
1 parent e6374c1 commit ac082cf

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

.changeset/big-cobras-sip.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
Disable illegal import detection when running unit tests

documentation/docs/30-advanced/50-server-only-modules.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,6 @@ Cannot import $lib/server/secrets.js into public-facing code:
5151

5252
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.
5353

54-
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.
54+
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.
55+
56+
> 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'`.

packages/kit/src/exports/vite/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ function kit({ svelte_config }) {
301301
},
302302

303303
async load(id, options) {
304-
if (options?.ssr === false) {
304+
if (options?.ssr === false && process.env.TEST !== 'true') {
305305
const normalized_cwd = vite.normalizePath(cwd);
306306
const normalized_lib = vite.normalizePath(svelte_config.kit.files.lib);
307307
if (

0 commit comments

Comments
 (0)