@@ -15,26 +15,42 @@ const __dirname = path.dirname(__filename);
1515
1616describe ( 'Webhook Server' , ( ) => {
1717 let server ;
18- const testPort = 3002 ; // Use different port for testing
18+ let currentTestPort = 3002 ; // Base port for testing
1919 const testLogFile = path . join ( __dirname , 'temp' , 'test-webhook-logs.json' ) ;
2020
21- beforeEach ( ( ) => {
21+ // Helper function to get unique port for each test
22+ const getNextPort = ( ) => ++ currentTestPort ;
23+
24+ beforeEach ( async ( ) => {
2225 // Clean up test files
2326 if ( fs . existsSync ( testLogFile ) ) {
2427 fs . unlinkSync ( testLogFile ) ;
2528 }
29+
30+ // Ensure temp directory exists
31+ const tempDir = path . dirname ( testLogFile ) ;
32+ if ( ! fs . existsSync ( tempDir ) ) {
33+ fs . mkdirSync ( tempDir , { recursive : true } ) ;
34+ }
2635 } ) ;
2736
2837 afterEach ( async ( ) => {
2938 if ( server ) {
30- await server . stop ( ) ;
39+ try {
40+ await server . stop ( ) ;
41+ } catch ( error ) {
42+ console . warn ( 'Warning: Error stopping server:' , error . message ) ;
43+ }
3144 server = null ;
3245 }
3346
3447 // Clean up test files
3548 if ( fs . existsSync ( testLogFile ) ) {
3649 fs . unlinkSync ( testLogFile ) ;
3750 }
51+
52+ // Small delay to ensure port is released
53+ await new Promise ( resolve => setTimeout ( resolve , 100 ) ) ;
3854 } ) ;
3955
4056 describe ( 'WebhookServer class' , ( ) => {
@@ -48,15 +64,16 @@ describe('Webhook Server', () => {
4864 } ) ;
4965
5066 it ( 'should initialize with custom configuration' , ( ) => {
67+ const testPortForConfig = getNextPort ( ) ;
5168 const config = {
52- port : testPort ,
69+ port : testPortForConfig ,
5370 logFile : testLogFile ,
5471 calendlyLink : 'https://calendly.com/test'
5572 } ;
5673
5774 const webhookServer = new WebhookServer ( config ) ;
5875
59- expect ( webhookServer . config . port ) . to . equal ( testPort ) ;
76+ expect ( webhookServer . config . port ) . to . equal ( testPortForConfig ) ;
6077 expect ( webhookServer . config . logFile ) . to . equal ( testLogFile ) ;
6178 expect ( webhookServer . config . calendlyLink ) . to . equal ( 'https://calendly.com/test' ) ;
6279 } ) ;
@@ -73,7 +90,7 @@ describe('Webhook Server', () => {
7390
7491 beforeEach ( ( ) => {
7592 webhookServer = new WebhookServer ( {
76- port : testPort ,
93+ port : getNextPort ( ) ,
7794 logFile : testLogFile
7895 } ) ;
7996 } ) ;
@@ -387,21 +404,24 @@ describe('Webhook Server', () => {
387404
388405 describe ( 'Server lifecycle' , ( ) => {
389406 it ( 'should start server successfully' , async ( ) => {
390- server = new WebhookServer ( { port : testPort } ) ;
407+ const testPortForStart = getNextPort ( ) ;
408+ server = new WebhookServer ( { port : testPortForStart } ) ;
391409
392410 const startedServer = await server . start ( ) ;
393411 expect ( startedServer ) . to . exist ;
394412 } ) ;
395413
396414 it ( 'should start server with startWebhookServer function' , async ( ) => {
397- server = await startWebhookServer ( { port : testPort + 1 } ) ;
415+ const testPortForFunction = getNextPort ( ) ;
416+ server = await startWebhookServer ( { port : testPortForFunction } ) ;
398417
399418 expect ( server ) . to . be . instanceOf ( WebhookServer ) ;
400- expect ( server . config . port ) . to . equal ( testPort + 1 ) ;
419+ expect ( server . config . port ) . to . equal ( testPortForFunction ) ;
401420 } ) ;
402421
403422 it ( 'should stop server gracefully' , async ( ) => {
404- server = new WebhookServer ( { port : testPort + 2 } ) ;
423+ const testPortForStop = getNextPort ( ) ;
424+ server = new WebhookServer ( { port : testPortForStop } ) ;
405425 await server . start ( ) ;
406426
407427 // This should not throw an error
0 commit comments