|
| 1 | +import fs from "fs" |
| 2 | +import os from "os" |
| 3 | +import path from "path" |
| 4 | + |
| 5 | +import { getEsbuildScriptPath, runEsbuild } from "../esbuild-runner.js" |
| 6 | + |
| 7 | +describe("getEsbuildScriptPath", () => { |
| 8 | + it("should find esbuild-wasm script in node_modules in development", () => { |
| 9 | + const scriptPath = getEsbuildScriptPath() |
| 10 | + |
| 11 | + // Should find the script. |
| 12 | + expect(typeof scriptPath).toBe("string") |
| 13 | + expect(scriptPath.length).toBeGreaterThan(0) |
| 14 | + |
| 15 | + // The script should exist. |
| 16 | + expect(fs.existsSync(scriptPath)).toBe(true) |
| 17 | + |
| 18 | + // Should be the esbuild script (not a binary). |
| 19 | + expect(scriptPath).toMatch(/esbuild$/) |
| 20 | + }) |
| 21 | + |
| 22 | + it("should prefer production path when extensionPath is provided and script exists", () => { |
| 23 | + // Create a temporary directory with a fake script. |
| 24 | + const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "esbuild-runner-test-")) |
| 25 | + const binDir = path.join(tempDir, "dist", "bin") |
| 26 | + fs.mkdirSync(binDir, { recursive: true }) |
| 27 | + |
| 28 | + const fakeScriptPath = path.join(binDir, "esbuild") |
| 29 | + fs.writeFileSync(fakeScriptPath, "#!/usr/bin/env node\nconsole.log('fake esbuild')") |
| 30 | + |
| 31 | + try { |
| 32 | + const result = getEsbuildScriptPath(tempDir) |
| 33 | + expect(result).toBe(fakeScriptPath) |
| 34 | + } finally { |
| 35 | + fs.rmSync(tempDir, { recursive: true, force: true }) |
| 36 | + } |
| 37 | + }) |
| 38 | + |
| 39 | + it("should fall back to node_modules when production script does not exist", () => { |
| 40 | + // Pass a non-existent extension path. |
| 41 | + const result = getEsbuildScriptPath("/nonexistent/extension/path") |
| 42 | + |
| 43 | + // Should fall back to development path. |
| 44 | + expect(typeof result).toBe("string") |
| 45 | + expect(result.length).toBeGreaterThan(0) |
| 46 | + expect(fs.existsSync(result)).toBe(true) |
| 47 | + }) |
| 48 | +}) |
| 49 | + |
| 50 | +describe("runEsbuild", () => { |
| 51 | + let tempDir: string |
| 52 | + |
| 53 | + beforeEach(() => { |
| 54 | + tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "esbuild-runner-test-")) |
| 55 | + }) |
| 56 | + |
| 57 | + afterEach(() => { |
| 58 | + fs.rmSync(tempDir, { recursive: true, force: true }) |
| 59 | + }) |
| 60 | + |
| 61 | + it("should compile a TypeScript file to ESM", async () => { |
| 62 | + // Create a simple TypeScript file. |
| 63 | + const inputFile = path.join(tempDir, "input.ts") |
| 64 | + const outputFile = path.join(tempDir, "output.mjs") |
| 65 | + |
| 66 | + fs.writeFileSync( |
| 67 | + inputFile, |
| 68 | + ` |
| 69 | + export const greeting = "Hello, World!" |
| 70 | + export function add(a: number, b: number): number { |
| 71 | + return a + b |
| 72 | + } |
| 73 | + `, |
| 74 | + ) |
| 75 | + |
| 76 | + await runEsbuild({ |
| 77 | + entryPoint: inputFile, |
| 78 | + outfile: outputFile, |
| 79 | + format: "esm", |
| 80 | + platform: "node", |
| 81 | + target: "node18", |
| 82 | + bundle: true, |
| 83 | + }) |
| 84 | + |
| 85 | + // Verify output file exists. |
| 86 | + expect(fs.existsSync(outputFile)).toBe(true) |
| 87 | + |
| 88 | + // Verify output content is valid JavaScript. |
| 89 | + const outputContent = fs.readFileSync(outputFile, "utf-8") |
| 90 | + expect(outputContent).toContain("Hello, World!") |
| 91 | + expect(outputContent).toContain("add") |
| 92 | + }, 30000) |
| 93 | + |
| 94 | + it("should generate inline source maps when specified", async () => { |
| 95 | + const inputFile = path.join(tempDir, "input.ts") |
| 96 | + const outputFile = path.join(tempDir, "output.mjs") |
| 97 | + |
| 98 | + fs.writeFileSync(inputFile, `export const value = 42`) |
| 99 | + |
| 100 | + await runEsbuild({ entryPoint: inputFile, outfile: outputFile, format: "esm", sourcemap: "inline" }) |
| 101 | + |
| 102 | + const outputContent = fs.readFileSync(outputFile, "utf-8") |
| 103 | + expect(outputContent).toContain("sourceMappingURL=data:") |
| 104 | + }, 30000) |
| 105 | + |
| 106 | + it("should throw an error for invalid TypeScript", async () => { |
| 107 | + const inputFile = path.join(tempDir, "invalid.ts") |
| 108 | + const outputFile = path.join(tempDir, "output.mjs") |
| 109 | + |
| 110 | + // Write syntactically invalid TypeScript. |
| 111 | + fs.writeFileSync(inputFile, `export const value = {{{ invalid syntax`) |
| 112 | + |
| 113 | + await expect(runEsbuild({ entryPoint: inputFile, outfile: outputFile, format: "esm" })).rejects.toThrow() |
| 114 | + }, 30000) |
| 115 | + |
| 116 | + it("should throw an error for non-existent file", async () => { |
| 117 | + const nonExistentFile = path.join(tempDir, "does-not-exist.ts") |
| 118 | + const outputFile = path.join(tempDir, "output.mjs") |
| 119 | + |
| 120 | + await expect(runEsbuild({ entryPoint: nonExistentFile, outfile: outputFile, format: "esm" })).rejects.toThrow() |
| 121 | + }, 30000) |
| 122 | + |
| 123 | + it("should bundle dependencies when bundle option is true", async () => { |
| 124 | + // Create two files where one imports the other. |
| 125 | + const libFile = path.join(tempDir, "lib.ts") |
| 126 | + const mainFile = path.join(tempDir, "main.ts") |
| 127 | + const outputFile = path.join(tempDir, "output.mjs") |
| 128 | + |
| 129 | + fs.writeFileSync(libFile, `export const PI = 3.14159`) |
| 130 | + fs.writeFileSync( |
| 131 | + mainFile, |
| 132 | + ` |
| 133 | + import { PI } from "./lib.js" |
| 134 | + export const circumference = (r: number) => 2 * PI * r |
| 135 | + `, |
| 136 | + ) |
| 137 | + |
| 138 | + await runEsbuild({ entryPoint: mainFile, outfile: outputFile, format: "esm", bundle: true }) |
| 139 | + |
| 140 | + const outputContent = fs.readFileSync(outputFile, "utf-8") |
| 141 | + // The PI constant should be bundled inline. |
| 142 | + expect(outputContent).toContain("3.14159") |
| 143 | + }, 30000) |
| 144 | + |
| 145 | + it("should respect platform option", async () => { |
| 146 | + const inputFile = path.join(tempDir, "input.ts") |
| 147 | + const outputFile = path.join(tempDir, "output.mjs") |
| 148 | + |
| 149 | + fs.writeFileSync(inputFile, `export const value = process.env.NODE_ENV`) |
| 150 | + |
| 151 | + await runEsbuild({ entryPoint: inputFile, outfile: outputFile, format: "esm", platform: "node" }) |
| 152 | + |
| 153 | + // File should be created successfully. |
| 154 | + expect(fs.existsSync(outputFile)).toBe(true) |
| 155 | + }, 30000) |
| 156 | +}) |
0 commit comments