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?
Screen_Shot_2023-11-06_at_6.20.27_PM.png
Was this page helpful?