-
Notifications
You must be signed in to change notification settings - Fork 37
Fix: Clean up temporary test directories after each test #518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: Clean up temporary test directories after each test #518
Conversation
Store temporary directory paths in global scope to survive jest.resetModules() calls. This ensures all temporary directories created by testSettings() are properly cleaned up after tests complete. Co-authored-by: manusa <488391+manusa@users.noreply.github.com>
Add test suite to verify that testSettings() creates temporary directories correctly and they are cleaned up after tests complete. Co-authored-by: manusa <488391+manusa@users.noreply.github.com>
Add test case that specifically reproduces the original issue where jest.resetModules() would prevent cleanup. Verifies that the fix using global scope works correctly. Co-authored-by: manusa <488391+manusa@users.noreply.github.com>
src/__tests__/setup-jest.js
Outdated
|
|
||
| // Clean up the current settings appDir if it's in tmpdir | ||
| if (os.tmpdir && settings?.paths?.appDir && settings.paths.appDir.startsWith(os.tmpdir())) { | ||
| await fs.promises.rm(settings.paths.appDir, {recursive: true, force: true}); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should no longer be needed since any usage of testSettings will register the directory
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed in 76c1532. Since testSettings() now tracks all directories in the global array, this redundant cleanup is no longer needed.
src/__tests__/settings.test.js
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't need this file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed in 76c1532.
src/__tests__/settings.js
Outdated
| const settings = require('../settings'); | ||
| settings.paths.appDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'electronim-test-')); | ||
| settings.paths.settingsPath = path.join(settings.paths.appDir, 'settings.json'); | ||
| // Track the directory for cleanup in global scope |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for this comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed in 76c1532.
src/__tests__/setup-jest.js
Outdated
| // Clean up all tracked temporary directories created by testSettings() | ||
| // Using global scope to survive jest.resetModules() calls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| // Clean up all tracked temporary directories created by testSettings() | |
| // Using global scope to survive jest.resetModules() calls | |
| // Clean up all tracked temporary directories |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated in 76c1532.
manusa
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot Mostly looking goof except for the comments
- Remove redundant cleanup logic for current settings appDir since testSettings tracks all directories - Remove settings.test.js file as requested - Remove unnecessary comment from settings.js - Simplify comment in setup-jest.js Co-authored-by: manusa <488391+manusa@users.noreply.github.com>
manusa
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM,thx!
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a resource leak issue where temporary test directories created by the testSettings() function were not being cleaned up after test execution, particularly when jest.resetModules() cleared the module cache and lost track of directory paths.
- Stores temporary directory paths in a global array that survives module resets
- Implements cleanup logic in the Jest teardown to remove all tracked directories
- Ensures proper resource cleanup even when the module cache is cleared during tests
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/__tests__/settings.js |
Adds global tracking of temporary directory paths to survive module cache resets |
src/__tests__/setup-jest.js |
Implements cleanup logic to remove all tracked temporary directories after each test |


✅ COMPLETE - Temporary directory cleanup issue resolved
Problem Statement
The
testSettings()function creates temporary directories that were not being removed after test completion, especially when tests calledjest.resetModules().Root Cause
When
jest.resetModules()is called, the module cache is cleared. The cleanup logic insetup-jest.jswould then load a fresh settings module instance with default paths, losing track of the temporary directory path.Solution
Store temporary directory paths in
global.__testTempDirectories__array, which persists across module resets.Changes Made
src/__tests__/settings.js- Track directories in global scope that survivesjest.resetModules()src/__tests__/setup-jest.js- Clean up all tracked directories from the global arrayVerification Results
jest.resetModules()correctlyOriginal prompt
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.