11import { spawn } from 'node:child_process'
2- import { existsSync } from 'node:fs'
2+ import fsp from 'node:fs/promises '
33import os from 'node:os'
44import path from 'node:path'
55import { fileURLToPath } from 'node:url'
@@ -8,10 +8,21 @@ import { beforeAll, describe, expect, it } from 'vitest'
88
99const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) )
1010const cliPath = path . join ( __dirname , '../../dist/cli.mjs' )
11+ let tarballPath : string | null = null
12+ let packTmpDir : string | null = null
1113
1214beforeAll ( async ( ) => {
13- if ( ! existsSync ( cliPath ) ) {
14- await execa ( 'npm' , [ 'run' , 'build' ] , { cwd : path . join ( __dirname , '../..' ) } )
15+ await execa ( 'npm' , [ 'run' , 'build' ] , { cwd : path . join ( __dirname , '../..' ) } )
16+ packTmpDir = await fsp . mkdtemp ( path . join ( os . tmpdir ( ) , 'branchpilot-pack-' ) )
17+ const { stdout : packOut } = await execa ( 'npm' , [ 'pack' , '--silent' , path . join ( __dirname , '../..' ) ] , {
18+ cwd : packTmpDir ,
19+ } )
20+ tarballPath = path . join ( packTmpDir , packOut . trim ( ) )
21+ } )
22+
23+ afterAll ( async ( ) => {
24+ if ( packTmpDir ) {
25+ await fsp . rm ( packTmpDir , { recursive : true , force : true } )
1526 }
1627} )
1728
@@ -136,7 +147,8 @@ describe('CLI', () => {
136147
137148 describe ( 'npx execution' , ( ) => {
138149 it ( 'works with npx for version command' , async ( ) => {
139- const { stdout, exitCode } = await execa ( 'npx' , [ '--yes' , 'branchpilot' , '--version' ] , {
150+ const pkgArg = tarballPath ?? 'branchpilot'
151+ const { stdout, exitCode } = await execa ( 'npx' , [ '--yes' , '--package' , pkgArg , 'branchpilot' , '--version' ] , {
140152 env : { ...process . env , NODE_ENV : 'test' } ,
141153 } )
142154 expect ( exitCode ) . toBe ( 0 )
@@ -145,9 +157,14 @@ describe('CLI', () => {
145157
146158 it ( 'works with npx for list command' , async ( ) => {
147159 const tempDir = os . tmpdir ( )
148- const { stdout, exitCode } = await execa ( 'npx' , [ '--yes' , 'branchpilot' , 'list' , '--dir' , tempDir ] , {
149- env : { ...process . env , NODE_ENV : 'test' } ,
150- } )
160+ const pkgArg = tarballPath ?? 'branchpilot'
161+ const { stdout, exitCode } = await execa (
162+ 'npx' ,
163+ [ '--yes' , '--package' , pkgArg , 'branchpilot' , 'list' , '--dir' , tempDir ] ,
164+ {
165+ env : { ...process . env , NODE_ENV : 'test' } ,
166+ } ,
167+ )
151168 expect ( exitCode ) . toBe ( 0 )
152169 expect ( stdout ) . toContain ( 'No tickets found' )
153170 } , 20000 )
0 commit comments