Add simple test for bun --hot

This commit is contained in:
Jarred Sumner
2023-01-24 03:30:27 -08:00
parent 6f682c6369
commit bb5119f7eb
3 changed files with 56 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
globalThis.importedCounter ??= 0;
console.log(
`[${Date.now()}] [#!imported] Reloaded: ${++globalThis.importedCounter}`,
);

View File

@@ -0,0 +1,7 @@
import "./hot-runner-imported";
globalThis.counter ??= 0;
console.log(`[${Date.now()}] [#!root] Reloaded: ${++globalThis.counter}`);
setTimeout(() => {}, 9999999999);

44
test/bun.js/hot.test.ts Normal file
View File

@@ -0,0 +1,44 @@
import { spawn } from "bun";
import { expect, it } from "bun:test";
import { bunEnv } from "bunEnv";
import { bunExe } from "bunExe";
import { readFileSync, unlinkSync, writeFileSync } from "fs";
it("should hot reload when file is overwritten", async () => {
const root = import.meta.dir + "/hot-runner.js";
const runner = spawn({
cmd: [bunExe(), "--hot", "run", root],
env: bunEnv,
stdout: "pipe",
stderr: "inherit",
stdin: "ignore",
});
var reloadCounter = 0;
async function onReload() {
writeFileSync(root, readFileSync(root, "utf-8"));
}
await (async function () {
for await (const line of runner.stdout!) {
var str = new TextDecoder().decode(line);
if (str.includes("[#!root]")) {
reloadCounter++;
if (reloadCounter === 3) {
runner.unref();
runner.kill();
return;
}
expect(str).toContain(`[#!root] Reloaded: ${reloadCounter}`);
await onReload();
}
}
})();
expect(reloadCounter).toBe(3);
});