@@ -21,7 +21,8 @@ describe("dev-server", () => {
2121 let childProcessStub : EventEmitter & { kill : SinonStub } ;
2222 let pipeLogsWithPrefixStub : SinonStub ;
2323 let waitDevServerReadyStub : SinonStub ;
24- let loggerStub : { log : SinonStub } ;
24+ let probeServerStub : SinonStub ;
25+ let loggerStub : { log : SinonStub ; warn : SinonStub } ;
2526 let debugLog : SinonStub ;
2627 let testplaneStub : Testplane & { halt : SinonStub } ;
2728 let findCwdStub : SinonStub ;
@@ -49,9 +50,10 @@ describe("dev-server", () => {
4950 spawnStub = sandbox . stub ( ) . returns ( childProcessStub ) ;
5051 pipeLogsWithPrefixStub = sandbox . stub ( ) ;
5152 waitDevServerReadyStub = sandbox . stub ( ) ;
53+ probeServerStub = sandbox . stub ( ) ;
5254 findCwdStub = sandbox . stub ( ) ;
5355
54- loggerStub = { log : sandbox . stub ( ) } ;
56+ loggerStub = { log : sandbox . stub ( ) , warn : sandbox . stub ( ) } ;
5557 debugLog = sandbox . stub ( ) ;
5658
5759 devServer = proxyquire ( "src/dev-server" , {
@@ -61,6 +63,7 @@ describe("dev-server", () => {
6163 "./utils" : {
6264 pipeLogsWithPrefix : pipeLogsWithPrefixStub ,
6365 waitDevServerReady : waitDevServerReadyStub ,
66+ probeServer : probeServerStub ,
6467 findCwd : findCwdStub ,
6568 } ,
6669 } ) ;
@@ -169,4 +172,188 @@ describe("dev-server", () => {
169172 assert . calledWith ( debugLog , "Dev server env:" , JSON . stringify ( { bar : "baz" } , null , 4 ) ) ;
170173 } ) ;
171174 } ) ;
175+
176+ describe ( "reuseExisting" , ( ) => {
177+ it ( "should throw error when reuseExisting is true but readinessProbe is a function" , async ( ) => {
178+ const readinessProbe = sandbox . stub ( ) ;
179+
180+ try {
181+ await initDevServer_ ( {
182+ command : "foo" ,
183+ reuseExisting : true ,
184+ readinessProbe,
185+ } ) ;
186+ assert . fail ( "Expected error to be thrown" ) ;
187+ } catch ( error ) {
188+ assert . match (
189+ ( error as Error ) . message ,
190+ / W h e n ' r e u s e E x i s t i n g ' i s s e t t o ' t r u e ' i n ' d e v S e r v e r ' c o n f i g , i t i s r e q u i r e d t o s e t ' d e v S e r v e r .r e a d i n e s s P r o b e .u r l ' / ,
191+ ) ;
192+ }
193+
194+ assert . notCalled ( spawnStub ) ;
195+ assert . notCalled ( probeServerStub ) ;
196+ } ) ;
197+
198+ it ( "should throw error when reuseExisting is true but readinessProbe.url is not set" , async ( ) => {
199+ try {
200+ await initDevServer_ ( {
201+ command : "foo" ,
202+ reuseExisting : true ,
203+ readinessProbe : {
204+ url : null ,
205+ isReady : null ,
206+ timeouts : {
207+ waitServerTimeout : 30000 ,
208+ probeRequestTimeout : 1000 ,
209+ probeRequestInterval : 500 ,
210+ } ,
211+ } ,
212+ } ) ;
213+ assert . fail ( "Expected error to be thrown" ) ;
214+ } catch ( error ) {
215+ assert . match (
216+ ( error as Error ) . message ,
217+ / W h e n ' r e u s e E x i s t i n g ' i s s e t t o ' t r u e ' i n ' d e v S e r v e r ' c o n f i g , i t i s r e q u i r e d t o s e t ' d e v S e r v e r .r e a d i n e s s P r o b e .u r l ' / ,
218+ ) ;
219+ }
220+
221+ assert . notCalled ( spawnStub ) ;
222+ assert . notCalled ( probeServerStub ) ;
223+ } ) ;
224+
225+ it ( "should reuse existing server when reuseExisting is true and server is ready" , async ( ) => {
226+ probeServerStub . resolves ( true ) ;
227+
228+ await initDevServer_ ( {
229+ command : "foo" ,
230+ reuseExisting : true ,
231+ readinessProbe : {
232+ url : "http://localhost:3000" ,
233+ isReady : null ,
234+ timeouts : {
235+ waitServerTimeout : 30000 ,
236+ probeRequestTimeout : 1000 ,
237+ probeRequestInterval : 500 ,
238+ } ,
239+ } ,
240+ } ) ;
241+
242+ assert . calledOnceWith ( probeServerStub , {
243+ url : "http://localhost:3000" ,
244+ isReady : null ,
245+ timeouts : {
246+ waitServerTimeout : 30000 ,
247+ probeRequestTimeout : 1000 ,
248+ probeRequestInterval : 500 ,
249+ } ,
250+ } ) ;
251+ assert . calledWith ( loggerStub . log , "Reusing existing dev server" ) ;
252+ assert . notCalled ( spawnStub ) ;
253+ assert . notCalled ( waitDevServerReadyStub ) ;
254+ } ) ;
255+
256+ it ( "should start new server when reuseExisting is true but server is not ready" , async ( ) => {
257+ probeServerStub . resolves ( false ) ;
258+
259+ await initDevServer_ ( {
260+ command : "foo" ,
261+ reuseExisting : true ,
262+ readinessProbe : {
263+ url : "http://localhost:3000" ,
264+ isReady : null ,
265+ timeouts : {
266+ waitServerTimeout : 30000 ,
267+ probeRequestTimeout : 1000 ,
268+ probeRequestInterval : 500 ,
269+ } ,
270+ } ,
271+ } ) ;
272+
273+ assert . calledOnceWith ( probeServerStub , {
274+ url : "http://localhost:3000" ,
275+ isReady : null ,
276+ timeouts : {
277+ waitServerTimeout : 30000 ,
278+ probeRequestTimeout : 1000 ,
279+ probeRequestInterval : 500 ,
280+ } ,
281+ } ) ;
282+ assert . calledWith ( loggerStub . log , "Starting dev server with command" , '"foo"' ) ;
283+ assert . calledOnceWith ( spawnStub , "foo" ) ;
284+ assert . calledOnce ( waitDevServerReadyStub ) ;
285+ } ) ;
286+
287+ it ( "should work with custom isReady function when reusing existing server" , async ( ) => {
288+ probeServerStub . resolves ( true ) ;
289+ const customIsReady = sandbox . stub ( ) ;
290+
291+ await initDevServer_ ( {
292+ command : "foo" ,
293+ reuseExisting : true ,
294+ readinessProbe : {
295+ url : "http://localhost:3000" ,
296+ isReady : customIsReady ,
297+ timeouts : {
298+ waitServerTimeout : 30000 ,
299+ probeRequestTimeout : 1000 ,
300+ probeRequestInterval : 500 ,
301+ } ,
302+ } ,
303+ } ) ;
304+
305+ assert . calledOnceWith ( probeServerStub , {
306+ url : "http://localhost:3000" ,
307+ isReady : customIsReady ,
308+ timeouts : {
309+ waitServerTimeout : 30000 ,
310+ probeRequestTimeout : 1000 ,
311+ probeRequestInterval : 500 ,
312+ } ,
313+ } ) ;
314+ assert . calledWith ( loggerStub . log , "Reusing existing dev server" ) ;
315+ assert . notCalled ( spawnStub ) ;
316+ } ) ;
317+
318+ it ( "should not probe server when reuseExisting is false" , async ( ) => {
319+ await initDevServer_ ( {
320+ command : "foo" ,
321+ reuseExisting : false ,
322+ readinessProbe : {
323+ url : "http://localhost:3000" ,
324+ isReady : null ,
325+ timeouts : {
326+ waitServerTimeout : 30000 ,
327+ probeRequestTimeout : 1000 ,
328+ probeRequestInterval : 500 ,
329+ } ,
330+ } ,
331+ } ) ;
332+
333+ assert . notCalled ( probeServerStub ) ;
334+ assert . calledWith ( loggerStub . log , "Starting dev server with command" , '"foo"' ) ;
335+ assert . calledOnceWith ( spawnStub , "foo" ) ;
336+ assert . calledOnce ( waitDevServerReadyStub ) ;
337+ } ) ;
338+
339+ it ( "should not probe server when reuseExisting is not set (default false)" , async ( ) => {
340+ await initDevServer_ ( {
341+ command : "foo" ,
342+ readinessProbe : {
343+ url : "http://localhost:3000" ,
344+ isReady : null ,
345+ timeouts : {
346+ waitServerTimeout : 30000 ,
347+ probeRequestTimeout : 1000 ,
348+ probeRequestInterval : 500 ,
349+ } ,
350+ } ,
351+ } ) ;
352+
353+ assert . notCalled ( probeServerStub ) ;
354+ assert . calledWith ( loggerStub . log , "Starting dev server with command" , '"foo"' ) ;
355+ assert . calledOnceWith ( spawnStub , "foo" ) ;
356+ assert . calledOnce ( waitDevServerReadyStub ) ;
357+ } ) ;
358+ } ) ;
172359} ) ;
0 commit comments