Skip to content

Commit bf99986

Browse files
NateIsernclaude
andcommitted
fix(ci): let notification DB tests download mongod when none on PATH
The mongodb-memory-server harness required a system `mongod` and threw on clean CI runners (no binary on PATH → 3 DB-backed test files failed setup). Now it uses a system binary when present (offline-friendly local runs) and falls back to mongodb-memory-server's own download otherwise; CI caches the downloaded binary across runs. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 65c43b8 commit bf99986

2 files changed

Lines changed: 25 additions & 9 deletions

File tree

.github/workflows/ci.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ jobs:
2323
- name: Typecheck (frontend + server)
2424
run: bun run typecheck
2525

26+
# The notification DB tests spin up mongodb-memory-server. No system
27+
# `mongod` on the runner, so it downloads one — cache it across runs.
28+
- name: Cache mongodb-memory-server binary
29+
uses: actions/cache@v4
30+
with:
31+
path: ~/.cache/mongodb-binaries
32+
key: mongodb-binaries-${{ runner.os }}
33+
2634
- name: Test
2735
run: bun run test
2836

server/lib/notifications/test-support.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,37 @@ import mongoose from 'mongoose'
44
import { MongoMemoryServer } from 'mongodb-memory-server'
55

66
// Shared test harness: an in-memory MongoDB the notification DB tests connect
7-
// mongoose to. It uses the machine's already-installed `mongod` binary
8-
// (resolved from PATH, overridable via MONGOMS_SYSTEM_BINARY) so tests need no
9-
// network download. NOT a `*.test.ts` file, so vitest never runs it directly.
7+
// mongoose to. It prefers an already-installed `mongod` (from PATH, overridable
8+
// via MONGOMS_SYSTEM_BINARY) so offline/local runs need no download; when none
9+
// is present (e.g. a clean CI runner) it lets mongodb-memory-server download and
10+
// cache its own binary. NOT a `*.test.ts` file, so vitest never runs it directly.
1011

1112
let memoryServer: MongoMemoryServer | null = null
1213

13-
/** Locate the `mongod` executable on PATH without invoking a shell. */
14-
function resolveMongodBinary(): string {
14+
/**
15+
* Locate an already-installed `mongod` (env override, then PATH) without
16+
* invoking a shell, or return null when none is found so the caller can fall
17+
* back to mongodb-memory-server's own download.
18+
*/
19+
function resolveMongodBinary(): string | null {
1520
const fromEnv = process.env.MONGOMS_SYSTEM_BINARY
1621
if (fromEnv) return fromEnv
1722
const dirs = (process.env.PATH ?? '').split(delimiter).filter(Boolean)
1823
for (const dir of dirs) {
1924
const candidate = join(dir, 'mongod')
2025
if (existsSync(candidate)) return candidate
2126
}
22-
throw new Error('mongod not found on PATH; set MONGOMS_SYSTEM_BINARY to its location')
27+
return null
2328
}
2429

2530
/** Boot an in-memory MongoDB and connect mongoose to it. Call in beforeAll. */
2631
export async function setupMemoryMongo(): Promise<void> {
27-
memoryServer = await MongoMemoryServer.create({
28-
binary: { systemBinary: resolveMongodBinary() },
29-
})
32+
const systemBinary = resolveMongodBinary()
33+
// Use a system binary when present (no network); otherwise omit `binary` so
34+
// mongodb-memory-server resolves, downloads, and caches one itself.
35+
memoryServer = await MongoMemoryServer.create(
36+
systemBinary ? { binary: { systemBinary } } : undefined,
37+
)
3038
await mongoose.connect(memoryServer.getUri(), { dbName: 'notifications_test' })
3139
}
3240

0 commit comments

Comments
 (0)