How to call server APIs from unit/e2e test?
I have a composable (among many others which I'm going to want to test) which call
useFetch
, $fetch
, etc... Ideally I want to test exactly what those functions do instead of making isolated $fetch
calls, but I can't figure out how to setup the server or how to mock the functions that call its endpoints.
So far I have @nuxt/test-utils/e2e
setup to make calls to a dev server I run separately with my test database. The problem lies in wanting to make my composables talk to that server, as by default it'll just try to make calls to /api/blabla...
without a base URL.
Is there a better way to do this without needing to refactor too much code, or am I just missing a small piece here? Thanks a lot
2 Replies
Hi @Marceau. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
To test composables that use
- For e2e tests, use the
useFetch
, $fetch
, etc., and have them call your server APIs during unit or end-to-end (e2e) tests, you have a few options depending on your testing goals and setup:
1. End-to-End (e2e) Testing with a Real or Dev Server
With @nuxt/test-utils/e2e
, you can configure your tests to target a running server (such as your dev server with a test database) by providing the host
option to the setup
function. This way, all requests (including those from your composables) will be directed to the specified server, and you don't need to refactor your code to add base URLs everywhere.
Example:
This approach allows your composables to call /api/xyz
as usual, but the requests will be routed to your running server at the specified host, so you don't need to change your composable code to include a base URL. This is the recommended way for e2e tests that should interact with a real backend or test database End-to-End Testing.
2. Unit Testing with Mocked Endpoints
For unit tests, you can use registerEndpoint
from @nuxt/test-utils/runtime
to mock server endpoints. This is useful if you want to test your composables in isolation without hitting a real server.
Example:
Now, when your composable calls /api/blabla
, it will receive the mocked response. This avoids the need for a running server and keeps your tests fast and isolated registerEndpoint.
3. Customizing Base URLs
If you need your composables to be flexible about the base URL (for example, to switch between local, test, or production APIs), consider using Nuxt's runtime config and referencing it in your composables. This way, you can override the base URL in your test environment without refactoring all your code. See useRuntimeConfig for details.
---
Summary:- For e2e tests, use the
host
option in setup
to point all API calls to your running dev/test server.
- For unit tests, use registerEndpoint
to mock API responses.
- Use runtime config for flexible base URLs if needed.
This should allow you to test your composables as they are, without major refactoring, and have them interact with either real or mocked APIs as appropriate for your test type.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@813712892552544307> Kapa.ai is still learning and improving, please let me know how I did by reacting below.