🚨 Issue: Testing with PostgreSQL and Drizzle ORM using Testcontainers
I’m writing tests for a function (listProjects) that interacts with a PostgreSQL database using Drizzle ORM. The problem is that listProjects is using a global db instance, which is initialized in the main application. In my tests, I want to use a PostgreSQL container from Testcontainers to spin up a test database, but my test code ends up using a different db instance, not the one I need.
test.ts
method to be tested.
3 Replies
I dont want to have to pass a db instance to my methods..
that leads me to handle the global instance to return the proper db instance based on the use case:
In my global db instance i would either return my dev/prod db instance but if test return the testcontainers postgres db instance ?
Is this the right approach?
If no, what is a good simple approach.
If yes how would i differenciate between dev/prod vs test? envs .. NODE_ENV?
const container = await new PostgreSqlContainer().start();
would have to move from my test files and would go in my global db handler file to be used as db instance if NODE_ENV
is test
?
Is that a clean approach
EDIT EDIT EDIT:
i now have this but i dont rly like it:
tests
Maybe this can help you? https://github.com/rphlmr/drizzle-vitest-pg
GitHub
GitHub - rphlmr/drizzle-vitest-pg: How to use Drizzle & PGLite with...
How to use Drizzle & PGLite with Vitest. Contribute to rphlmr/drizzle-vitest-pg development by creating an account on GitHub.
Yeah i was just lookin at this, i kinda like the idea of override the import, thats what i need (I think)