Skip to content

Commit f299338

Browse files
committed
fix(test): restore SENTRY_CONFIG_DIR in afterEach instead of deleting
Three test files deleted process.env.SENTRY_CONFIG_DIR in afterEach without restoring the value set by preload.ts. Since Bun runs test files in parallel, this caused a race condition: other test files that capture the env var at module scope would get undefined if their module loaded after one of these deletes ran. Save the original value in beforeEach and restore it in afterEach, matching the pattern already used by schema.test.ts and fix.test.ts.
1 parent d010122 commit f299338

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

test/lib/db/install-info.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,21 @@ import {
1212
import { cleanupTestDir, createTestConfigDir } from "../../helpers.js";
1313

1414
let testConfigDir: string;
15+
let savedConfigDir: string | undefined;
1516

1617
beforeEach(async () => {
18+
savedConfigDir = process.env.SENTRY_CONFIG_DIR;
1719
testConfigDir = await createTestConfigDir("test-install-info-");
1820
process.env.SENTRY_CONFIG_DIR = testConfigDir;
1921
});
2022

2123
afterEach(async () => {
2224
closeDatabase();
23-
delete process.env.SENTRY_CONFIG_DIR;
25+
if (savedConfigDir !== undefined) {
26+
process.env.SENTRY_CONFIG_DIR = savedConfigDir;
27+
} else {
28+
delete process.env.SENTRY_CONFIG_DIR;
29+
}
2430
await cleanupTestDir(testConfigDir);
2531
});
2632

test/lib/db/project-cache.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,22 @@ import {
1616
import { cleanupTestDir, createTestConfigDir } from "../../helpers.js";
1717

1818
let testConfigDir: string;
19+
let savedConfigDir: string | undefined;
1920

2021
beforeEach(async () => {
22+
savedConfigDir = process.env.SENTRY_CONFIG_DIR;
2123
testConfigDir = await createTestConfigDir("test-project-cache-");
2224
process.env.SENTRY_CONFIG_DIR = testConfigDir;
2325
});
2426

2527
afterEach(async () => {
2628
// Close database to release file handles before cleanup
2729
closeDatabase();
28-
delete process.env.SENTRY_CONFIG_DIR;
30+
if (savedConfigDir !== undefined) {
31+
process.env.SENTRY_CONFIG_DIR = savedConfigDir;
32+
} else {
33+
delete process.env.SENTRY_CONFIG_DIR;
34+
}
2935
await cleanupTestDir(testConfigDir);
3036
});
3137

test/lib/version-check.test.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ describe("shouldSuppressNotification", () => {
4848
describe("getUpdateNotification", () => {
4949
let testConfigDir: string;
5050
let savedNoUpdateCheck: string | undefined;
51+
let savedConfigDir: string | undefined;
5152

5253
beforeEach(async () => {
54+
savedConfigDir = process.env.SENTRY_CONFIG_DIR;
5355
testConfigDir = await createTestConfigDir("test-version-notif-");
5456
process.env.SENTRY_CONFIG_DIR = testConfigDir;
5557
// Save and clear the env var to test real implementation
@@ -58,7 +60,11 @@ describe("getUpdateNotification", () => {
5860
});
5961

6062
afterEach(async () => {
61-
delete process.env.SENTRY_CONFIG_DIR;
63+
if (savedConfigDir !== undefined) {
64+
process.env.SENTRY_CONFIG_DIR = savedConfigDir;
65+
} else {
66+
delete process.env.SENTRY_CONFIG_DIR;
67+
}
6268
// Restore the env var
6369
if (savedNoUpdateCheck !== undefined) {
6470
process.env.SENTRY_CLI_NO_UPDATE_CHECK = savedNoUpdateCheck;
@@ -117,8 +123,10 @@ describe("abortPendingVersionCheck", () => {
117123
describe("maybeCheckForUpdateInBackground", () => {
118124
let testConfigDir: string;
119125
let savedNoUpdateCheck: string | undefined;
126+
let savedConfigDir: string | undefined;
120127

121128
beforeEach(async () => {
129+
savedConfigDir = process.env.SENTRY_CONFIG_DIR;
122130
testConfigDir = await createTestConfigDir("test-version-bg-");
123131
process.env.SENTRY_CONFIG_DIR = testConfigDir;
124132
// Save and clear the env var to test real implementation
@@ -129,7 +137,11 @@ describe("maybeCheckForUpdateInBackground", () => {
129137
afterEach(async () => {
130138
// Abort any pending check to clean up
131139
abortPendingVersionCheck();
132-
delete process.env.SENTRY_CONFIG_DIR;
140+
if (savedConfigDir !== undefined) {
141+
process.env.SENTRY_CONFIG_DIR = savedConfigDir;
142+
} else {
143+
delete process.env.SENTRY_CONFIG_DIR;
144+
}
133145
// Restore the env var
134146
if (savedNoUpdateCheck !== undefined) {
135147
process.env.SENTRY_CLI_NO_UPDATE_CHECK = savedNoUpdateCheck;

0 commit comments

Comments
 (0)