Test-driving a SvelteKit form action
The form
action is the thing that SvelteKit calls when the form is submitted. It is defined in the +page.server.js
file as an object named actions
. The general form is shown in the following code block. Don’t add this just yet; we’ll come to it later on:
export const actions = { default: async ({ request }) => { const data = await request.formData(); // ... do something with data here ... } };
This is what we’ll test-drive now. There’s a few things to note:
- First, the Vitest unit tests can check the behavior of the
form
action, but it doesn’t check any of the SvelteKit framework code that invokes the action. You’ll recall we took the same approach with the HTML form: we didn’t test thesubmit
action since that magic is managed by SvelteKit. For testing the framework integration, we need the Playwright tests.
...