|
| 1 | +import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { afterEach, describe, expect, it } from "vitest"; |
| 5 | +import { loadConfig } from "./config.js"; |
| 6 | + |
| 7 | +const projectDirs: string[] = []; |
| 8 | + |
| 9 | +afterEach(async () => { |
| 10 | + await Promise.all(projectDirs.splice(0).map((dir) => rm(dir, { recursive: true, force: true }))); |
| 11 | +}); |
| 12 | + |
| 13 | +async function createProject(runtime?: string) { |
| 14 | + const cwd = await mkdtemp(join(tmpdir(), "trigger-runtime-config-")); |
| 15 | + projectDirs.push(cwd); |
| 16 | + |
| 17 | + await mkdir(join(cwd, "trigger")); |
| 18 | + await writeFile(join(cwd, "package.json"), JSON.stringify({ name: "runtime-config-test" })); |
| 19 | + await writeFile(join(cwd, "pnpm-lock.yaml"), "lockfileVersion: '9.0'\n"); |
| 20 | + await writeFile( |
| 21 | + join(cwd, "trigger.config.ts"), |
| 22 | + `export default { |
| 23 | + project: "proj_runtime_config_test", |
| 24 | + maxDuration: 60, |
| 25 | + dirs: ["./trigger"], |
| 26 | + ${runtime === undefined ? "" : `runtime: ${JSON.stringify(runtime)},`} |
| 27 | +}; |
| 28 | +` |
| 29 | + ); |
| 30 | + |
| 31 | + return cwd; |
| 32 | +} |
| 33 | + |
| 34 | +describe("loadConfig runtime", () => { |
| 35 | + it.each([ |
| 36 | + ["experimental-node-24", "node-24"], |
| 37 | + ["experimental-node-26", "node-26"], |
| 38 | + ["node-24", "node-24"], |
| 39 | + ["node-26", "node-26"], |
| 40 | + ] as const)("normalizes %s before returning the resolved config", async (runtime, expected) => { |
| 41 | + const cwd = await createProject(runtime); |
| 42 | + |
| 43 | + await expect(loadConfig({ cwd, warn: false })).resolves.toMatchObject({ runtime: expected }); |
| 44 | + }); |
| 45 | + |
| 46 | + it("keeps node as the default", async () => { |
| 47 | + const cwd = await createProject(); |
| 48 | + |
| 49 | + await expect(loadConfig({ cwd, warn: false })).resolves.toMatchObject({ runtime: "node" }); |
| 50 | + }); |
| 51 | + |
| 52 | + it("rejects unsupported runtimes while loading config", async () => { |
| 53 | + const cwd = await createProject("node-23"); |
| 54 | + |
| 55 | + await expect(loadConfig({ cwd, warn: false })).rejects.toThrowError( |
| 56 | + /Unsupported runtime "node-23" in trigger\.config/ |
| 57 | + ); |
| 58 | + }); |
| 59 | +}); |
0 commit comments