{"library":"pg-transactional-tests","title":"PostgreSQL Transactional Tests","description":"pg-transactional-tests is a utility library designed to simplify database testing by wrapping each test in a PostgreSQL transaction. It currently stands at version 1.2.0, with a stable release cadence implied by its versioning and active development. The library patches the `pg` package to automatically initiate a transaction before a test, and then roll it back afterward, ensuring a clean database state for every test run without the overhead of clearing tables. A key differentiator is its compatibility with many popular ORMs like Sequelize, TypeORM, MikroORM, Objection, and Knex, which all build upon the `pg` driver. It also intelligently handles nested transactions using savepoints and supports parallel testing across multiple databases by tracking transaction state per connection. A significant limitation is its incompatibility with Prisma, due to Prisma's distinct database interaction model. This approach vastly accelerates test suites and reduces boilerplate for database setup and teardown.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install pg-transactional-tests"],"cli":null},"imports":["import { testTransaction } from 'pg-transactional-tests';","beforeEach(testTransaction.start);","afterEach(testTransaction.rollback);","afterAll(testTransaction.close);"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { testTransaction } from 'pg-transactional-tests';\n\n// This setup file should be configured in your test runner, e.g., Jest's `setupFilesAfterEnv`.\n// It ensures that every test involving the database runs within its own transaction\n// and that the transaction is rolled back afterwards.\n\n// Starts a transaction before any tests begin (useful if using `beforeAll` hooks that perform queries)\nbeforeAll(testTransaction.start);\n\n// Starts a new savepoint/transaction before each individual test\nbeforeEach(testTransaction.start);\n\n// Rolls back the transaction/savepoint after each test completes, ensuring isolation\nafterEach(testTransaction.rollback);\n\n// Closes all PostgreSQL connections managed by pg-transactional-tests after all tests are done\nafterAll(testTransaction.close);\n\n// Example test structure for context:\n// describe('User Service', () => {\n//   it('should create a user', async () => {\n//     // Your ORM or pg client code here will run inside a transaction\n//     await someORM.user.create({ name: 'Test User' });\n//     const user = await someORM.user.findUnique({ where: { name: 'Test User' } });\n//     expect(user).not.toBeNull();\n//   });\n//   it('should not persist user across tests', async () => {\n//     const count = await someORM.user.count();\n//     expect(count).toBe(0); // Because previous test's transaction was rolled back\n//   });\n// });","lang":"typescript","description":"Demonstrates the essential Jest setup to enable transactional tests for a test suite, ensuring clean database state for every test.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}