diff --git a/test/cli.subprocess.test.ts b/test/cli.subprocess.test.ts index 959f212..97ddece 100644 --- a/test/cli.subprocess.test.ts +++ b/test/cli.subprocess.test.ts @@ -7,7 +7,7 @@ * and runs `auth whoami` against the mock." */ -import { execFileSync, spawn } from 'node:child_process'; +import { spawn } from 'node:child_process'; import { existsSync, mkdtempSync, rmSync, statSync } from 'node:fs'; import type { IncomingMessage, Server, ServerResponse } from 'node:http'; import { createServer } from 'node:http'; @@ -15,6 +15,7 @@ import { tmpdir } from 'node:os'; import { dirname, join, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; import { afterAll, beforeAll, describe, expect, it } from 'vitest'; +import { runNpmScript } from './helpers/npm.js'; const __dirname = dirname(fileURLToPath(import.meta.url)); const REPO_ROOT = resolve(__dirname, '..'); @@ -37,7 +38,7 @@ beforeAll(async () => { // existsSync skip we used to do here let `dist` rot under // refactors and gave false-green on `project list` once // already. - execFileSync('npm', ['run', 'build'], { cwd: REPO_ROOT, stdio: 'pipe' }); + runNpmScript('build', REPO_ROOT); server = createServer((req: IncomingMessage, res: ServerResponse) => { const url = req.url ?? '/'; if (url.startsWith('/api/cli/v1/projects/')) { diff --git a/test/help.snapshot.test.ts b/test/help.snapshot.test.ts index ee89b3b..79b150b 100644 --- a/test/help.snapshot.test.ts +++ b/test/help.snapshot.test.ts @@ -13,6 +13,7 @@ import { execFileSync } from 'node:child_process'; import { dirname, join, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; import { beforeAll, describe, expect, it } from 'vitest'; +import { runNpmScript } from './helpers/npm.js'; const __dirname = dirname(fileURLToPath(import.meta.url)); const REPO_ROOT = resolve(__dirname, '..'); @@ -47,7 +48,7 @@ const cases: Array<[string, string[]]> = [ describe('--help snapshots', () => { beforeAll(() => { - execFileSync('npm', ['run', 'build'], { cwd: REPO_ROOT, stdio: 'pipe' }); + runNpmScript('build', REPO_ROOT); }); for (const [name, args] of cases) { diff --git a/test/helpers/npm.ts b/test/helpers/npm.ts new file mode 100644 index 0000000..747405a --- /dev/null +++ b/test/helpers/npm.ts @@ -0,0 +1,13 @@ +import { execFileSync } from 'node:child_process'; + +export function runNpmScript(script: string, cwd: string): void { + const npmExecPath = process.env.npm_execpath; + if (npmExecPath) { + execFileSync(process.execPath, [npmExecPath, 'run', script], { cwd, stdio: 'pipe' }); + return; + } + execFileSync(process.platform === 'win32' ? 'npm.cmd' : 'npm', ['run', script], { + cwd, + stdio: 'pipe', + }); +}