Skip to content

Commit 49f43de

Browse files
committed
test(config): check settings import laziness in a fresh process
1 parent 596e138 commit 49f43de

2 files changed

Lines changed: 27 additions & 16 deletions

File tree

‎scripts/settings-lazy-probe.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { schemaMaterializations } from "../src/schema/lazy.js";
2+
3+
const before = schemaMaterializations();
4+
await import("../src/config/settings.js");
5+
process.stdout.write(
6+
JSON.stringify({ before, afterImport: schemaMaterializations() }),
7+
);

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

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
import { mkdtemp, rm, writeFile } from "node:fs/promises";
2-
import { tmpdir } from "node:os";
31
import { join } from "node:path";
42
import { expect, test } from "bun:test";
53

6-
import { schemaMaterializations } from "../schema/lazy.js";
4+
const settingsLazinessProbe = join(
5+
import.meta.dirname,
6+
"../../scripts/settings-lazy-probe.ts",
7+
);
78

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);
9+
test("importing settings does not build schemas", async () => {
10+
const child = Bun.spawn([process.execPath, settingsLazinessProbe], {
11+
stdout: "pipe",
12+
stderr: "pipe",
13+
});
14+
const [exitCode, stdout, stderr] = await Promise.all([
15+
child.exited,
16+
new Response(child.stdout).text(),
17+
new Response(child.stderr).text(),
18+
]);
1219

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-
}
20+
expect(exitCode, stderr).toBe(0);
21+
const materializations = JSON.parse(stdout) as {
22+
before: number;
23+
afterImport: number;
24+
};
25+
expect(materializations.afterImport).toBe(materializations.before);
2226
});

0 commit comments

Comments
 (0)