refactor(tests): centralize test harness boilerplate into bundle.mjs helper
This commit is contained in:
parent
1729f7c236
commit
43a03272bf
9 changed files with 107 additions and 150 deletions
55
tests/helpers/bundle.mjs
Normal file
55
tests/helpers/bundle.mjs
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
// Shared test-harness bundler.
|
||||
//
|
||||
// Every run-*-test.mjs used to hand-roll the same esbuild boilerplate:
|
||||
// write temp entry (optional) -> esbuild.build(bundle/node/esm/silent)
|
||||
// -> dynamic import of the artifact -> rmSync cleanup.
|
||||
// This module is that sequence, parameterized. Harnesses call it once (or
|
||||
// more, like the chapters harness's three bundles) and get back the loaded
|
||||
// module plus a scoped cleanup function.
|
||||
import esbuild from "esbuild";
|
||||
import fs from "fs";
|
||||
import os from "os";
|
||||
import path from "path";
|
||||
import url from "url";
|
||||
|
||||
export async function bundleForTest({
|
||||
// Unique-ish name; also names the temp artifacts (and the generated entry
|
||||
// file when entrySource is used).
|
||||
name,
|
||||
// Module paths to bundle directly (relative to the project root or absolute).
|
||||
entryPoints,
|
||||
// Alternative to entryPoints: inline TS re-export text, written to a temp
|
||||
// entry file next to the project root (deleted right after the build).
|
||||
entrySource,
|
||||
format = "esm",
|
||||
external = [],
|
||||
} = {}) {
|
||||
const ext = format === "cjs" ? "cjs" : "mjs";
|
||||
const out = path.join(os.tmpdir(), `${name}-${process.pid}.${ext}`);
|
||||
let tempEntry = null;
|
||||
let eps = entryPoints ?? [];
|
||||
if (entrySource !== undefined) {
|
||||
tempEntry = path.resolve(`.${name}-entry.ts`);
|
||||
fs.writeFileSync(tempEntry, entrySource);
|
||||
eps = [tempEntry];
|
||||
}
|
||||
await esbuild.build({
|
||||
entryPoints: eps.map((p) => path.resolve(p)),
|
||||
bundle: true,
|
||||
platform: "node",
|
||||
format,
|
||||
outfile: out,
|
||||
external,
|
||||
write: true,
|
||||
logLevel: "silent",
|
||||
});
|
||||
if (tempEntry) fs.rmSync(tempEntry, { force: true });
|
||||
const mod = await import(url.pathToFileURL(out).href);
|
||||
return {
|
||||
mod,
|
||||
// Removes only this call's bundled artifact.
|
||||
cleanupBundle() {
|
||||
fs.rmSync(out, { force: true });
|
||||
},
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue