preload.ts
| 1 | // Runs before any test module is evaluated (see the --preload flag in the |
| 2 | // "test" script). |
| 3 | // |
| 4 | // src/db/index.ts opens the database and runs migrations in its module body, |
| 5 | // and src/config.ts reads DATA_DIR in its own. Both happen while imports are |
| 6 | // still being hoisted, i.e. before any statement in a test file or in |
| 7 | // helpers.ts can assign to config. Pointing DATA_DIR at a per-worker path |
| 8 | // here is the only way to get in front of that — otherwise every parallel |
| 9 | // worker opens the real ./data/hearthforge.db and they contend for the write |
| 10 | // lock ("SQLiteError: database is locked"). |
| 11 | const worker = process.env.BUN_TEST_WORKER_ID; |
| 12 | process.env.DATA_DIR = worker ? `./data-test-${worker}` : "./data-test"; |
| 13 | |
| 14 | // One highlight worker per test process is plenty, and the pool is only |
| 15 | // built if a test actually highlights something. |
| 16 | process.env.HIGHLIGHT_WORKERS ??= "1"; |
| 17 |