[Question]Best practice to mock the database for API tests in Hono? (Runtime: Node, DB: DynamoDB, Test: Bun) #4413
Replies: 2 comments 1 reply
-
|
Got it! You want to test Hono routes in Node + Bun without hitting a real DynamoDB instance and without starting a real HTTP server. The key is to inject a mock repository or mock the database client at the function layer. Here’s a best-practice approach: 1. Structure Your Code for Dependency Injection 2. Inject Repository into Hono App
3. Create a Mock Repository for Tests 4. Write the Test Without Starting a Server ✅ Advantages:
5. Optional: Mock AWS SDK If you want to test the repository itself without hitting DynamoDB, you can also mock TL;DR
If you want, I can provide a full example with DynamoDBClient mocked so that even your repo can be tested without touching AWS. That way, all layers stay isolated. |
Beta Was this translation helpful? Give feedback.
-
|
The simple way (without extra code) is mock modules directly. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello! I’m looking for best practices to mock/stub the database layer when testing Hono APIs without starting a real server.
Beta Was this translation helpful? Give feedback.
All reactions