@@ -51,7 +51,11 @@ describe("pm2-control", () => {
5151 return undefined ;
5252 } ) ;
5353 start . mockImplementation (
54- ( _config : unknown , callback : ( error : Error | null , apps : unknown [ ] ) => void ) => {
54+ (
55+ _script : string ,
56+ _config : unknown ,
57+ callback : ( error : Error | null , apps : unknown [ ] ) => void
58+ ) => {
5559 writeRuntimeConfig ( {
5660 host : "127.0.0.1" ,
5761 port : 4187 ,
@@ -106,20 +110,19 @@ describe("pm2-control", () => {
106110 } ) ;
107111
108112 it ( "starts the managed server with the fixed app name" , async ( ) => {
113+ process . env . NODE_ENV = "development" ;
114+
109115 await startManagedServer ( {
110116 script : "/cli/dist/esm/server-runner.js" ,
111117 cwd : "/repo" ,
112118 waitMs : 10 ,
113119 } ) ;
114120
115121 expect ( start ) . toHaveBeenCalledWith (
122+ "/cli/dist/esm/server-runner.js" ,
116123 expect . objectContaining ( {
117124 name : MANAGED_SERVER_NAME ,
118- script : "/cli/dist/esm/server-runner.js" ,
119125 cwd : "/repo" ,
120- env : expect . objectContaining ( {
121- NODE_ENV : "production" ,
122- } ) ,
123126 autorestart : true ,
124127 restart_delay : 2000 ,
125128 min_uptime : "5s" ,
@@ -131,6 +134,54 @@ describe("pm2-control", () => {
131134 ) ;
132135 } ) ;
133136
137+ it ( "temporarily forces NODE_ENV=production during PM2 startup without passing env opts" , async ( ) => {
138+ process . env . NODE_ENV = "development" ;
139+
140+ start . mockImplementationOnce (
141+ (
142+ _script : string ,
143+ config : unknown ,
144+ callback : ( error : Error | null , apps : unknown [ ] ) => void
145+ ) => {
146+ expect ( process . env . NODE_ENV ) . toBe ( "production" ) ;
147+ expect ( config ) . not . toHaveProperty ( "env" ) ;
148+ writeRuntimeConfig ( {
149+ host : "127.0.0.1" ,
150+ port : 4187 ,
151+ pid : 424242 ,
152+ token : "test-token" ,
153+ serverInstanceId : "server-1" ,
154+ startedAt : Date . now ( ) ,
155+ } ) ;
156+ callback ( null , [ { pm_id : 1 } ] ) ;
157+ }
158+ ) ;
159+
160+ await startManagedServer ( {
161+ script : "/cli/dist/esm/server-runner.js" ,
162+ cwd : "/repo" ,
163+ waitMs : 10 ,
164+ } ) ;
165+
166+ expect ( process . env . NODE_ENV ) . toBe ( "development" ) ;
167+ } ) ;
168+
169+ it ( "starts PM2 in script mode instead of JSON config mode" , async ( ) => {
170+ await startManagedServer ( {
171+ script : "/cli/dist/esm/server-runner.js" ,
172+ cwd : "/repo" ,
173+ waitMs : 10 ,
174+ } ) ;
175+
176+ expect ( start ) . toHaveBeenCalledWith (
177+ "/cli/dist/esm/server-runner.js" ,
178+ expect . not . objectContaining ( {
179+ script : expect . anything ( ) ,
180+ } ) ,
181+ expect . any ( Function )
182+ ) ;
183+ } ) ;
184+
134185 it ( "passes script args through for the managed server entrypoint" , async ( ) => {
135186 await startManagedServer ( {
136187 script : "/cli/dist/esm/server-runner.js" ,
@@ -140,8 +191,8 @@ describe("pm2-control", () => {
140191 } ) ;
141192
142193 expect ( start ) . toHaveBeenCalledWith (
194+ "/cli/dist/esm/server-runner.js" ,
143195 expect . objectContaining ( {
144- script : "/cli/dist/esm/server-runner.js" ,
145196 args : [ "--flag" ] ,
146197 } ) ,
147198 expect . any ( Function )
@@ -305,7 +356,11 @@ describe("pm2-control", () => {
305356
306357 it ( "fails background startup when runtime readiness times out" , async ( ) => {
307358 start . mockImplementationOnce (
308- ( _config : unknown , callback : ( error : Error | null , apps : unknown [ ] ) => void ) => {
359+ (
360+ _script : string ,
361+ _config : unknown ,
362+ callback : ( error : Error | null , apps : unknown [ ] ) => void
363+ ) => {
309364 callback ( null , [ { pm_id : 1 } ] ) ;
310365 }
311366 ) ;
@@ -340,7 +395,11 @@ describe("pm2-control", () => {
340395 writeFileSync ( errFile , "Error: stale previous startup failure\n" ) ;
341396
342397 start . mockImplementationOnce (
343- ( config : unknown , callback : ( error : Error | null , apps : unknown [ ] ) => void ) => {
398+ (
399+ _script : string ,
400+ config : unknown ,
401+ callback : ( error : Error | null , apps : unknown [ ] ) => void
402+ ) => {
344403 const errorFile = ( config as { error_file : string } ) . error_file ;
345404 writeFileSync (
346405 errorFile ,
@@ -374,6 +433,11 @@ describe("pm2-control", () => {
374433 } ) ;
375434
376435 it ( "maps missing PM2 app to stopped status" , async ( ) => {
436+ describeProcess . mockImplementationOnce (
437+ ( _name : string , callback : ( error : Error | null , result : unknown [ ] ) => void ) =>
438+ callback ( null , [ ] )
439+ ) ;
440+
377441 await expect ( getManagedServerStatus ( ) ) . resolves . toEqual ( {
378442 status : "stopped" ,
379443 pm2Pid : null ,
0 commit comments