























@@ -48,12 +48,14 @@ type MockChild = EventEmitter & {
4848killed?: boolean;
4949};
505051-function spawnCallAt(callIndex: number): unknown[] {
51+type SpawnCall = [string, string[], Record<string, unknown>];
52+53+function requireSpawnCall(callIndex: number): SpawnCall {
5254const call = spawnMock.mock.calls[callIndex];
5355if (!call) {
5456throw new Error(`expected spawn call ${callIndex}`);
5557}
56-return call;
58+return call as SpawnCall;
5759}
58605961function createMockChild(params?: {
@@ -90,22 +92,25 @@ function createMockChild(params?: {
9092return child;
9193}
929493-type SpawnCall = [string, string[], Record<string, unknown>];
94-9595type ExecCall = [
9696string,
9797string[],
9898Record<string, unknown>,
9999(err: Error | null, stdout: string, stderr: string) => void,
100100];
101101102+function requireExecFileCall(callIndex: number): ExecCall {
103+const call = execFileMock.mock.calls[callIndex];
104+if (!call) {
105+throw new Error(`expected execFile call ${callIndex}`);
106+}
107+return call as ExecCall;
108+}
109+102110function expectCmdWrappedInvocation(params: {
103-captured: SpawnCall | ExecCall | undefined;
111+captured: SpawnCall | ExecCall;
104112expectedComSpec: string;
105113}) {
106-if (!params.captured) {
107-throw new Error("expected command wrapper to be called");
108-}
109114expect(params.captured[0]).toBe(params.expectedComSpec);
110115expect(params.captured[1].slice(0, 3)).toEqual(["/d", "/s", "/c"]);
111116expect(params.captured[1][3]).toContain("pnpm.cmd --version");
@@ -186,7 +191,7 @@ describe("windows command wrapper behavior", () => {
186191try {
187192const result = await runCommandWithTimeout(["pnpm", "--version"], { timeoutMs: 1000 });
188193expect(result.code).toBe(0);
189-const captured = spawnMock.mock.calls[0] as SpawnCall | undefined;
194+const captured = requireSpawnCall(0);
190195expectCmdWrappedInvocation({ captured, expectedComSpec });
191196} finally {
192197platformSpy.mockRestore();
@@ -207,7 +212,7 @@ describe("windows command wrapper behavior", () => {
207212try {
208213const result = await runCommandWithTimeout(["pnpm", "--version"], { timeoutMs: 1000 });
209214expect(result.code).toBe(0);
210-const captured = spawnMock.mock.calls[0] as SpawnCall | undefined;
215+const captured = requireSpawnCall(0);
211216expectCmdWrappedInvocation({
212217 captured,
213218expectedComSpec: path.win32.join("C:\\Windows", "System32", "cmd.exe"),
@@ -254,7 +259,7 @@ describe("windows command wrapper behavior", () => {
254259255260const result = await runCommandWithTimeout(["pnpm", "--version"], { timeoutMs: 1000 });
256261expect(result.code).toBe(0);
257-const captured = spawnMock.mock.calls[0] as SpawnCall | undefined;
262+const captured = requireSpawnCall(0);
258263expectCmdWrappedInvocation({
259264 captured,
260265expectedComSpec: path.win32.join("C:\\Windows", "System32", "cmd.exe"),
@@ -286,10 +291,7 @@ describe("windows command wrapper behavior", () => {
286291try {
287292const result = await runCommandWithTimeout(["corepack", "--version"], { timeoutMs: 1000 });
288293expect(result.code).toBe(0);
289-const captured = spawnMock.mock.calls[0] as SpawnCall | undefined;
290-if (!captured) {
291-throw new Error("expected corepack shim spawn");
292-}
294+const captured = requireSpawnCall(0);
293295expect(captured[0]).toBe(expectedComSpec);
294296expect(captured[1].slice(0, 3)).toEqual(["/d", "/s", "/c"]);
295297expect(captured[1][3]).toContain("corepack.cmd --version");
@@ -324,10 +326,7 @@ describe("windows command wrapper behavior", () => {
324326try {
325327const result = await runCommandWithTimeout(["npm", "--version"], { timeoutMs: 1000 });
326328expect(result.code).toBe(0);
327-const captured = spawnMock.mock.calls[0] as SpawnCall | undefined;
328-if (!captured) {
329-throw new Error("expected npm shim spawn");
330-}
329+const captured = requireSpawnCall(0);
331330expect(captured[0]).toBe(process.execPath);
332331expect(captured[1][0]).toBe(
333332path.join(path.dirname(process.execPath), "node_modules", "npm", "bin", "npm-cli.js"),
@@ -354,10 +353,7 @@ describe("windows command wrapper behavior", () => {
354353try {
355354const result = await runCommandWithTimeout(["npm", "--version"], { timeoutMs: 1000 });
356355expect(result.code).toBe(0);
357-const captured = spawnMock.mock.calls[0] as SpawnCall | undefined;
358-if (!captured) {
359-throw new Error("expected npm.cmd fallback spawn");
360-}
356+const captured = requireSpawnCall(0);
361357expect(captured[0]).toBe(expectedComSpec);
362358expect(captured[1].slice(0, 3)).toEqual(["/d", "/s", "/c"]);
363359expect(captured[1][3]).toContain("npm.cmd --version");
@@ -414,7 +410,7 @@ describe("windows command wrapper behavior", () => {
414410415411try {
416412await runExec("pnpm", ["--version"], 1000);
417-const captured = execFileMock.mock.calls[0] as ExecCall | undefined;
413+const captured = requireExecFileCall(0);
418414expectCmdWrappedInvocation({ captured, expectedComSpec });
419415} finally {
420416platformSpy.mockRestore();
@@ -437,10 +433,7 @@ describe("windows command wrapper behavior", () => {
437433438434try {
439435await runExec("node", ["--version"], 1000);
440-const captured = execFileMock.mock.calls[0] as ExecCall | undefined;
441-if (!captured) {
442-throw new Error("expected direct execFile invocation");
443-}
436+const captured = requireExecFileCall(0);
444437expect(captured[0]).toBe("node");
445438expect(captured[1]).toEqual(["--version"]);
446439expect(captured[2].windowsHide).toBe(true);
@@ -459,10 +452,7 @@ describe("windows command wrapper behavior", () => {
459452try {
460453const result = await runCommandWithTimeout(["node", "--version"], { timeoutMs: 1000 });
461454expect(result.code).toBe(0);
462-const captured = spawnMock.mock.calls[0] as SpawnCall | undefined;
463-if (!captured) {
464-throw new Error("expected direct spawn invocation");
465-}
455+const captured = requireSpawnCall(0);
466456expect(captured[0]).toBe("node");
467457expect(captured[1]).toEqual(["--version"]);
468458expect(captured[2].windowsHide).toBe(true);
@@ -486,7 +476,7 @@ describe("windows command wrapper behavior", () => {
486476await vi.advanceTimersByTimeAsync(81);
487477expect(child.kill).not.toHaveBeenCalled();
488478expect(spawnMock).toHaveBeenCalledTimes(2);
489-const taskkillCall = spawnCallAt(1);
479+const taskkillCall = requireSpawnCall(1);
490480expect(taskkillCall[0]).toBe("taskkill");
491481expect(taskkillCall[1]).toEqual(["/PID", "1234", "/T", "/F"]);
492482expect(taskkillCall[2]).toEqual({
@@ -524,8 +514,8 @@ describe("windows command wrapper behavior", () => {
524514const result = await runExec("node", ["gbk-output.js"], 1000);
525515expect(result.stdout).toBe("测试");
526516expect(result.stderr).toBe(";");
527-const captured = execFileMock.mock.calls[0] as ExecCall | undefined;
528-expect(captured?.[2].encoding).toBe("buffer");
517+const captured = requireExecFileCall(0);
518+expect(captured[2].encoding).toBe("buffer");
529519} finally {
530520platformSpy.mockRestore();
531521}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。