Unit testing my Nuxt server API routes with a mock SQLite db (NuxtHub)
I created a Nuxt 4 project with (currently) only a few API routes. I'm using the NuxtHub SQL Database (SQLite) https://hub.nuxt.com/docs/features/database to store user data and more. The db is using Drizzle ORM.
I want to test my routes using
@nuxt/test-utils and vitest. I followed the setup for the testing utils: https://nuxt.com/docs/4.x/getting-started/testing.
The problem is that I want to simulate a mock SQLite database to test my routes but cannot find any documentation about this online. The tests should create a user before running the test and use that user to do the test. Then discarding the mock db.
How can I do this?NuxtHub
SQL Database · NuxtHub
Access a SQL database in your Nuxt application to store and retrieve relational data.
2 Replies
Hi @Samball. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
I’m sorry, but the Nuxt knowledge sources don’t provide guidance for unit-testing Nitro API routes or mocking a NuxtHub/SQLite (Drizzle) database. The docs currently cover:
- Setting up @nuxt/test-utils with Vitest, organizing tests, and running a Nuxt runtime environment for component/composable tests, including helpers like mockNuxtImport, mockComponent, mountSuspended/renderSuspended, and registerEndpoint for mocking fetches to Nitro endpoints when testing components (not route handlers themselves) (Testing overview, Unit testing setup, Helpers, registerEndpoint, mockNuxtImport, mockComponent).
- End-to-end testing via @nuxt/test-utils/e2e with setup and $fetch against a running test fixture app (E2E testing).
- A known gap: testing Nitro APIs is not yet documented and is tracked in issues (docs mention only e2e being documented so far) (GitHub issue). Given this, there’s no official example for spinning up and tearing down a mock SQLite/Drizzle database for API route unit tests in Nuxt’s docs. You can: - Use e2e tests with a fixture app and call your API via $fetch, but you’d need to handle database setup/teardown yourself outside the documented scope (e.g., via your own test bootstrapping), since the docs don’t cover DB mocking for Nitro (E2E setup).
- For component-level tests that consume APIs, mock responses with registerEndpoint instead of hitting a real database (registerEndpoint). If you need true route-level tests with a temporary SQLite, the knowledge sources do not explicitly mention how to do this with NuxtHub/Drizzle. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
- End-to-end testing via @nuxt/test-utils/e2e with setup and $fetch against a running test fixture app (E2E testing).
- A known gap: testing Nitro APIs is not yet documented and is tracked in issues (docs mention only e2e being documented so far) (GitHub issue). Given this, there’s no official example for spinning up and tearing down a mock SQLite/Drizzle database for API route unit tests in Nuxt’s docs. You can: - Use e2e tests with a fixture app and call your API via $fetch, but you’d need to handle database setup/teardown yourself outside the documented scope (e.g., via your own test bootstrapping), since the docs don’t cover DB mocking for Nitro (E2E setup).
- For component-level tests that consume APIs, mock responses with registerEndpoint instead of hitting a real database (registerEndpoint). If you need true route-level tests with a temporary SQLite, the knowledge sources do not explicitly mention how to do this with NuxtHub/Drizzle. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
<@448768622643642369> Kapa.ai is still learning and improving, please let me know how I did by reacting below.