|
| 1 | +import { createRequire } from "node:module"; |
| 2 | +import { mkdtempSync, mkdirSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { dirname, join } from "node:path"; |
| 5 | +import { afterEach, describe, expect, it } from "vitest"; |
| 6 | +import { loadTypescript } from "./loadTypescript.js"; |
| 7 | + |
| 8 | +const packageRequire = createRequire(join(process.cwd(), "package.json")); |
| 9 | +const projectDirs = new Set<string>(); |
| 10 | + |
| 11 | +function createProject(packages: Record<string, string>) { |
| 12 | + const projectDir = mkdtempSync(join(tmpdir(), "trigger-typescript-")); |
| 13 | + projectDirs.add(projectDir); |
| 14 | + const nodeModulesDir = join(projectDir, "node_modules"); |
| 15 | + |
| 16 | + mkdirSync(nodeModulesDir); |
| 17 | + writeFileSync(join(projectDir, "package.json"), JSON.stringify({ private: true })); |
| 18 | + |
| 19 | + for (const [installedName, sourceName] of Object.entries(packages)) { |
| 20 | + const target = dirname(packageRequire.resolve(`${sourceName}/package.json`)); |
| 21 | + const destination = join(nodeModulesDir, installedName); |
| 22 | + |
| 23 | + mkdirSync(dirname(destination), { recursive: true }); |
| 24 | + symlinkSync(target, destination, "junction"); |
| 25 | + } |
| 26 | + |
| 27 | + return projectDir; |
| 28 | +} |
| 29 | + |
| 30 | +describe("loadTypescript", () => { |
| 31 | + afterEach(() => { |
| 32 | + for (const projectDir of projectDirs) { |
| 33 | + rmSync(projectDir, { recursive: true, force: true }); |
| 34 | + } |
| 35 | + |
| 36 | + projectDirs.clear(); |
| 37 | + }); |
| 38 | + |
| 39 | + it("loads the consumer's TypeScript 5 compiler", () => { |
| 40 | + const compiler = loadTypescript(createProject({ typescript: "typescript5" })); |
| 41 | + |
| 42 | + expect(compiler.version).toBe("5.9.3"); |
| 43 | + expect(typeof compiler.transpileModule).toBe("function"); |
| 44 | + }); |
| 45 | + |
| 46 | + it("loads the consumer's TypeScript 6 compiler", () => { |
| 47 | + const compiler = loadTypescript(createProject({ typescript: "typescript" })); |
| 48 | + |
| 49 | + expect(compiler.version).toBe("6.0.3"); |
| 50 | + expect(typeof compiler.transpileModule).toBe("function"); |
| 51 | + }); |
| 52 | + |
| 53 | + it("returns an actionable error for TypeScript 7 without the compatibility package", () => { |
| 54 | + const projectDir = createProject({ typescript: "typescript7" }); |
| 55 | + const requireFromProject = createRequire(join(projectDir, "package.json")); |
| 56 | + |
| 57 | + expect(typeof requireFromProject("typescript").transpileModule).toBe("undefined"); |
| 58 | + expect(() => loadTypescript(projectDir, ["typescript"])).toThrowError( |
| 59 | + expect.objectContaining({ |
| 60 | + message: expect.stringContaining("npm install --save-dev @typescript/typescript6"), |
| 61 | + }) |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + it("surfaces errors from an installed compiler package", () => { |
| 66 | + const projectDir = createProject({}); |
| 67 | + const packageDir = join(projectDir, "node_modules", "typescript"); |
| 68 | + |
| 69 | + mkdirSync(packageDir); |
| 70 | + writeFileSync( |
| 71 | + join(packageDir, "package.json"), |
| 72 | + JSON.stringify({ name: "typescript", main: "index.cjs" }) |
| 73 | + ); |
| 74 | + writeFileSync(join(packageDir, "index.cjs"), 'throw new Error("broken compiler");'); |
| 75 | + |
| 76 | + expect(() => loadTypescript(projectDir)).toThrowError( |
| 77 | + `Failed to load "typescript" from ${projectDir}.` |
| 78 | + ); |
| 79 | + }); |
| 80 | + |
| 81 | + it("falls back to the TypeScript 6 compatibility package for TypeScript 7", () => { |
| 82 | + const compiler = loadTypescript( |
| 83 | + createProject({ |
| 84 | + typescript: "typescript7", |
| 85 | + "@typescript/typescript6": "@typescript/typescript6", |
| 86 | + }) |
| 87 | + ); |
| 88 | + |
| 89 | + const output = compiler.transpileModule( |
| 90 | + ` |
| 91 | + class Dependency {} |
| 92 | + function injectable<T extends new (...args: any[]) => object>(target: T) {} |
| 93 | +
|
| 94 | + @injectable |
| 95 | + class Service { |
| 96 | + constructor(public dependency: Dependency) {} |
| 97 | + } |
| 98 | + `, |
| 99 | + { |
| 100 | + compilerOptions: { |
| 101 | + experimentalDecorators: true, |
| 102 | + emitDecoratorMetadata: true, |
| 103 | + }, |
| 104 | + } |
| 105 | + ).outputText; |
| 106 | + |
| 107 | + expect(compiler.version).toBe("6.0.3"); |
| 108 | + expect(output).toContain('__metadata("design:paramtypes", [Dependency])'); |
| 109 | + }); |
| 110 | + |
| 111 | + it("supports aliasing TypeScript to the compatibility package", () => { |
| 112 | + const compiler = loadTypescript(createProject({ typescript: "@typescript/typescript6" })); |
| 113 | + |
| 114 | + expect(compiler.version).toBe("6.0.3"); |
| 115 | + expect(typeof compiler.transpileModule).toBe("function"); |
| 116 | + }); |
| 117 | +}); |
0 commit comments