-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.test.ts
41 lines (34 loc) · 1.46 KB
/
index.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import {fork} from "node:child_process";
import {platform, tmpdir} from "node:os";
import {mkdtempSync} from "node:fs";
import {readFile, copyFile, rm} from "node:fs/promises";
import {join} from "node:path";
const testDir = mkdtempSync(join(tmpdir(), "daemonize-process-"));
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
beforeAll(async () => {
await copyFile(new URL("index.ts", import.meta.url), join(testDir, "index.ts"));
await copyFile(new URL("child.ts", import.meta.url), join(testDir, "child.ts"));
await copyFile(new URL("package.json", import.meta.url), join(testDir, "package.json"));
});
afterAll(async () => {
await rm(testDir, {recursive: true});
});
test("simple", () => {
return new Promise(resolve => {
const child = fork(join(testDir, "child.ts"));
child.on("exit", async () => {
await sleep(1000); // give the child some time to exit
const [ppid, envVar] = (await readFile(join(testDir, "test-output"), "utf8")).split(",");
// check if process was correctly orphaned. on unix, this should generally mean that
// pid 1 picked up the child, but it'll be different on other platforms.
if (platform() === "win32") {
expect(ppid).toMatch(/[0-9]+/);
} else {
expect(["0", "1"].includes(ppid)).toEqual(true);
}
// verify that internal tracking variable is not leaked to the child
expect(envVar).toEqual("false");
resolve(undefined);
});
});
});