@@ -322,62 +322,74 @@ describe('init()', () => {
322322 } ) ;
323323 } ) ;
324324
325- it ( 'registers a SIGTERM handler on Vercel' , ( ) => {
326- const originalVercelEnv = process . env . VERCEL ;
327- process . env . VERCEL = '1' ;
325+ describe ( 'Vercel keep-alive' , ( ) => {
326+ let vercelInit : typeof init ;
327+ let VercelNodeClient : typeof NodeClient ;
328+
329+ beforeEach ( async ( ) => {
330+ // The keep-alive listeners are registered once per module instance, so each test needs a fresh one.
331+ vi . resetModules ( ) ;
332+ ( { init : vercelInit } = await import ( '../../src/sdk' ) ) ;
333+ ( { NodeClient : VercelNodeClient } = await import ( '../../src/sdk/client' ) ) ;
334+ } ) ;
328335
329- const baselineListeners = process . listeners ( 'SIGTERM' ) ;
336+ it ( 'registers a SIGTERM handler on Vercel' , ( ) => {
337+ const originalVercelEnv = process . env . VERCEL ;
338+ process . env . VERCEL = '1' ;
330339
331- init ( { dsn : PUBLIC_DSN , enableOpenTelemetrySetup : false } ) ;
340+ const baselineListeners = process . listeners ( 'SIGTERM' ) ;
332341
333- const postInitListeners = process . listeners ( 'SIGTERM' ) ;
334- const addedListeners = postInitListeners . filter ( l => ! baselineListeners . includes ( l ) ) ;
342+ vercelInit ( { dsn : PUBLIC_DSN , enableOpenTelemetrySetup : false } ) ;
335343
336- expect ( addedListeners ) . toHaveLength ( 1 ) ;
344+ const postInitListeners = process . listeners ( 'SIGTERM' ) ;
345+ const addedListeners = postInitListeners . filter ( l => ! baselineListeners . includes ( l ) ) ;
337346
338- // Cleanup: remove the handler we added in this test.
339- process . off ( 'SIGTERM' , addedListeners [ 0 ] as any ) ;
340- process . env . VERCEL = originalVercelEnv ;
341- } ) ;
347+ expect ( addedListeners ) . toHaveLength ( 1 ) ;
342348
343- it ( 'flushes when SIGTERM is received on Vercel' , ( ) => {
344- const originalVercelEnv = process . env . VERCEL ;
345- process . env . VERCEL = '1' ;
349+ // Cleanup: remove the handler we added in this test.
350+ process . off ( 'SIGTERM' , addedListeners [ 0 ] as any ) ;
351+ process . env . VERCEL = originalVercelEnv ;
352+ } ) ;
346353
347- const baselineListeners = process . listeners ( 'SIGTERM' ) ;
354+ it ( 'flushes when SIGTERM is received on Vercel' , ( ) => {
355+ const originalVercelEnv = process . env . VERCEL ;
356+ process . env . VERCEL = '1' ;
348357
349- const client = init ( { dsn : PUBLIC_DSN , enableOpenTelemetrySetup : false } ) ;
350- expect ( client ) . toBeInstanceOf ( NodeClient ) ;
358+ const baselineListeners = process . listeners ( 'SIGTERM' ) ;
351359
352- const flushSpy = vi . spyOn ( client as NodeClient , 'flush' ) . mockResolvedValue ( true ) ;
360+ const client = vercelInit ( { dsn : PUBLIC_DSN , enableOpenTelemetrySetup : false } ) ;
361+ expect ( client ) . toBeInstanceOf ( VercelNodeClient ) ;
353362
354- const postInitListeners = process . listeners ( 'SIGTERM' ) ;
355- const addedListeners = postInitListeners . filter ( l => ! baselineListeners . includes ( l ) ) ;
356- expect ( addedListeners ) . toHaveLength ( 1 ) ;
363+ const flushSpy = vi . spyOn ( client as InstanceType < typeof VercelNodeClient > , 'flush' ) . mockResolvedValue ( true ) ;
357364
358- process . emit ( 'SIGTERM' ) ;
365+ const postInitListeners = process . listeners ( 'SIGTERM' ) ;
366+ const addedListeners = postInitListeners . filter ( l => ! baselineListeners . includes ( l ) ) ;
367+ expect ( addedListeners ) . toHaveLength ( 1 ) ;
359368
360- expect ( flushSpy ) . toHaveBeenCalledWith ( 200 ) ;
369+ process . emit ( 'SIGTERM' ) ;
361370
362- // Cleanup: remove the handler we added in this test.
363- process . off ( 'SIGTERM' , addedListeners [ 0 ] as any ) ;
364- process . env . VERCEL = originalVercelEnv ;
365- } ) ;
371+ expect ( flushSpy ) . toHaveBeenCalledWith ( 200 ) ;
372+
373+ // Cleanup: remove the handler we added in this test.
374+ process . off ( 'SIGTERM' , addedListeners [ 0 ] as any ) ;
375+ process . env . VERCEL = originalVercelEnv ;
376+ } ) ;
366377
367- it ( 'does not register a SIGTERM handler when not running on Vercel' , ( ) => {
368- const originalVercelEnv = process . env . VERCEL ;
369- delete process . env . VERCEL ;
378+ it ( 'does not register a SIGTERM handler when not running on Vercel' , ( ) => {
379+ const originalVercelEnv = process . env . VERCEL ;
380+ delete process . env . VERCEL ;
370381
371- const baselineListeners = process . listeners ( 'SIGTERM' ) ;
382+ const baselineListeners = process . listeners ( 'SIGTERM' ) ;
372383
373- init ( { dsn : PUBLIC_DSN , enableOpenTelemetrySetup : false } ) ;
384+ vercelInit ( { dsn : PUBLIC_DSN , enableOpenTelemetrySetup : false } ) ;
374385
375- const postInitListeners = process . listeners ( 'SIGTERM' ) ;
376- const addedListeners = postInitListeners . filter ( l => ! baselineListeners . includes ( l ) ) ;
386+ const postInitListeners = process . listeners ( 'SIGTERM' ) ;
387+ const addedListeners = postInitListeners . filter ( l => ! baselineListeners . includes ( l ) ) ;
377388
378- expect ( addedListeners ) . toHaveLength ( 0 ) ;
389+ expect ( addedListeners ) . toHaveLength ( 0 ) ;
379390
380- process . env . VERCEL = originalVercelEnv ;
391+ process . env . VERCEL = originalVercelEnv ;
392+ } ) ;
381393 } ) ;
382394
383395 describe ( 'environment variable options' , ( ) => {
0 commit comments