@@ -6,6 +6,29 @@ import fs from 'node:fs/promises'
66import { afterAll } from 'vitest'
77import { beforeAll } from 'vitest'
88
9+ // Helper to run command and capture output even if it fails
10+ function runCommand ( cmd : string , options : any ) {
11+ const opts = { ...options , encoding : 'utf8' , env : { ...process . env , NO_COLOR : '1' } }
12+ try {
13+ return execSync ( cmd , opts ) . toString ( )
14+ } catch ( error : any ) {
15+ // If command fails, still return the output (stdout + stderr)
16+ const stdout = error . stdout ? error . stdout . toString ( ) : ''
17+ const stderr = error . stderr ? error . stderr . toString ( ) : ''
18+ const output = stdout + stderr
19+
20+ // Debug: log first 200 chars of output if test is failing
21+ if ( process . env . DEBUG_CLI_TEST ) {
22+ console . log ( 'Command failed:' , cmd )
23+ console . log ( 'Output (first 200 chars):' , output . slice ( 0 , 200 ) )
24+ console . log ( 'stdout length:' , stdout . length , 'stderr length:' , stderr . length )
25+ }
26+
27+ if ( output ) return output
28+ throw error
29+ }
30+ }
31+
932describe ( 'CLI' , ( ) => {
1033 const cwd = process . cwd ( )
1134 const _dirname = path . dirname ( fileURLToPath ( import . meta. url ) )
@@ -51,7 +74,7 @@ describe('CLI', () => {
5174 const cmd = `node ${ binPath } init --cwd="${ testsCwd } "`
5275
5376 // init
54- const output = execSync ( cmd , { cwd : testsCwd } ) . toString ( )
77+ const output = runCommand ( cmd , { cwd : testsCwd } )
5578 // Check for either "Thanks" (new config) or existing config message
5679 expect ( output . includes ( 'Thanks' ) || output . includes ( 'It looks like you already have panda created' ) ) . toBe ( true )
5780
@@ -60,11 +83,11 @@ describe('CLI', () => {
6083 expect ( configFileExists ) . toBeUndefined ( )
6184
6285 // init on existing project
63- const output2 = execSync ( cmd , { cwd : testsCwd } ) . toString ( )
86+ const output2 = runCommand ( cmd , { cwd : testsCwd } )
6487 expect ( output2 . includes ( 'It looks like you already have panda created' ) ) . toBe ( true )
6588
6689 // init with --force
67- const output3 = execSync ( cmd + ' --force --postcss --logfile="./panda.log"' , { cwd : testsCwd } ) . toString ( )
90+ const output3 = runCommand ( cmd + ' --force --postcss --logfile="./panda.log"' , { cwd : testsCwd } )
6891 expect ( output3 . includes ( 'Panda initialized' ) ) . toBe ( true )
6992
7093 // Check if the postcss config file was created
@@ -80,7 +103,7 @@ describe('CLI', () => {
80103 const cmd = `node ${ binPath } codegen --cwd="${ testsCwd } "`
81104
82105 // codegen
83- const output = execSync ( cmd + ' --cpu-prof' , { cwd : testsCwd } ) . toString ( )
106+ const output = runCommand ( cmd + ' --cpu-prof' , { cwd : testsCwd } )
84107 expect ( output . includes ( 'the css function to author styles' ) ) . toBe ( true )
85108
86109 // Check that the `styled-system` folder was created
@@ -102,33 +125,33 @@ describe('CLI', () => {
102125 const cmd = `node ${ binPath } cssgen --cwd="${ testsCwd } "`
103126
104127 // cssgen
105- const output = execSync ( cmd , { cwd : testsCwd } ) . toString ( )
128+ const output = runCommand ( cmd , { cwd : testsCwd } )
106129 expect ( output . includes ( 'Successfully extracted css' ) ) . toBe ( true )
107130
108131 // Check that the `styled-system/styles.css` was created
109132 const stylesCssExists = await fs . access ( path . resolve ( paths . styledSystem , 'styles.css' ) )
110133 expect ( stylesCssExists ) . toBeUndefined ( )
111134
112135 // Check that using `lightningcss` is fine
113- const output2 = execSync ( cmd + ' --lightningcss' , { cwd : testsCwd } ) . toString ( )
136+ const output2 = runCommand ( cmd + ' --lightningcss' , { cwd : testsCwd } )
114137 expect ( output2 . includes ( 'Successfully extracted css' ) ) . toBe ( true )
115138
116139 // Check that `--outfile` is fine
117- const output3 = execSync ( cmd + ' --outfile="./styles.css"' , { cwd : testsCwd } ) . toString ( )
140+ const output3 = runCommand ( cmd + ' --outfile="./styles.css"' , { cwd : testsCwd } )
118141 expect ( output3 . includes ( 'Successfully extracted css' ) ) . toBe ( true )
119142
120143 await fs . unlink ( path . resolve ( testsCwd , 'styles.css' ) )
121144
122145 // Check that `--silent` is fine
123- const output4 = execSync ( cmd + ' --silent' , { cwd : testsCwd } ) . toString ( )
146+ const output4 = runCommand ( cmd + ' --silent' , { cwd : testsCwd } )
124147 expect ( output4 . trim ( ) . length ) . toBe ( 0 )
125148 } )
126149
127150 test ( 'default' , async ( ) => {
128151 const cmd = `node ${ binPath } --cwd="${ testsCwd } "`
129152
130153 // default
131- const output = execSync ( cmd , { cwd : testsCwd } ) . toString ( )
154+ const output = runCommand ( cmd , { cwd : testsCwd } )
132155 expect ( output . includes ( 'Successfully extracted css' ) ) . toBe ( true )
133156
134157 // Check that the `styled-system` folder was created
@@ -144,7 +167,7 @@ describe('CLI', () => {
144167 const cmd = `node ${ binPath } studio --cwd="${ testsCwd } "`
145168
146169 // studio
147- const output = execSync ( cmd + ' --build' , { cwd : testsCwd } ) . toString ( )
170+ const output = runCommand ( cmd + ' --build' , { cwd : testsCwd } )
148171 expect ( output . includes ( 'Complete!' ) ) . toBe ( true )
149172
150173 // Check that the `styled-system-studio` folder was created
@@ -156,7 +179,7 @@ describe('CLI', () => {
156179 const cmd = `node ${ binPath } debug --cwd="${ testsCwd } "`
157180
158181 // debug
159- const output = execSync ( cmd , { cwd : testsCwd } ) . toString ( )
182+ const output = runCommand ( cmd , { cwd : testsCwd } )
160183 expect ( output . includes ( 'files using Panda' ) ) . toBe ( true )
161184
162185 // Check that the `styled-system/debug` folder was created
@@ -172,7 +195,7 @@ describe('CLI', () => {
172195 const cmd = `node ${ binPath } ship --cwd="${ testsCwd } "`
173196
174197 // ship
175- const output = execSync ( cmd , { cwd : testsCwd } ) . toString ( )
198+ const output = runCommand ( cmd , { cwd : testsCwd } )
176199 expect ( output . includes ( 'files using Panda' ) ) . toBe ( true )
177200
178201 // Check that the `styled-system/panda.buildinfo.json` file was created
@@ -184,7 +207,7 @@ describe('CLI', () => {
184207 const cmd = `node ${ binPath } emit-pkg --cwd="${ testsCwd } "`
185208
186209 // emit-pkg
187- const output = execSync ( cmd , { cwd : testsCwd } ) . toString ( )
210+ const output = runCommand ( cmd , { cwd : testsCwd } )
188211 expect ( output . includes ( 'Emit package.json' ) ) . toBe ( true )
189212
190213 // Check that the `package.json` file was created
0 commit comments