Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { test } from '../../test';

export default test({
production: true
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
import { DEV } from "esm-env";

if (DEV) {
throw new Error("Dev-only code must not run");
}
</script>
66 changes: 62 additions & 4 deletions packages/svelte/tests/suite.ts
Original file line number Diff line number Diff line change
@@ -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;
}

/**
Expand All @@ -27,11 +29,31 @@ 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;

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();
});
}
}
};
}
Expand All @@ -45,6 +67,8 @@ 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;
Expand All @@ -57,15 +81,35 @@ 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;

it_fn(`${dir} (${variant})`, async () => {
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();
});
}
}
};
}
Expand Down Expand Up @@ -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<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