Skip to content

Use postgres with useFakeTimer #798

Discussion options

You must be logged in to vote

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);

I suppose useFakeTimers mocks something that is used by postgres in…

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
3 replies
@mehdi-valette
Comment options

@joaomarcosjova
Comment options

@mehdi-valette
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by mehdi-valette
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants