Use drizzle with useFakeTimer

Hello everyone! At my job we've been working with Drizzle for a few months now and it offers a really nice developer experience, congrats 👍 Recently we hit a wall: in a test (vitest) we use useFakeTimers, which mocks the javascript date and time objects (https://vitest.dev/api/vi.html#vi-usefaketimers) When we call any database query (transaction, insert, query, ...) the script is blocked and the test times out. I think it's due to the database (we use drizzle-orm/postgres-js and postgres), but maybe someone here have an idea?
Vitest
Next generation testing framework powered by Vite
1 Reply
MV
MV9mo ago
For the record, it works with this code:
import postgres from 'postgres';
import { afterAll, beforeAll, test, vi } from 'vitest';

const pgClient = postgres(process.env.PG_CONNECTION_STRING, {});

beforeAll(() => {
vi.useFakeTimers({toFake: ['Date']}); // mock only what you need
vi.setSystemTime(new Date());
});

afterAll(() => {
vi.useRealTimers();
});

test('lock', async () => {
try {
console.log('before call');
const result2 = await pgClient`SELECT * FROM "user"`;
console.log('after call');
} catch (e) {
console.log('catch');
}

console.log('end');
}, 20_000);
import postgres from 'postgres';
import { afterAll, beforeAll, test, vi } from 'vitest';

const pgClient = postgres(process.env.PG_CONNECTION_STRING, {});

beforeAll(() => {
vi.useFakeTimers({toFake: ['Date']}); // mock only what you need
vi.setSystemTime(new Date());
});

afterAll(() => {
vi.useRealTimers();
});

test('lock', async () => {
try {
console.log('before call');
const result2 = await pgClient`SELECT * FROM "user"`;
console.log('after call');
} catch (e) {
console.log('catch');
}

console.log('end');
}, 20_000);
I suppose useFakeTimers mocks something that is used by postgres in a way that disturbs postgres, thus creating an issue.
Want results from more Discord servers?
Add your server