Skip to content

Commit 952dfa1

Browse files
jahoomaclaude
andcommitted
Fix transaction test crashing on db/index.ts import
The test imports db/index.ts which eagerly calls postgres() and drizzle() at the top level. In CI, the dummy DATABASE_URL produces a client without options.parsers/serializers, crashing drizzle's construct(). Mock both postgres and @codebuff/internal/env before importing to avoid the real db initialization — the test only ever spies on db.transaction anyway. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent c50cec7 commit 952dfa1

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

packages/internal/src/db/__tests__/transaction.test.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,25 @@ import { AnalyticsEvent } from '@codebuff/common/constants/analytics-events'
33
import { createPostgresError } from '@codebuff/common/testing/errors'
44
import { afterEach, beforeEach, describe, expect, it, mock, spyOn } from 'bun:test'
55

6-
import * as dbModule from '../index'
7-
import {
8-
getRetryableErrorDescription,
9-
isRetryablePostgresError,
10-
} from '../transaction'
11-
126
import type { Logger } from '@codebuff/common/types/contracts/logger'
137

8+
// Mock postgres and env before any module that imports db/index.ts is loaded.
9+
// db/index.ts calls postgres(env.DATABASE_URL) and drizzle() at the top level,
10+
// which fails without real env vars / DB. These tests only need db.transaction (spied).
11+
mock.module('postgres', () => ({
12+
default: () => ({
13+
options: { parsers: {}, serializers: {} },
14+
}),
15+
}))
16+
mock.module('@codebuff/internal/env', () => ({
17+
env: { DATABASE_URL: 'postgres://mock:mock@localhost:5432/mock' },
18+
}))
19+
20+
// Now safe to import modules that depend on db/index.ts
21+
const dbModule = await import('../index')
22+
const { getRetryableErrorDescription, isRetryablePostgresError } =
23+
await import('../transaction')
24+
1425
describe('transaction error handling', () => {
1526
describe('getRetryableErrorDescription', () => {
1627
describe('Class 40 — Transaction Rollback errors', () => {

0 commit comments

Comments
 (0)