| @@ -0,0 +1,69 @@ |
| 1 | +import { afterEach, expect, test } from "bun:test"; |
| 2 | +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { dirname, join } from "node:path"; |
| 5 | +import { shipped } from "../scripts/declarations"; |
| 6 | + |
| 7 | +let root = ""; |
| 8 | + |
| 9 | +/** A types/ tree from { "pkg/file.d.ts": "contents" }. */ |
| 10 | +function tree(files: Record<string, string>): string { |
| 11 | + root = mkdtempSync(join(tmpdir(), "declarations-")); |
| 12 | + for (const [path, contents] of Object.entries(files)) { |
| 13 | + mkdirSync(dirname(join(root, path)), { recursive: true }); |
| 14 | + writeFileSync(join(root, path), contents); |
| 15 | + } |
| 16 | + return root; |
| 17 | +} |
| 18 | + |
| 19 | +afterEach(() => rmSync(root, { recursive: true, force: true })); |
| 20 | + |
| 21 | +test("ships what index reaches, through every import form, and nothing else", () => { |
| 22 | + const types = tree({ |
| 23 | + "tablet/index.d.ts": `export { a } from "./a";\nexport type { B } from "./engine";\nimport "./side";`, |
| 24 | + "tablet/a.d.ts": `export type A = import("./deep").Deep;`, |
| 25 | + "tablet/deep.d.ts": `export type Deep = 1;`, |
| 26 | + "tablet/engine/index.d.ts": `export type B = 2;`, |
| 27 | + "tablet/side.d.ts": `export {};`, |
| 28 | + "tablet/dev.d.ts": `export declare const DEV: boolean;`, |
| 29 | + "toaster/index.d.ts": `export {};`, |
| 30 | + }); |
| 31 | + expect(shipped(types, "tablet", [])).toEqual(["a.d.ts", "deep.d.ts", "engine/index.d.ts", "index.d.ts", "side.d.ts"]); |
| 32 | +}); |
| 33 | + |
| 34 | +test("an unreached module's bare import does not ship, so it does not count", () => { |
| 35 | + const types = tree({ |
| 36 | + "toaster/index.d.ts": `export { announceToast } from "./announce";`, |
| 37 | + "toaster/announce.d.ts": `export declare function announceToast(): void;`, |
| 38 | + "toaster/lifetime.d.ts": `import { type Source } from "@pebble/pebble";`, |
| 39 | + }); |
| 40 | + expect(shipped(types, "toaster", [])).toEqual(["announce.d.ts", "index.d.ts"]); |
| 41 | +}); |
| 42 | + |
| 43 | +test("a reached bare import must be a declared dependency", () => { |
| 44 | + const types = tree({ "toaster/index.d.ts": `import { type Source } from "@pebble/pebble/rxcore";` }); |
| 45 | + expect(() => shipped(types, "toaster", [])).toThrow("@pebble/pebble is not a dependency"); |
| 46 | + expect(shipped(types, "toaster", ["@pebble/pebble"])).toEqual(["index.d.ts"]); |
| 47 | +}); |
| 48 | + |
| 49 | +test("a relative import out of the package fails", () => { |
| 50 | + const types = tree({ |
| 51 | + "tablet/index.d.ts": `export * from "../toaster/index";`, |
| 52 | + "toaster/index.d.ts": `export {};`, |
| 53 | + }); |
| 54 | + expect(() => shipped(types, "tablet", [])).toThrow("outside types/tablet/"); |
| 55 | +}); |
| 56 | + |
| 57 | +test("an import with no declaration fails", () => { |
| 58 | + const types = tree({ "tablet/index.d.ts": `export * from "./gone";` }); |
| 59 | + expect(() => shipped(types, "tablet", [])).toThrow(`"./gone", which has no declaration`); |
| 60 | +}); |
| 61 | + |
| 62 | +test("the library's own packages ship standalone declarations", () => { |
| 63 | + const built = Bun.spawnSync(["bunx", "tsc", "-p", "tsconfig.build.json", "--outDir", join(tree({}), "types")], { |
| 64 | + cwd: `${import.meta.dir}/..`, |
| 65 | + }); |
| 66 | + expect(built.exitCode).toBe(0); |
| 67 | + expect(shipped(join(root, "types"), "tablet", [])).not.toContain("dev.d.ts"); |
| 68 | + expect(shipped(join(root, "types"), "toaster", [])).not.toContain("lifetime.d.ts"); |
| 69 | +}); |