Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Sep 30, 2025

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 called jest.resetModules().

Root Cause

When jest.resetModules() is called, the module cache is cleared. The cleanup logic in setup-jest.js would 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

  1. src/__tests__/settings.js - Track directories in global scope that survives jest.resetModules()
  2. src/__tests__/setup-jest.js - Clean up all tracked directories from the global array

Verification Results

  • ✅ All 627 tests pass
  • ✅ No temporary directories remain after test execution
  • ✅ Cleanup works consistently across multiple test runs
  • ✅ Solution handles jest.resetModules() correctly
  • ✅ Linting passes
Original prompt

The testSettings function in src/tests/settings.js creates temporary directories to hold the settings.json file for tests.
This is working great, however, these directories are not removed.
It would be great if the directories were automatically cleared and removed once the test, the test suite, or the test runner (whatever is more conventient or easy to implement) completes its execution.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 3 commits September 30, 2025 12:42
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>
Comment on lines 21 to 25

// 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});
}
Copy link
Owner

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

Copy link
Contributor Author

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.

Copilot AI changed the title The testSettings function in src/__tests__/settings.js creates temporary directories to hold the settings.json file for tests. This is working great, however, these directories are not removed. It would be great if the directories were automatically cl... Fix: Clean up temporary test directories after each test Sep 30, 2025
Copilot AI requested a review from manusa September 30, 2025 12:48
Copilot finished work on behalf of manusa September 30, 2025 12:48
Copy link
Owner

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in 76c1532.

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
Copy link
Owner

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in 76c1532.

Comment on lines 27 to 28
// Clean up all tracked temporary directories created by testSettings()
// Using global scope to survive jest.resetModules() calls
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Clean up all tracked temporary directories created by testSettings()
// Using global scope to survive jest.resetModules() calls
// Clean up all tracked temporary directories

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in 76c1532.

Copy link
Owner

@manusa manusa left a 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>
Copilot AI requested a review from manusa September 30, 2025 12:55
Copilot finished work on behalf of manusa September 30, 2025 12:55
Copy link
Owner

@manusa manusa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM,thx!

@manusa manusa added this to the 0.1.0 milestone Sep 30, 2025
@sonarqubecloud
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
50.0% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

@manusa manusa marked this pull request as ready for review September 30, 2025 13:09
Copilot AI review requested due to automatic review settings September 30, 2025 13:09
Copy link
Contributor

Copilot AI left a 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

@manusa manusa merged commit 379cceb into main Sep 30, 2025
9 of 10 checks passed
@manusa manusa deleted the copilot/fix-1d257feb-6232-47ca-bff2-90cc326795f7 branch September 30, 2025 13:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants