Updating the route loader to use the API
In this section, you’ll introduce a call to the GET
/api/birthdays
endpoint using the SvelteKit fetch function. That will involve using a spy.
What is a test spy?
A spy is a function that keeps a record of each time it is called, together with the arguments it is called with. It can then be inspected later to verify that it was called with the correct arguments. The spy is almost always a stub as well, meaning it avoids calling the real function entirely, instead returning a hardcoded – stubbed – value. The spy acts as a substitute for a real function.
In Vitest, a spy is created by calling the vi.fn
function.
When we use a test spy, you’ll have at least one test that checks the arguments passed to the spy. Then you’ll have at least one more test for each stubbed return value that the spy returns.
We will use a spy for the fetch
function that mimics retrieving birthdays via the GET
/
api/birthdays...