Test-driving the load function
Now that we’ve decided to implement a load
function that returns hardcoded birthday data for Hercules and Athena, the actual change has become very simple.
The load
function is a special SvelteKit function that will be invoked when a request comes in for the given route. So, when the user navigates to the /birthdays
route, SvelteKit calls the load
function in the src/routes/birthdays/+page.server.js
file and then renders the component in the src/routes/birthdays/+page.svelte
file.
Follow these steps to create the load
function using TDD:
- Create a new Vitest test file named
src/routes/birthdays/page.server.test.js
and start it off as shown. We are importing theload
function from a+page.server.js
file that doesn’t yet exist. We call the function in our test and store the result:import { describe, it, expect } from 'vitest'; import { load } from './+page.server.js'; describe('/birthdays - load'...