Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off - Runs the real Knip CLI against a disposable on-disk project.
2import * as NodeChildProcess from "node:child_process";
3import * as NodeFS from "node:fs";
4import * as NodePath from "node:path";
5import * as NodeProcess from "node:process";
6import { expect, it } from "vite-plus/test";
7
8it("agrees with an uncached run after a new file takes over an import or an imported type changes", () => {
9 // Keeping the disposable project here gives it the same Effect installation as the scripts.
10 const cwd = NodeFS.mkdtempSync(NodePath.join(import.meta.dirname, ".knip-test-"));
11 const write = (file: string, content: string) => {
12 NodeFS.mkdirSync(NodePath.dirname(NodePath.join(cwd, file)), { recursive: true });
13 NodeFS.writeFileSync(NodePath.join(cwd, file), content);
14 };
15 const run = () => {
16 const result = NodeChildProcess.spawnSync(
17 NodeProcess.execPath,
18 [NodePath.join(import.meta.dirname, "knip-check.ts"), "--cwd", cwd],
19 { encoding: "utf8" },
20 );
21 return { status: result.status, output: result.stdout + result.stderr };
22 };
23 const uncached = () => {
24 NodeFS.rmSync(NodePath.join(cwd, "node_modules"), { recursive: true, force: true });
25 return run();
26 };
27 try {
28 write("package.json", JSON.stringify({ private: true, workspaces: ["apps/*"] }));
29 write(
30 "knip.json",
31 JSON.stringify({
32 // As in knip.jsonc.
33 rules: { types: "off", nsTypes: "off" },
34 workspaces: { "apps/web": { entry: ["main.ts"], project: ["**/*.ts"] } },
35 }),
36 );
37 write(
38 "apps/web/package.json",
39 JSON.stringify({ name: "web", type: "module", dependencies: { effect: "*" } }),
40 );
41 write(
42 "apps/web/tsconfig.json",
43 JSON.stringify({ compilerOptions: { module: "ESNext", moduleResolution: "Bundler" } }),
44 );
45 write(
46 "apps/web/main.ts",
47 `
48 import "./entry";
49 const kept: import("./shape/index").Shape | undefined = undefined;
50 console.log(kept);
51 `,
52 );
53 write("apps/web/entry.ts", `export const Value = null as unknown as import("./shape").Shape;`);
54 write(
55 "apps/web/shape/index.ts",
56 `import * as S from "effect/Schema"; export type Shape = typeof S.String;`,
57 );
58 const first = run();
59 expect(first.status, first.output).toBe(0);
60 // A second run answers from both caches.
61 expect(run()).toEqual(first);
62
63 // "./shape" now resolves here; entry.ts and shape/index.ts are untouched.
64 write("apps/web/shape.ts", `export type Shape = { id: number };`);
65 const takenOver = run();
66 expect(takenOver.status, takenOver.output).toBe(1);
67 expect(takenOver.output).toContain("Value");
68 expect(takenOver.output).not.toContain("shape.ts");
69 expect(takenOver).toEqual(uncached());
70
71 // Only a file that entry.ts imports changes.
72 run();
73 write(
74 "apps/web/shape.ts",
75 `import * as S from "effect/Schema"; export type Shape = typeof S.Number;`,
76 );
77 const schemaAgain = run();
78 expect(schemaAgain.status, schemaAgain.output).toBe(0);
79 expect(schemaAgain).toEqual(uncached());
80 } finally {
81 NodeFS.rmSync(cwd, { recursive: true, force: true });
82 }
83});