-
Notifications
You must be signed in to change notification settings - Fork 310
Open
Description
I encountered an issue while using vitest for unit testing. When I use vitest's useFakeTimers simultaneously, postgres queries hang indefinitely until they time out. However, pg (node-postgres) does not have this problem. I'm not sure what is happening; I discovered this issue while testing Cloudflare Workers.
Describe the bug
If vi.useFakeTimers()
is removed, the query can be performed normally and results are returned.
FAIL test/index.test.ts > useFakeTimers
Error: Test timed out in 5000ms.
If this is a long-running test, pass a timeout value as the last argument or configure it globally with "testTimeout".
Reproduction
- Start a local Postgres service
docker run --name vitest-postgres -e POSTGRES_PASSWORD=pass -p 5432:5432 -d postgres
- Create a test file
import { it, vi } from 'vitest'
import postgres from 'postgres'
it('useFakeTimers', async () => {
vi.useFakeTimers()
const sql = postgres(
'postgres://postgres:pass@localhost:5432/postgres?sslmode=disable',
{
max: 5,
fetch_types: false,
},
)
const r = await sql`select 1`
console.log(r)
vi.useRealTimers()
})
- pnpm vitest index.test.ts
System Info
System:
OS: macOS 15.5
CPU: (12) arm64 Apple M2 Max
Memory: 441.38 MB / 64.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 22.14.0 - /usr/local/bin/node
npm: 10.9.2 - /usr/local/bin/npm
pnpm: 10.5.2 - /usr/local/bin/pnpm
bun: 1.1.6 - ~/.bun/bin/bun
Browsers:
Brave Browser: 131.1.73.105
Chrome: 138.0.7204.101
Edge: 128.0.2739.54
Safari: 18.5
The following code using pg will not be affected by useFakeTimers.
import { it, vi } from 'vitest'
import { Client } from 'pg'
it('useFakeTimers & pg', async () => {
vi.useFakeTimers()
const client = new Client(
'postgres://postgres:pass@localhost:5432/postgres?sslmode=disable',
)
await client.connect()
const r = await client.query('select 1')
console.log(r)
await client.end()
vi.useRealTimers()
})
Related issues
vitest-dev/vitest#8302
Metadata
Metadata
Assignees
Labels
No labels