From f5ea3ad8d9f5201b8d498d0f4ca0712d2019cfef Mon Sep 17 00:00:00 2001 From: 7nik Date: Thu, 25 Sep 2025 14:49:49 +0300 Subject: [PATCH 1/2] allow testing in production env --- .../samples/production/_config.js | 5 ++ .../samples/production/main.svelte | 7 ++ packages/svelte/tests/suite.ts | 70 +++++++++++++++++-- 3 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 packages/svelte/tests/runtime-runes/samples/production/_config.js create mode 100644 packages/svelte/tests/runtime-runes/samples/production/main.svelte diff --git a/packages/svelte/tests/runtime-runes/samples/production/_config.js b/packages/svelte/tests/runtime-runes/samples/production/_config.js new file mode 100644 index 000000000000..ce36c9d95b06 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/production/_config.js @@ -0,0 +1,5 @@ +import { test } from '../../test'; + +export default test({ + production: true +}); diff --git a/packages/svelte/tests/runtime-runes/samples/production/main.svelte b/packages/svelte/tests/runtime-runes/samples/production/main.svelte new file mode 100644 index 000000000000..af1a8cd55ee3 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/production/main.svelte @@ -0,0 +1,7 @@ + \ No newline at end of file diff --git a/packages/svelte/tests/suite.ts b/packages/svelte/tests/suite.ts index bbd252b8e188..cbfab608ee20 100644 --- a/packages/svelte/tests/suite.ts +++ b/packages/svelte/tests/suite.ts @@ -1,9 +1,11 @@ import fs from 'node:fs'; -import { it } from 'vitest'; +import { it, vi } from 'vitest'; export interface BaseTest { skip?: boolean; solo?: boolean; + /** Set `DEV` to `false` */ + production?: boolean; } /** @@ -27,11 +29,33 @@ export function suite(fn: (config: Test, test_dir: string return { test: (config: Test) => config, run: async (cwd: string, samples_dir = 'samples') => { + const production_tests: Array<[Function, string, Function]> = []; + await for_each_dir(cwd, samples_dir, (config, dir) => { let it_fn = config.skip ? it.skip : config.solo ? it.only : it; - it_fn(dir, () => fn(config, `${cwd}/${samples_dir}/${dir}`)); + if (config.production) { + production_tests.push([it_fn, dir, () => fn(config, `${cwd}/${samples_dir}/${dir}`)]); + } else { + it_fn(dir, () => fn(config, `${cwd}/${samples_dir}/${dir}`)); + } }); + + if (production_tests.length) { + let mocked = false; + for (const [it, name, test] of production_tests) { + it(name, () => { + if (!mocked) { + vi.doMock('esm-env', async (importEnv) => ({ + ...(await importEnv()), + DEV: false + })); + mocked = true; + } + return test(); + }); + } + } } }; } @@ -45,6 +69,8 @@ export function suite_with_variants config, run: async (cwd: string, samples_dir = 'samples') => { + const production_tests: Array<[Function, string, Function]> = []; + await for_each_dir(cwd, samples_dir, (config, dir) => { let called_common = false; let common: any = undefined; @@ -57,15 +83,37 @@ export function suite_with_variants { + const test = async () => { if (!called_common) { called_common = true; common = await common_setup(config, `${cwd}/${samples_dir}/${dir}`); } return fn(config, `${cwd}/${samples_dir}/${dir}`, variant, common); - }); + }; + + if (config.production) { + production_tests.push([it_fn, `${dir} (${variant})`, test]); + } else { + it_fn(`${dir} (${variant})`, test); + } } }); + + if (production_tests.length) { + let mocked = false; + for (const [it, name, test] of production_tests) { + it(name, () => { + if (!mocked) { + vi.doMock('esm-env', async (importEnv) => ({ + ...(await importEnv()), + DEV: false + })); + mocked = true; + } + return test(); + }); + } + } } }; } @@ -108,3 +156,17 @@ export function assert_ok(value: any): asserts value { throw new Error(`Expected truthy value, got ${value}`); } } + +function run_in_production(fn: (...args: any[]) => void | Promise) { + return async (...args: any[]) => { + vi.doMock('esm-env', async (importEnv) => ({ + ...(await importEnv()), + DEV: false + })); + try { + await fn(...args); + } finally { + vi.doUnmock('esm-env'); + } + }; +} From 872fa3a66cfc634dc392118fd247a4d7e0c0c747 Mon Sep 17 00:00:00 2001 From: 7nik Date: Fri, 26 Sep 2025 21:48:55 +0300 Subject: [PATCH 2/2] oops --- .../samples/production/main.svelte | 2 +- packages/svelte/tests/suite.ts | 52 +++++++++---------- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/packages/svelte/tests/runtime-runes/samples/production/main.svelte b/packages/svelte/tests/runtime-runes/samples/production/main.svelte index af1a8cd55ee3..d9cff745d15a 100644 --- a/packages/svelte/tests/runtime-runes/samples/production/main.svelte +++ b/packages/svelte/tests/runtime-runes/samples/production/main.svelte @@ -1,6 +1,6 @@