Testcontainers for NestJS, for Spring Developers: Fresh Database, Still Two
The design sheet The full architecture and the verified references. Free, no email.
3:18 docker compose up, and watch it happen
Last episode the same two tests counted 1 and 2, then 3 and 4 on the next run. With a Postgres container from Testcontainers, every run counted 1 and 2, on Spring and on NestJS. The old rows are gone, but the second test still sees the first test’s row. Measured on 30 September 2026.
Per run, not per test
The container lives for the class or the file, so every run starts empty and the tests inside it share. On this machine, with the image cached, a container started in a little over a second.
Where you own the container
Three Spring classes with their own static @Container started three containers, and each class
saw only its own row. Three Nest files that each start one cost about four seconds of starts and
stops. One container for the run, a base class in Spring or a Vitest globalSetup that hands the
URI over with provide and inject, starts once, and then the classes and files saw 1, 2 and 3 in
whatever order they ran. Share the container, and the database is shared too.
Container for the run, reset for the test
One container per run, plus episode 18’s truncate before each test.
The full code for every measurement is in the repository, and the free design sheet above has the checklist on one page.