Trouble running JEST testing in Hono for Cloudfare Workers
So, I have an API setup in Hono with cloudfare workers and I setup the JEST tests but they don't seem to inject the D1 DB into the context environment so I can use it to init the DB and perform the associated request actions. I am trying to just do this locally. What am I doing wrong? I'm kinda new so any help would be much appreciated. The test just sends requests to the server, do I have to manually add the DB to the context so Drizzle ORM can be initialized or something else. Sorry, if I'm not using the right words to describe it
7 Replies
It'll not easy to help you since testing is a vast subject.
Do you want to achieve unit testing, calling your real code?
Hono has just released a test helper
https://hono.dev/helpers/testing
Testing Helper - Hono
Ultrafast web framework for Cloudflare Workers, Fastly Compute@Edge, Deno, Bun, Vercel, Lagon, Node.js, and others. Fast, but not only fast.
I am a hono user (with node) but I haven't done any testing yet 😅
Thanks. I'll try this, hopefully will work
@walkersyed581 Hey sorry for waking this thread but I am struggling to get hono+cf workers set up with drizzle+d1, do you have a repo you can share?
I think the db object needs to be set to the hono context during a middleware initialization but drizzle is using .env in its
drizzle.config.ts
so I'm not sure how to combine the two things.
This is how I have it set up - I pass the cloudflare account and D1 credentials in the dbCredentials node.
I then use a middleware like yours (but mine is its own file) to initialize the DB on the first hono request and reuse that connection on subsequent requests (as needed):
You might need to mess with the client settings since you seem to be using Turso. Also, I'm using wrangler which auto-generated an env.d.ts file that contains my Cloudflare env types that are needed to make a db connection.
I'll give that a go, thanks for the help!