Testing authentication with Vitest
Now we drop down a level and get into specifics. Our tests will focus on the /birthdays
route and how it is presented given authentication data.
The Auth.js library utilizes SvelteKit’s session mechanism for passing authentication information into components, so what we do is harness that via the parent.session
object and the locals.getSession
function. All we have to do is use test doubles to mimic the responses we want.
We start by defining a session factory that can be used to set up these session test doubles. Then we’ll update page load tests with new authentication functionality, and finally, we’ll end with updating the form action tests.
Defining a session factory
Here’s the definition of src/factories/session.js
, which defines four exports that are used in the subsequent tests:
import { vi } from 'vitest'; const validSession = { user: 'api ' }; export const loggedInSession = (...