Acceptance testing practices? In-memory Postgres mock for high-speed acceptance testing?
Hi! I'm using Drizzle with SvelteKit and Vercel Postgres.
I would like to do extensive acceptance test coverage. I want my tests to be:
* very fast: Postgres speed is a drag when you have thousands of test cases;
* very reliable: not prone to network hiccups, paywalls, etc;
* isolated: the database should be recreated from scratch for every test case, so that state does not leak from test to test and from job to job.
These requirements make using real Postres database in tests not feasible. I would like the test env (and the dev env too, for that matter!) use a fast in-memory equivalent of Postgres.
What are my options? I can't believe that I'm the first one to have such requirements.
PS I considered using a non-SQL database mock solution such as https://miragejs.com . This is feasible, but I lose Drizzle's type safety and fluency. I want to stay in Drizzle land.
Mirage JS • An API mocking library for frontend developers
Build, test and demo your JavaScript application without an API
9 Replies
You'll be surprise on how fast postgres on a quick docker image can be, specially for tests with not a lot of rows. I would suggest to take a look at the integration tests in the drizzle repo itself to see how they implemented it. It's very interesting IMO
I also recommend thinking about how to balance unit tests where drizzle is mocked vs integration tests where you actually touch a database
This!
@Luxaritas @Angelelz Yes, mocking Drizzle in tests is exactly what I want.
Do you know examples where it's already done?
Any guidance on how specifically mock Drizzle while maintaining the typed DX?
You can see how I mock drizzle in unit tests here: https://discord.com/channels/1043890932593987624/1158204053998813266/1158471811734978791
I use testcontainers to spin up a postgres db for each test suite, with some optimisations (e.g. turn off fsync) it starts up in <2 seconds
We have hundreds of tests like this, best way to test DB queries IMO
If someone is curious, I did a poc with PGLite for testing: 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.
this is awesome! Do you think this approach could replace testcontainers?
😬 I don't know what is it. Maybe some docker with pg?
I am confident with PGLite, Drizzle team also run their tests with it
PGLite also manages my web app IndexedDB. I use it everywhere