Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form posting with use:enhance #30

Open
bamthomas opened this issue Jan 13, 2023 · 2 comments
Open

Form posting with use:enhance #30

bamthomas opened this issue Jan 13, 2023 · 2 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@bamthomas
Copy link

bamthomas commented Jan 13, 2023

First of all thank you for all the recipes it helped me a lot to test my app (I'm new to nodejs development).
I'm trying to make unit tests on form posting with use:enhance. I'm having a

Error: Cannot call applyAction(...) on the server
    at file:///home/dev/src/iroco-app-client/node_modules/@sveltejs/kit/src/runtime/app/forms.js:11:9
    at fallback_callback (file:///home/dev/src/iroco-app-client/node_modules/@sveltejs/kit/src/runtime/app/forms.js:56:4)
    at HTMLFormElement.handle_submit (file:///home/dev/src/iroco-app-client/node_modules/@sveltejs/kit/src/runtime/app/forms.js:116:3)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)

I haven't found tests on this on the sveltekit repo other than the e2e tests, neither in this repository.

Do you have examples or insights to make it work? To be more specific, before migrating to sveltekit, I was doing posting tests with MSW. I like to test the result of the request sending with MSW because it gives a grasp of the Web API without knowing the details of how the request is done on the client side. So I'd like to avoid mocking it.

More generally, as tests are executed by node, server classes are executed by node, could we imagine a way of testing client/server integration like it is done for python WSGI webapps, i.e. without actual HTTP requests and webserver ?

@bamthomas bamthomas changed the title form posting with use:enhance Form posting with use:enhance Jan 13, 2023
@wd-David wd-David added enhancement New feature or request question Further information is requested labels Jan 15, 2023
@wd-David
Copy link
Owner

I have limited knowledge about testing forms with use:enhance since we're not using SSR in my work project.
Maybe e2e testing is a better approach.

Do you have a repro or sandbox link?

@Skylli202
Copy link

Hi,

I'm really new to test in Svelte. First of all, @davipon, I can't be grateful enough about this repo. It learns me a ton of things.

As for the initial question of this issue, I am in the same issue as the author @bamthomas. I am using SvelteKit and I use use:enhance on my forms.

From the regular Svelte Actions, I isolated my form action like so :

import type { RequestEvent } from '@sveltejs/kit';

export async function generate(requestEvent: RequestEvent):  {
  // ....
}

I'm not a professional when it comes to typing (yet!) but I figured out from SvelteKit Docs what is the type of the parameter for the SvelteKit Form Actions.

Then it becomes a simple Svelte Action to test from there, from my experience. I can imagine there is some complex case where it is not as easy as that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants