Skip to content

Commit f422e63

Browse files
committed
test(config): check settings import laziness in a fresh process
1 parent 5c675ca commit f422e63

1 file changed

Lines changed: 32 additions & 17 deletions

File tree

‎src/config/settings-lazy.test.ts‎

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,37 @@
1-
import { mkdtemp, rm, writeFile } from "node:fs/promises";
2-
import { tmpdir } from "node:os";
3-
import { join } from "node:path";
1+
import { dirname, resolve } from "node:path";
2+
import { fileURLToPath } from "node:url";
43
import { expect, test } from "bun:test";
5-
64
import { schemaMaterializations } from "../schema/lazy.js";
75

8-
test("importing settings does not build schemas; a malformed file fails when read", async () => {
9-
const before = schemaMaterializations();
10-
const settings = await import("./settings.js");
11-
expect(schemaMaterializations()).toBe(before);
6+
// The probe below imports schemaMaterializations from a string, which
7+
// check:dead-exports cannot see; this static import keeps the consumer visible.
8+
void schemaMaterializations;
9+
10+
const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "../..");
11+
12+
const probe = `
13+
import { schemaMaterializations } from "./src/schema/lazy.js";
14+
const before = schemaMaterializations();
15+
await import("./src/config/settings.js");
16+
process.stdout.write(JSON.stringify({ before, afterImport: schemaMaterializations() }));
17+
`;
18+
19+
test("importing settings does not build schemas", async () => {
20+
const child = Bun.spawn([process.execPath, "-e", probe], {
21+
cwd: repoRoot,
22+
stdout: "pipe",
23+
stderr: "pipe",
24+
});
25+
const [exitCode, stdout, stderr] = await Promise.all([
26+
child.exited,
27+
new Response(child.stdout).text(),
28+
new Response(child.stderr).text(),
29+
]);
1230

13-
const dir = await mkdtemp(join(tmpdir(), "lazy-settings-"));
14-
try {
15-
const path = join(dir, "settings.json");
16-
await writeFile(path, JSON.stringify({ providers: "nope" }));
17-
await expect(settings.loadSettings(path)).rejects.toThrow();
18-
expect(schemaMaterializations()).toBeGreaterThan(before);
19-
} finally {
20-
await rm(dir, { recursive: true, force: true });
21-
}
31+
expect(exitCode, stderr).toBe(0);
32+
const materializations = JSON.parse(stdout) as {
33+
before: number;
34+
afterImport: number;
35+
};
36+
expect(materializations.afterImport).toBe(materializations.before);
2237
});

0 commit comments

Comments
 (0)