MV
MV
DTDrizzle Team
Created by MV on 2/1/2024 in #help
Use drizzle with useFakeTimer
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.
2 replies