porsager / porsager/postgres

vitest useFakeTimers causes the postgres connection to hang

Open
#1,091 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
8.7k
Forks
374
Avg merge
11d 16h
Merged PRs (30d)
1

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
  1. Start a local Postgres service
docker run --name vitest-postgres -e POSTGRES_PASSWORD=pass -p 5432:5432 -d postgres
  1. 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()
})
  1. 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
https://github.com/vitest-dev/vitest/issues/8302

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the reproduction in test/index.test.ts with the local Postgres Docker service and pnpm vitest index.test.ts. Compare the postgres client behavior with the provided pg example while vi.useFakeTimers() is enabled. Done means the select 1 query completes and returns results without timing out, while real-timer cleanup still works.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs, postgresql
Domain
databases, testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.