import { readFile } from "node:fs/promises"; import { describe, expect, it } from "vitest"; import { judgeTypeFixture, listTypeFixtures, parseTypeFixtureExpectation, } from "../../scripts/check-type-fixtures.ts"; import { ownInputFiles, typecheckProjects } from "../../scripts/check-typecheck-inputs.ts"; describe("negative type fixtures (FE-GATE-003)", () => { it("reads the expectation from the first line only", () => { expect(parseTypeFixtureExpectation("// expect: TS2322\nconst a = 1;")).toEqual({ kind: "fail", code: "TS2322" }); expect(parseTypeFixtureExpectation("// expect: pass\n")).toEqual({ kind: "pass" }); expect(parseTypeFixtureExpectation("import x from './x';\n// expect: TS2322\n")).toBeUndefined(); expect(parseTypeFixtureExpectation("// expect: 2322\n")).toBeUndefined(); }); it("states an expectation in every fixture", async () => { const fixtures = await listTypeFixtures(process.cwd()); expect(fixtures.length).toBeGreaterThan(0); for (const fixture of fixtures) { expect(parseTypeFixtureExpectation(await readFile(fixture, "utf8")), fixture).toBeDefined(); } }); // run-ci-gate.ts applied the same test to each of the 23 commands this // runner replaced: exit 1 and the registered `error TS:` in the output. it("passes a failing fixture only on exit 1 with the expected code", () => { const fail = { kind: "fail", code: "TS2322" } as const; const output = "a.ts(1,7): error TS2322: Type 'number' is not assignable to type 'string'.\n"; expect(judgeTypeFixture(fail, 1, output)).toEqual({ passed: true, evidence: ["a.ts(1,7): error TS2322: Type 'number' is not assignable to type 'string'."], }); expect(judgeTypeFixture(fail, 1, "a.ts(1,7): error TS2339: Property 'x' does not exist.\n").passed).toBe(false); expect(judgeTypeFixture(fail, 0, "").passed).toBe(false); expect(judgeTypeFixture(fail, 2, output).passed).toBe(false); }); // 07-05. A port type that widens leaves an `@ts-expect-error` unused (TS2578). it("fails a fixture expected to compile when tsc reports anything", () => { expect(judgeTypeFixture({ kind: "pass" }, 0, "").passed).toBe(true); expect( judgeTypeFixture({ kind: "pass" }, 1, "a.ts(20,1): error TS2578: Unused '@ts-expect-error' directive.\n"), ).toEqual({ passed: false, evidence: ["a.ts(20,1): error TS2578: Unused '@ts-expect-error' directive."] }); }); }); describe("typecheck inputs (R8)", () => { it("finds every tsconfig a package script hands to tsc", () => { expect( typecheckProjects({ app: "node gen.ts && tsc --project tsconfig.app.json", sw: "tsc -p tsconfig.sw.json", lint: "eslint .", }), ).toEqual(["tsconfig.app.json", "tsconfig.sw.json"]); }); it("counts only this repository's files", () => { expect( ownInputFiles( "/repo/node_modules/typescript/lib/lib.es5.d.ts\n/repo/node_modules/@types/node/index.d.ts\n/repo/src/app.ts\n", ), ).toEqual(["/repo/src/app.ts"]); expect(ownInputFiles("/repo/node_modules/typescript/lib/lib.es5.d.ts\n")).toEqual([]); }); });