@@ -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,134 @@ describe("dev-server", () => {
169172 assert . calledWith ( debugLog , "Dev server env:" , JSON . stringify ( { bar : "baz" } , null , 4 ) ) ;
170173 } ) ;
171174 } ) ;
175+
176+ describe ( "reuseExisting" , ( ) => {
177+ const defaultReadinessProbe = {
178+ url : "http://localhost:3000" ,
179+ isReady : null ,
180+ timeouts : {
181+ waitServerTimeout : 30000 ,
182+ probeRequestTimeout : 1000 ,
183+ probeRequestInterval : 500 ,
184+ } ,
185+ } ;
186+
187+ it ( "should throw error when reuseExisting is true but readinessProbe is a function" , async ( ) => {
188+ const readinessProbe = sandbox . stub ( ) ;
189+
190+ try {
191+ await initDevServer_ ( {
192+ command : "foo" ,
193+ reuseExisting : true ,
194+ readinessProbe,
195+ } ) ;
196+ assert . fail ( "Expected error to be thrown" ) ;
197+ } catch ( error ) {
198+ assert . match (
199+ ( error as Error ) . message ,
200+ / 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 ' / ,
201+ ) ;
202+ }
203+
204+ assert . notCalled ( spawnStub ) ;
205+ assert . notCalled ( probeServerStub ) ;
206+ } ) ;
207+
208+ it ( "should throw error when reuseExisting is true but readinessProbe.url is not set" , async ( ) => {
209+ try {
210+ await initDevServer_ ( {
211+ command : "foo" ,
212+ reuseExisting : true ,
213+ readinessProbe : {
214+ ...defaultReadinessProbe ,
215+ url : null ,
216+ } ,
217+ } ) ;
218+ assert . fail ( "Expected error to be thrown" ) ;
219+ } catch ( error ) {
220+ assert . match (
221+ ( error as Error ) . message ,
222+ / 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 ' / ,
223+ ) ;
224+ }
225+
226+ assert . notCalled ( spawnStub ) ;
227+ assert . notCalled ( probeServerStub ) ;
228+ } ) ;
229+
230+ it ( "should reuse existing server when reuseExisting is true and server is ready" , async ( ) => {
231+ probeServerStub . resolves ( true ) ;
232+
233+ await initDevServer_ ( {
234+ command : "foo" ,
235+ reuseExisting : true ,
236+ readinessProbe : defaultReadinessProbe ,
237+ } ) ;
238+
239+ assert . calledOnceWith ( probeServerStub , defaultReadinessProbe ) ;
240+ assert . calledWith ( loggerStub . log , "Reusing existing dev server" ) ;
241+ assert . notCalled ( spawnStub ) ;
242+ assert . notCalled ( waitDevServerReadyStub ) ;
243+ } ) ;
244+
245+ it ( "should start new server when reuseExisting is true but server is not ready" , async ( ) => {
246+ probeServerStub . resolves ( false ) ;
247+
248+ await initDevServer_ ( {
249+ command : "foo" ,
250+ reuseExisting : true ,
251+ readinessProbe : defaultReadinessProbe ,
252+ } ) ;
253+
254+ assert . calledOnceWith ( probeServerStub , defaultReadinessProbe ) ;
255+ assert . calledWith ( loggerStub . log , "Starting dev server with command" , '"foo"' ) ;
256+ assert . calledOnceWith ( spawnStub , "foo" ) ;
257+ assert . calledOnce ( waitDevServerReadyStub ) ;
258+ } ) ;
259+
260+ it ( "should work with custom isReady function when reusing existing server" , async ( ) => {
261+ probeServerStub . resolves ( true ) ;
262+ const customIsReady = sandbox . stub ( ) ;
263+
264+ const customReadinessProbe = {
265+ ...defaultReadinessProbe ,
266+ isReady : customIsReady ,
267+ } ;
268+
269+ await initDevServer_ ( {
270+ command : "foo" ,
271+ reuseExisting : true ,
272+ readinessProbe : customReadinessProbe ,
273+ } ) ;
274+
275+ assert . calledOnceWith ( probeServerStub , customReadinessProbe ) ;
276+ assert . calledWith ( loggerStub . log , "Reusing existing dev server" ) ;
277+ assert . notCalled ( spawnStub ) ;
278+ } ) ;
279+
280+ it ( "should not probe server when reuseExisting is false" , async ( ) => {
281+ await initDevServer_ ( {
282+ command : "foo" ,
283+ reuseExisting : false ,
284+ readinessProbe : defaultReadinessProbe ,
285+ } ) ;
286+
287+ assert . notCalled ( probeServerStub ) ;
288+ assert . calledWith ( loggerStub . log , "Starting dev server with command" , '"foo"' ) ;
289+ assert . calledOnceWith ( spawnStub , "foo" ) ;
290+ assert . calledOnce ( waitDevServerReadyStub ) ;
291+ } ) ;
292+
293+ it ( "should not probe server when reuseExisting is not set (default false)" , async ( ) => {
294+ await initDevServer_ ( {
295+ command : "foo" ,
296+ readinessProbe : defaultReadinessProbe ,
297+ } ) ;
298+
299+ assert . notCalled ( probeServerStub ) ;
300+ assert . calledWith ( loggerStub . log , "Starting dev server with command" , '"foo"' ) ;
301+ assert . calledOnceWith ( spawnStub , "foo" ) ;
302+ assert . calledOnce ( waitDevServerReadyStub ) ;
303+ } ) ;
304+ } ) ;
172305} ) ;
0 commit comments