pull/16840/merge
7nik 2 days ago committed by GitHub
commit adeb165290
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

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

@ -0,0 +1,7 @@
<script>
import { DEV } from "esm-env";
if (DEV) {
throw new Error("Dev-only code must not run");
}
</script>

@ -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<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();
});
}
}
};
}
@ -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;
@ -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();
});
}
}
};
}
@ -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…
Cancel
Save