Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/__tests__/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
limitations under the License.
*/

// Store temporary directories created during tests in global scope
// to ensure cleanup even when jest.resetModules() clears the module cache
if (!global.__testTempDirectories__) {
global.__testTempDirectories__ = [];
}

/**
* Creates a test-isolated settings module with temporary directory overrides.
*
Expand All @@ -30,6 +36,7 @@ const testSettings = async () => {
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');
global.__testTempDirectories__.push(settings.paths.appDir);
return settings;
};

Expand Down
15 changes: 11 additions & 4 deletions src/__tests__/setup-jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@
*/
afterEach(async () => {
jest.useRealTimers();
const os = require('node:os');
const fs = require('node:fs');
const settings = require('../settings');
if (os.tmpdir && settings?.paths?.appDir && settings.paths.appDir.startsWith(os.tmpdir())) {
await fs.promises.rm(settings.paths.appDir, {recursive: true, force: true});

// Clean up all tracked temporary directories
if (global.__testTempDirectories__) {
while (global.__testTempDirectories__.length > 0) {
const dir = global.__testTempDirectories__.pop();
try {
await fs.promises.rm(dir, {recursive: true, force: true});
} catch {
// Ignore errors if directory was already cleaned up
}
}
}
});