tobogganSe connecter

library

Prebuilt pebble components: drop-in custom elements, no compiler needed

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

Dernier runAucun
0Étoiles
0Forks
2Issues
69 lignes · 3059 octetsBrut · Historique
1import { afterEach, expect, test } from "bun:test";
2import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
3import { tmpdir } from "node:os";
4import { dirname, join } from "node:path";
5import { shipped } from "../scripts/declarations";
6
7let root = "";
8
9/** A types/ tree from { "pkg/file.d.ts": "contents" }. */
10function 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
19afterEach(() => rmSync(root, { recursive: true, force: true }));
20
21test("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
34test("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
43test("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
49test("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
57test("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
62test("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});