From 8a522377c8817a28f56762b84792fb1b6d809bfb Mon Sep 17 00:00:00 2001 From: Ryan Mwangi Date: Wed, 6 Nov 2024 15:23:30 +0300 Subject: [PATCH] fix: Improve cleanup process in test suite - Ensured the server is properly closed before executing cleanup. - Added a delay to allow for the release of any lingering file handles. - Enhanced the cleanup logic to safely remove the merged calendars directory after tests. - Prevented potential EBUSY errors during directory removal. --- calendar.test.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/calendar.test.js b/calendar.test.js index c7a062d..e2bf417 100644 --- a/calendar.test.js +++ b/calendar.test.js @@ -17,8 +17,10 @@ describe('Calendar Merging API', () => { beforeAll(async () => { // Change the working directory to the test-specific directory process.chdir(path.join(__dirname, 'temp_test_calendar')); + // Start the server server = app.listen(0); + // Ensure the test merged calendars directory exists if (!fs.existsSync(TEST_MERGED_CALENDARS_DIR)) { fs.mkdirSync(TEST_MERGED_CALENDARS_DIR, { recursive: true }); @@ -26,13 +28,16 @@ describe('Calendar Merging API', () => { }); afterAll(async () => { - // // Clean up the merged calendars directory after tests - // if (fs.existsSync(TEST_MERGED_CALENDARS_DIR)) { - // fs.rmSync(TEST_MERGED_CALENDARS_DIR, { recursive: true, force: true }); - // } - - // Close the server - await new Promise(resolve => server.close(resolve)); + // Ensure the server is closed before cleanup + await new Promise(resolve => server.close(resolve)); + + // Optional: Add a delay to ensure all handles are released + await new Promise(resolve => setTimeout(resolve, 100)); + + // Clean up the merged calendars directory after tests + if (fs.existsSync(TEST_MERGED_CALENDARS_DIR)) { + fs.rmSync(TEST_MERGED_CALENDARS_DIR, { recursive: true, force: true }); + } }); const loadCalendarFile = (filename) => {