mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-20 14:06:27 +00:00
43 lines
1.4 KiB
TypeScript
43 lines
1.4 KiB
TypeScript
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
|
|
import { createSnapshotTestDb } from '../../helpers/db-mock';
|
|
import { resetTestDb } from '../../helpers/test-db';
|
|
import { createTestUnitOfWork } from '../../helpers/test-uow';
|
|
import { createUser } from '../../helpers/factories';
|
|
import type { UnitOfWork } from '../../../src/nest/database/unit-of-work';
|
|
|
|
const testDb = createSnapshotTestDb();
|
|
let uow: UnitOfWork;
|
|
|
|
beforeAll(async () => {
|
|
uow = await createTestUnitOfWork(testDb);
|
|
});
|
|
beforeEach(() => resetTestDb(testDb));
|
|
afterAll(() => testDb.close());
|
|
|
|
const countUsers = () => (testDb.prepare('SELECT COUNT(*) AS n FROM users').get() as { n: number }).n;
|
|
|
|
describe('createTestUnitOfWork', () => {
|
|
it('TUOW-001: raw better-sqlite3 statements inside transactional() commit together', async () => {
|
|
await uow.transactional(async () => {
|
|
createUser(testDb);
|
|
createUser(testDb);
|
|
});
|
|
expect(countUsers()).toBe(2);
|
|
});
|
|
|
|
it('TUOW-002: a throw rolls raw statements back', async () => {
|
|
await expect(
|
|
uow.transactional(async () => {
|
|
createUser(testDb);
|
|
throw new Error('boom');
|
|
}),
|
|
).rejects.toThrow('boom');
|
|
expect(countUsers()).toBe(0);
|
|
});
|
|
|
|
it('TUOW-003: the helper is memoised per handle', async () => {
|
|
const again = await createTestUnitOfWork(testDb);
|
|
expect(again).toBe(uow);
|
|
});
|
|
});
|