Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/big-cobras-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Disable illegal import detection when running unit tests
4 changes: 3 additions & 1 deletion documentation/docs/30-advanced/50-server-only-modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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'`.
2 changes: 1 addition & 1 deletion packages/kit/src/exports/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down