chore: allow testing in production env 2 (#17590)

* Revert "chore: allow testing in production env (#16840)"

This reverts commit ffd65e90fe.

* new approach
fix-15339
7nik 6 days ago
parent dd67655697
commit 2339a739ec

@ -0,0 +1,5 @@
<script>
let { foo = $bindable(42) } = $props();
</script>
{foo};

@ -0,0 +1,12 @@
import { flushSync } from 'svelte';
import { test } from '../../test';
export default test({
async test({ target }) {
let button = target.querySelector('button');
button?.click();
flushSync();
}
});

@ -0,0 +1,19 @@
<script>
import Child from "./Child.svelte";
let foo;
function onerror(err) {
// re-throw if it isn't the production error
// do in a such way because config.error is checked via `includes`
if (err.message !== 'https://svelte.dev/e/props_invalid_value') {
throw err;
}
}
</script>
<svelte:boundary {onerror}>
<Child bind:foo />
</svelte:boundary>

@ -0,0 +1,17 @@
// @vitest-environment jsdom
import { vi } from 'vitest';
import { runtime_suite, ok } from '../runtime-legacy/shared';
vi.mock('esm-env', async (importEnv) => {
return {
...(await importEnv()),
DEV: false
};
});
const { test, run } = runtime_suite(true);
export { test, ok };
await run(__dirname);

@ -1,5 +0,0 @@
import { test } from '../../test';
export default test({
production: true
});

@ -1,11 +1,9 @@
import fs from 'node:fs';
import { it, vi } from 'vitest';
import { it } from 'vitest';
export interface BaseTest {
skip?: boolean;
solo?: boolean;
/** Set `DEV` to `false` */
production?: boolean;
}
/**
@ -29,31 +27,11 @@ export function suite<Test extends BaseTest>(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<Test>(cwd, samples_dir, (config, dir) => {
let it_fn = config.skip ? it.skip : config.solo ? it.only : it;
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}`));
}
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();
});
}
}
};
}
@ -67,8 +45,6 @@ export function suite_with_variants<Test extends BaseTest, Variants extends stri
return {
test: (config: Test) => config,
run: async (cwd: string, samples_dir = 'samples') => {
const production_tests: Array<[Function, string, Function]> = [];
await for_each_dir<Test>(cwd, samples_dir, (config, dir) => {
let called_common = false;
let common: any = undefined;
@ -81,35 +57,15 @@ export function suite_with_variants<Test extends BaseTest, Variants extends stri
const solo = config.solo;
let it_fn = skip ? it.skip : solo ? it.only : it;
const test = async () => {
it_fn(`${dir} (${variant})`, 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();
});
}
}
};
}
@ -152,17 +108,3 @@ 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<void>) {
return async (...args: any[]) => {
vi.doMock('esm-env', async (importEnv) => ({
...(await importEnv()),
DEV: false
}));
try {
await fn(...args);
} finally {
vi.doUnmock('esm-env');
}
};
}

Loading…
Cancel
Save