From 1c7fcc639fa9f4d2866f1a0f437998dcc57ca7f5 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 6 Jan 2023 09:57:38 -0500 Subject: [PATCH 1/3] disable illegal import detection when running tests --- .changeset/big-cobras-sip.md | 5 +++++ packages/kit/src/exports/vite/index.js | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 .changeset/big-cobras-sip.md 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/packages/kit/src/exports/vite/index.js b/packages/kit/src/exports/vite/index.js index 0acd4dabd2f8..eba06fab6310 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) { const normalized_cwd = vite.normalizePath(cwd); const normalized_lib = vite.normalizePath(svelte_config.kit.files.lib); if ( From 849b1739923a3bdf5c17e87a0fb94524dadb5bf7 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 6 Jan 2023 10:05:05 -0500 Subject: [PATCH 2/3] robustify --- packages/kit/src/exports/vite/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/exports/vite/index.js b/packages/kit/src/exports/vite/index.js index eba06fab6310..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 && !process.env.TEST) { + 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 ( From e9e128588f2e4da91985f6a4a82eb66a5730b0cb Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 8 Jan 2023 12:21:44 -0500 Subject: [PATCH 3/3] docs --- documentation/docs/30-advanced/50-server-only-modules.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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