deckhearth/test/lib/sql.test.js

32 lines
983 B
JavaScript
Raw Permalink Normal View History

import { describe, expect, it, vi, afterEach } from 'vitest';
describe('lib/sql.js', () => {
afterEach(() => {
vi.resetModules();
vi.unstubAllGlobals();
delete process.env.POSTGRES_URL;
delete process.env.DATABASE_URL;
});
it('throws when POSTGRES_URL is unset', async () => {
const { sql } = await import('../../lib/sql.js');
await expect(sql`SELECT 1`).rejects.toThrow('POSTGRES_URL is not set');
});
it('returns rows in the vercel/postgres shape', async () => {
process.env.POSTGRES_URL = 'postgresql://example.com/postgres';
const mockQuery = vi.fn(async () => {
const result = [{ id: 1 }];
result.count = 1;
return result;
});
vi.doMock('postgres', () => ({
default: vi.fn(() => mockQuery),
}));
const { sql } = await import('../../lib/sql.js');
const response = await sql`SELECT id FROM users WHERE id = ${1}`;
expect(response).toEqual({ rows: [{ id: 1 }], rowCount: 1 });
});
});