mirror of
https://github.com/oven-sh/bun
synced 2026-02-14 21:01:52 +00:00
Add simple test for bun --hot
This commit is contained in:
5
test/bun.js/hot-runner-imported.js
Normal file
5
test/bun.js/hot-runner-imported.js
Normal file
@@ -0,0 +1,5 @@
|
||||
globalThis.importedCounter ??= 0;
|
||||
|
||||
console.log(
|
||||
`[${Date.now()}] [#!imported] Reloaded: ${++globalThis.importedCounter}`,
|
||||
);
|
||||
7
test/bun.js/hot-runner.js
Normal file
7
test/bun.js/hot-runner.js
Normal 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
44
test/bun.js/hot.test.ts
Normal 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);
|
||||
});
|
||||
Reference in New Issue
Block a user