Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/cli/src/commands/previewLifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,11 @@ describe("background preview lifecycle", () => {

expect(result).toMatchObject({ type: "started", port: 3210, pid: 4321 });
expect(unref).toHaveBeenCalledOnce();
expect(spawn).toHaveBeenCalledWith(
"/usr/bin/node",
expect.any(Array),
expect.objectContaining({ detached: true, windowsHide: true }),
);
expect(existsSync(previewSessionPath(projectDir, stateHome))).toBe(true);
});

Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/commands/previewLifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type SpawnPreview = (
detached: boolean;
stdio: ["ignore", number, number];
env: NodeJS.ProcessEnv;
windowsHide: boolean;
},
) => SpawnResult;

Expand Down Expand Up @@ -219,6 +220,7 @@ function spawnDetachedPreview(
detached: true,
stdio: ["ignore", logFd, logFd],
env: process.env,
windowsHide: true,
},
);
} finally {
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/telemetry/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,14 @@ describe("telemetry queue delivery", () => {
const [execPath, args, opts] = spawnMock.mock.calls[0] as unknown as [
string,
string[],
{ detached: boolean },
{ detached: boolean; windowsHide?: boolean },
];
expect(execPath).toBe(process.execPath);
expect(args[0]).toBe("-e");
expect(args[1]).toContain("render_complete");
expect(args[1]).toMatch(/[0-9a-f-]{36}/); // event uuid rides along
expect(opts.detached).toBe(true);
expect(opts.windowsHide).toBe(true);

// Queue handed to the child — nothing left for a regular flush.
const fetchMock = vi.fn(() => Promise.resolve(new Response("")));
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/telemetry/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export function flushSync(): void {
"-e",
`fetch(${JSON.stringify(`${POSTHOG_HOST}/batch/`)},{method:"POST",headers:{"Content-Type":"application/json"},body:${JSON.stringify(payload)},signal:AbortSignal.timeout(${FLUSH_TIMEOUT_MS})}).catch(()=>{})`,
],
{ detached: true, stdio: "ignore" },
{ detached: true, stdio: "ignore", windowsHide: true },
);
// Let the parent exit without waiting for the child
child.unref();
Expand Down
24 changes: 23 additions & 1 deletion packages/cli/src/utils/openBrowser.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
import { describe, it, expect } from "vitest";
import { describe, it, expect, vi } from "vitest";

const spawnMock = vi.hoisted(() =>
vi.fn(() => ({
on: vi.fn(),
unref: vi.fn(),
})),
);
vi.mock("node:child_process", () => ({ spawn: spawnMock }));

import {
buildBrowserArgs,
openBrowser,
parseRemoteDebuggingPort,
validateRemoteDebuggingPortDeps,
} from "./openBrowser.js";

describe("openBrowser", () => {
it("hides the console window when spawning a detached browser", () => {
openBrowser("http://localhost:3002", { browserPath: "C:\\Browser\\browser.exe" });

expect(spawnMock).toHaveBeenCalledWith(
"C:\\Browser\\browser.exe",
["http://localhost:3002"],
expect.objectContaining({ detached: true, windowsHide: true }),
);
});
});

describe("buildBrowserArgs", () => {
it("returns only the URL when no options are given", () => {
expect(buildBrowserArgs("http://localhost:3002", {})).toEqual(["http://localhost:3002"]);
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/utils/openBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export function openBrowser(url: string, options: OpenBrowserOptions = {}): void
const child = spawn(options.browserPath, args, {
detached: true,
stdio: "ignore",
windowsHide: true,
});
child.on("error", () => {});
child.unref();
Expand Down