

























@@ -1,4 +1,5 @@
11import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
2+import { withMockedPlatform } from "../test-utils/vitest-spies.js";
2334const { spawnMock } = vi.hoisted(() => ({
45spawnMock: vi.fn(),
@@ -28,18 +29,6 @@ function expectTaskkillCall(index: number, args: string[]) {
2829]);
2930}
303131-async function withPlatform<T>(platform: NodeJS.Platform, run: () => Promise<T> | T): Promise<T> {
32-const originalPlatform = Object.getOwnPropertyDescriptor(process, "platform");
33-Object.defineProperty(process, "platform", { value: platform, configurable: true });
34-try {
35-return await run();
36-} finally {
37-if (originalPlatform) {
38-Object.defineProperty(process, "platform", originalPlatform);
39-}
40-}
41-}
42-4332describe("killProcessTree", () => {
4433let killSpy: ReturnType<typeof vi.spyOn>;
4534@@ -67,7 +56,7 @@ describe("killProcessTree", () => {
6756return true;
6857}) as typeof process.kill);
695870-await withPlatform("win32", async () => {
59+await withMockedPlatform("win32", async () => {
7160killProcessTree(4242, { graceMs: 25 });
72617362expect(spawnMock).toHaveBeenCalledTimes(1);
@@ -86,7 +75,7 @@ describe("killProcessTree", () => {
8675return true;
8776}) as typeof process.kill);
887789-await withPlatform("win32", async () => {
78+await withMockedPlatform("win32", async () => {
9079killProcessTree(5252, { graceMs: 10 });
91809281await vi.advanceTimersByTimeAsync(10);
@@ -108,7 +97,7 @@ describe("killProcessTree", () => {
10897return true;
10998}) as typeof process.kill);
11099111-await withPlatform("linux", async () => {
100+await withMockedPlatform("linux", async () => {
112101killProcessTree(3333, { graceMs: 10 });
113102114103await vi.advanceTimersByTimeAsync(10);
@@ -127,7 +116,7 @@ describe("killProcessTree", () => {
127116return true;
128117}) as typeof process.kill);
129118130-await withPlatform("linux", async () => {
119+await withMockedPlatform("linux", async () => {
131120killProcessTree(4444, { graceMs: 5 });
132121133122await vi.advanceTimersByTimeAsync(5);
@@ -145,7 +134,7 @@ describe("killProcessTree", () => {
145134return true;
146135}) as typeof process.kill);
147136148-await withPlatform("linux", async () => {
137+await withMockedPlatform("linux", async () => {
149138killProcessTree(5555, { graceMs: 10, detached: false });
150139await vi.advanceTimersByTimeAsync(10);
151140@@ -169,7 +158,7 @@ describe("killProcessTree", () => {
169158return true;
170159}) as typeof process.kill);
171160172-await withPlatform("linux", async () => {
161+await withMockedPlatform("linux", async () => {
173162killProcessTree(6666, { graceMs: 10 });
174163await vi.advanceTimersByTimeAsync(10);
175164此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。