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
15 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
Never heard of PGLite before! I saw this repo (https://github.com/michaelshimeles/react-local-first-hono) using DexieJS + Hono + Drizzle & got curious to see if anyone in this Discord did use IndexedDB to build a sync engine! So I guess we need PGlite vs. what he did in his repo right?
GitHub
GitHub - michaelshimeles/react-local-first-hono: React Local First ...
React Local First Application with Hono RPC Sync Engine - michaelshimeles/react-local-first-hono
I have a demo too https://github.com/rphlmr/drizzle-on-indexeddb
GitHub
GitHub - rphlmr/drizzle-on-indexeddb: This is Drizzle ORM in your b...
This is Drizzle ORM in your browser, with PGLite. Contribute to rphlmr/drizzle-on-indexeddb development by creating an account on GitHub.
Yeah cloned that one!
I have a NextJS setup, does it change much? I guess not if I use RQ hooks like the guy did in the repo I shared?
Should be fine in a client component (for the indexeddb part).
I guess you could have a server component that get the migration and forward it to the client component 👀
Haha you lost me in the second part 🤣
https://nextjs.org/docs/app/building-your-application/rendering/composition-patterns#passing-props-from-server-to-client-components-serialization
with nextjs you can’t do the same thing I did here (https://github.com/rphlmr/drizzle-on-indexeddb/blob/main/app/database/.client/db.ts)
importing a json like that is a Vite feature.
But since a JSON is serializable, you should be able to use what is described in the nextjs doc.
If I find some room in my schedule I could create a demo
GitHub
drizzle-on-indexeddb/app/database/.client/db.ts at main · rphlmr/dr...
This is Drizzle ORM in your browser, with PGLite. Contribute to rphlmr/drizzle-on-indexeddb development by creating an account on GitHub.
Rendering: Composition Patterns | Next.js
Recommended patterns for using Server and Client Components.