Using transactions in automated testing like Jest

I have a testing set-up as follows: 1. Start postgres container 2. Apply migrations to db 3. Jest test that connects to the postgres container 4. Teardown postgres container I would like to roll-back all data inserted/deleted during each individual test instance. I was struggling to make the built-in db.transaction work w/ the beforeEach and afterEach pattern. I tried to manually create a top-level transaction which didn't work because the application code also starts a transaction using the ORM. And the ORM is un-aware of the already existing top-level transaction. I can't manually use save-points for that same reason. Essentially, I can't manually execute transactions in conjuction with using the ORM's transaction. So now I want to see if anyone has a solution of using the existing db.transaction and applying it w/ beforeEach and afterEach Maybe I can inject a db.transaction into the using service in place of the already injected db (since one is a subset of the other). Maybe someone has an idea of how to do that?
No description
6 Replies
foobarfighter
foobarfighter14mo ago
I've been looking at doing something similar. Has anybody managed to get a setup like this working for their test environment?
itay
itayOP13mo ago
quick and dirty solution i got going

const listOfTables: string = Object.keys(db._.tableNamesMap).join(", ");
await db.execute(sql.raw(`TRUNCATE ${listOfTables} RESTART IDENTITY CASCADE`));

const listOfTables: string = Object.keys(db._.tableNamesMap).join(", ");
await db.execute(sql.raw(`TRUNCATE ${listOfTables} RESTART IDENTITY CASCADE`));
No description
foobarfighter
foobarfighter13mo ago
clever workaround. thanks for sharing.
CanRau
CanRau13mo ago
GitHub
[FEATURE]: Manual transactions · Issue #966 · drizzle-team/drizzle-...
Describe what you want Expected behavior: const transaction = database.beginTransaction(); // Do something with the DB transaction.commit(); // or transaction.rollback() Prior Art Laravel Manual Tr...
foobarfighter
foobarfighter13mo ago
ive needed this in other places as well
DiamondDragon
DiamondDragon8mo ago
hmm if we have the tables in different schemas, is there a way of accessing the schema name or "{schema}.{tableName}" ?
Want results from more Discord servers?
Add your server