Skip to content

Commit bd21310

Browse files
committed
fix(test): save/restore SENTRY_CONFIG_DIR to prevent cross-file test pollution
Three test files (version-check, install-info, project-cache) were deleting process.env.SENTRY_CONFIG_DIR in afterEach without saving the original value set by preload.ts. When Bun's test runner shares a process across files, this poisons the env for subsequent tests that capture the var at module load time.
1 parent 76b8c09 commit bd21310

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
@@ -47,9 +47,11 @@ describe("shouldSuppressNotification", () => {
4747

4848
describe("getUpdateNotification", () => {
4949
let testConfigDir: string;
50+
let savedConfigDir: string | undefined;
5051
let savedNoUpdateCheck: 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;
@@ -116,9 +122,11 @@ describe("abortPendingVersionCheck", () => {
116122

117123
describe("maybeCheckForUpdateInBackground", () => {
118124
let testConfigDir: string;
125+
let savedConfigDir: string | undefined;
119126
let savedNoUpdateCheck: 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)