Commit baaa3d3
authored
fix(tests): centralize test config dir lifecycle to prevent env var pollution (#242)
## Summary
Fixes 37 test failures caused by the Ubuntu 24.04 runner image upgrade
(`20260201.15.1` → `20260209.23.1`, kernel 6.11 → 6.14) that changed
`readdir` ordering, exposing a latent env var pollution bug.
## Root Cause
Bun runs test files **sequentially in one thread** (load → run all tests
→ load next file). Several test files unconditionally `delete
process.env.SENTRY_CONFIG_DIR` in their `afterEach` hooks. When the next
file loads, its module-level code captures `undefined`, causing
`join(undefined, ...)` → `TypeError`.
The runner image upgrade changed the kernel which changed file discovery
order, putting "deleting" files before "capturing" files.
## Changes
- **`test/helpers.ts`**: Added `useTestConfigDir()` helper that creates
a unique temp directory in `beforeEach`, points `SENTRY_CONFIG_DIR` at
it, and **restores** (never deletes) the original value in `afterEach`.
Also handles `closeDatabase()` and temp dir cleanup.
- **11 test files migrated** to use `useTestConfigDir()`, eliminating:
- 3 files that unconditionally deleted `SENTRY_CONFIG_DIR` (root cause)
- 3 files with fragile module-level `process.env[CONFIG_DIR_ENV_VAR]!`
captures
- 2 files with `if (original) restore else delete` patterns (off-by-one
on falsy check)
- 2 files missing env var restore in `afterEach`
- **`AGENTS.md`**: Added "Test Environment Isolation" section
documenting the pattern and anti-patterns
Net: **-87 lines** (157 added, 244 removed) — less boilerplate, more
safety.
## Verification
- `bun run typecheck` — passes
- `bun run lint` — passes
- `bun run test:unit` — **1545 tests pass, 0 failures**
Supercedes #2401 parent 35d9b0d commit baaa3d3
File tree
16 files changed
+200
-253
lines changed- src/lib
- test
- commands
- cli
- issue
- isolated/dsn
- lib
- db
16 files changed
+200
-253
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
383 | 383 | | |
384 | 384 | | |
385 | 385 | | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
386 | 417 | | |
387 | 418 | | |
388 | 419 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
62 | | - | |
| 62 | + | |
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
27 | 30 | | |
28 | 31 | | |
29 | 32 | | |
30 | | - | |
31 | | - | |
32 | | - | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
33 | 41 | | |
34 | 42 | | |
35 | 43 | | |
| |||
85 | 93 | | |
86 | 94 | | |
87 | 95 | | |
88 | | - | |
| 96 | + | |
| 97 | + | |
89 | 98 | | |
90 | 99 | | |
91 | 100 | | |
| |||
99 | 108 | | |
100 | 109 | | |
101 | 110 | | |
102 | | - | |
| 111 | + | |
103 | 112 | | |
104 | 113 | | |
105 | 114 | | |
| |||
142 | 151 | | |
143 | 152 | | |
144 | 153 | | |
145 | | - | |
| 154 | + | |
146 | 155 | | |
147 | 156 | | |
148 | 157 | | |
| |||
313 | 322 | | |
314 | 323 | | |
315 | 324 | | |
316 | | - | |
| 325 | + | |
| 326 | + | |
317 | 327 | | |
318 | 328 | | |
319 | 329 | | |
| |||
327 | 337 | | |
328 | 338 | | |
329 | 339 | | |
330 | | - | |
| 340 | + | |
331 | 341 | | |
332 | 342 | | |
333 | 343 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | | - | |
| 6 | + | |
8 | 7 | | |
9 | 8 | | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
| 9 | + | |
14 | 10 | | |
15 | 11 | | |
16 | 12 | | |
17 | 13 | | |
18 | 14 | | |
| 15 | + | |
19 | 16 | | |
20 | 17 | | |
21 | 18 | | |
| |||
64 | 61 | | |
65 | 62 | | |
66 | 63 | | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
| 64 | + | |
102 | 65 | | |
103 | 66 | | |
104 | 67 | | |
105 | 68 | | |
106 | | - | |
| 69 | + | |
107 | 70 | | |
108 | 71 | | |
109 | 72 | | |
| |||
124 | 87 | | |
125 | 88 | | |
126 | 89 | | |
127 | | - | |
| 90 | + | |
128 | 91 | | |
129 | 92 | | |
130 | 93 | | |
| |||
148 | 111 | | |
149 | 112 | | |
150 | 113 | | |
151 | | - | |
| 114 | + | |
152 | 115 | | |
153 | 116 | | |
154 | 117 | | |
| |||
169 | 132 | | |
170 | 133 | | |
171 | 134 | | |
172 | | - | |
| 135 | + | |
173 | 136 | | |
174 | 137 | | |
175 | 138 | | |
| |||
188 | 151 | | |
189 | 152 | | |
190 | 153 | | |
191 | | - | |
| 154 | + | |
192 | 155 | | |
193 | 156 | | |
194 | 157 | | |
| |||
210 | 173 | | |
211 | 174 | | |
212 | 175 | | |
213 | | - | |
| 176 | + | |
214 | 177 | | |
215 | 178 | | |
216 | 179 | | |
| |||
221 | 184 | | |
222 | 185 | | |
223 | 186 | | |
224 | | - | |
| 187 | + | |
225 | 188 | | |
226 | 189 | | |
227 | 190 | | |
| |||
237 | 200 | | |
238 | 201 | | |
239 | 202 | | |
240 | | - | |
| 203 | + | |
241 | 204 | | |
242 | 205 | | |
0 commit comments