Mocking Drizzle instance
Hey there, I am currently trying to find a good way to mock our drizzle instance for our unit test suite, but the nature of the usage of it makes it kinda hard. Is there somebody who is running a code base with a mocked drizzle?
14 Replies
@bloberenober @Andrew Sherman Any idea? 😦
Sorry for a late response, finally have free time to answer all questions in Discord
https://discord.com/channels/1043890932593987624/1101108758127591435/1139240309939777719
I'll answer this question anyway
We don't have mocks for drizzle. How do you want to use it?
And why do you need to mock database for tests at all?
hey @Andrew Sherman I have the same scenario here, I'm testing my repositories and I'm creating an unit test and I need to mock it hehe
why not use something like sinon to stub/mock specific methods on the db?
or if you are using vitest it has its own mocking API I think
there's nothing specific to Drizzle that can't be mocked like anything else
I've tried to mock using vitest but I'm facing some errors
have you already use vitest to mock drizzle?
Then add this to your jest config:
And here is a sample test
youre welcome
@jakeleventhal How can I mock with select, insert and update?
what i eneded up doing is basically using very few of those drizzle mock tests
IMO, any test like that is kind of weak. its much better to either do an integration test or dont make those checks entirely
but if you really wanted to you can just extend the return types of each of those funcs
I have a User creation function like this that uses the returning() function. I want to mock test it but don't know what to pass in below.
did you ever figure this out?
it gives me issues when im not using the query method
Just sharing this if any helpful to anyone:
I ended up using in-memory db from @electric-sql/pglite instead of mocking. So even if I am using "drizzle-orm/node-postgres" in my actual application, tests are working as expected with "Pglite"
Any idea how to solve this error? It works at runtime, just vscode throws an error on this line.
i ended up just testing with another database instead of mocking it