You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is more like an open discussion than a question, but I was wondering what is the best way to do actual unit testing of an Hono app, when lots of its context and derived bindings are derived from the Cloudflare Workers ecosystem (e.g. env vars, D1 database/KV/Durable Objects etc. binding. This might also apply to other ecosystems I am not familiar with (vercel, deno cloud etc.).
Here are some examples of what I think can currently be done and what can not based on my experiements (referring to the docs here and source code in the hono repo):
✅ Testing simple methods or hono routes which don't require any specific CF Worker bindings (except for env, that seems to work fine passing it to app.request or app.fetch.
❌ Testing more complex endpoints which use bindings like D1/KV etc. and being able to use other testing methods like mocking, spying etc.. I particularily wanted to run tests with D1 (which would translate to a local or in memory SQLite database), but mock away other third-party services, like sending email. I had a hard time finding either examples or working approaches I could use, which led me to the next point
✅ Using unstable_dev() to setup the testing and basically run a full service with a full wrangler configuration here. This allows for some kind of integration tests, but I can't really mock things, making the testing a bit more dangerous and litters the code with runtime conditions for mocks or stubs, which isn't great. Another problem with this is that there is no access to the underlying bindings through the unstable_dev interface, which means I have to rely on other libraries to for example access a local D1/SQLite database (for setup, teardown data for tests if needed).
At the moment I ended up with a mix of 1. and 3., but I would really prefer 2. with a relatively simple setup and access to the underlying bindings. Maybe this can be done with Miniflare, but development of v3 seems in a bit of a flux right now and v2 does not support D1 bindings, beside the fact that is also lacking good examples, so I am not sure the ideal approach would be feasible or it would be more or less like unstable_dev.
Anyway, just wanted to put that out here and maybe spark a bit of a discussion and maybe even discover ways to do this in a better way.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
This is more like an open discussion than a question, but I was wondering what is the best way to do actual unit testing of an Hono app, when lots of its context and derived bindings are derived from the Cloudflare Workers ecosystem (e.g. env vars, D1 database/KV/Durable Objects etc. binding. This might also apply to other ecosystems I am not familiar with (vercel, deno cloud etc.).
Here are some examples of what I think can currently be done and what can not based on my experiements (referring to the docs here and source code in the hono repo):
app.request
orapp.fetch
.unstable_dev()
to setup the testing and basically run a full service with a full wrangler configuration here. This allows for some kind of integration tests, but I can't really mock things, making the testing a bit more dangerous and litters the code with runtime conditions for mocks or stubs, which isn't great. Another problem with this is that there is no access to the underlying bindings through theunstable_dev
interface, which means I have to rely on other libraries to for example access a local D1/SQLite database (for setup, teardown data for tests if needed).At the moment I ended up with a mix of 1. and 3., but I would really prefer 2. with a relatively simple setup and access to the underlying bindings. Maybe this can be done with Miniflare, but development of v3 seems in a bit of a flux right now and v2 does not support D1 bindings, beside the fact that is also lacking good examples, so I am not sure the ideal approach would be feasible or it would be more or less like
unstable_dev
.Anyway, just wanted to put that out here and maybe spark a bit of a discussion and maybe even discover ways to do this in a better way.
Beta Was this translation helpful? Give feedback.
All reactions