Registry /
testing / typeorm-test-transactions
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
runInTransaction
✓ import { runInTransaction } from 'typeorm-test-transactions';
✗ const { runInTransaction } = require('typeorm-test-transactions');
The library primarily uses ES module syntax. CommonJS `require` is generally discouraged for new projects.
initialiseTestTransactions
✓ import { initialiseTestTransactions } from 'typeorm-test-transactions';
✗ import initialiseTestTransactions from 'typeorm-test-transactions';
This is a named export, not a default export. It must be called once before your tests run.
This quickstart demonstrates how to set up `typeorm-test-transactions` with a basic TypeORM `DataSource` and wrap individual test cases using `runInTransaction`. It verifies that database changes are automatically rolled back after a test completes, ensuring isolation between tests.
import { runInTransaction, initialiseTestTransactions } from 'typeorm-test-transactions';
import { DataSource } from 'typeorm';
// Mock or provide your TypeORM DataSource
// In a real application, this would come from your setup (e.g., NestJS module)
const mockDataSource = new DataSource({
type: 'sqlite',
database: ':memory:',
synchronize: true,
entities: [],
});
initialiseTestTransactions();
describe('UserService', () => {
let dataSource: DataSource;
beforeAll(async () => {
dataSource = await mockDataSource.initialize();
});
afterAll(() => {
return dataSource.destroy();
});
it('should create a user and roll back', async () => {
await runInTransaction(async () => {
// Simulate some database operation
const queryRunner = dataSource.createQueryRunner();
await queryRunner.connect();
await queryRunner.manager.query('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)');
await queryRunner.manager.query("INSERT INTO users (name) VALUES ('Test User')");
const result = await queryRunner.manager.query('SELECT COUNT(*) FROM users');
expect(result[0]['COUNT(*)']).toBe(1);
await queryRunner.release();
});
// After runInTransaction, the changes should be rolled back.
// If we try to query now, the table or data should not exist.
const queryRunner = dataSource.createQueryRunner();
await queryRunner.connect();
// This query might fail or return 0, depending on database and exact setup
const countAfterRollback = await queryRunner.manager.query('SELECT COUNT(*) FROM users').catch(() => 0);
await queryRunner.release();
// Expect 0 or an error indicating table not found, demonstrating rollback.
// For sqlite :memory:, the table disappears. Adjust expectation for other DBs.
expect(countAfterRollback).toBe(0);
});
it('should allow another transaction in parallel', async () => {
await runInTransaction(async () => {
// This test runs in its own isolated transaction
const queryRunner = dataSource.createQueryRunner();
await queryRunner.connect();
await queryRunner.manager.query('CREATE TABLE other_table (id INTEGER PRIMARY KEY)');
await queryRunner.release();
});
// Rollback ensures isolation
});
});
Audit
Dependencies
typeormrequiredCore ORM dependency for database interaction.
reflect-metadatarequiredRequired for TypeORM's decorator-based metadata handling.