Skip to content

Commit 7c1894c

Browse files
committed
fix(testing): add Bun/Jest compatibility layer for mock-db
Use runtime detection to conditionally load bun:test mock function. Falls back to identity function when running in Jest.
1 parent 507fd3d commit 7c1894c

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

common/src/testing/mock-db.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
1-
import { mock } from 'bun:test'
2-
31
import type {
42
TestableDb,
53
TestableDbWhereResult,
64
} from '@codebuff/common/types/contracts/database'
75
import type { Logger } from '@codebuff/common/types/contracts/logger'
86

7+
// Compatibility layer: use bun:test mock when available, otherwise identity function
8+
// This allows the utilities to work in both Bun and Jest environments
9+
/* eslint-disable @typescript-eslint/no-require-imports, @typescript-eslint/no-explicit-any */
10+
const mock: <T extends (...args: any[]) => any>(fn: T) => T = (() => {
11+
if (typeof globalThis.Bun !== 'undefined') {
12+
try {
13+
return require('bun:test').mock
14+
} catch {
15+
// Fall through to identity function
16+
}
17+
}
18+
// Identity function for Jest or when bun:test is not available
19+
return <T extends (...args: any[]) => any>(fn: T) => fn
20+
})()
21+
/* eslint-enable @typescript-eslint/no-require-imports, @typescript-eslint/no-explicit-any */
22+
923
// ============================================================================
1024
// Types
1125
// ============================================================================

0 commit comments

Comments
 (0)