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?6 Replies
I've been looking at doing something similar. Has anybody managed to get a setup like this working for their test environment?
quick and dirty solution i got going
clever workaround. thanks for sharing.
There's an open issue https://github.com/drizzle-team/drizzle-orm/issues/966
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...
ive needed this in other places as well
hmm if we have the tables in different schemas, is there a way of accessing the schema name or "{schema}.{tableName}" ?