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..d9cff745d15a --- /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..72c40f526bc1 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,31 @@ 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}`)); + } }); + + 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 +67,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 +81,35 @@ 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); + } } }); + + 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 +152,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'); + } + }; +}