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

TypeError: Cannot read properties of undefined (reading '$set') #525

Open
peter-bab opened this issue Dec 8, 2024 · 2 comments
Open

TypeError: Cannot read properties of undefined (reading '$set') #525

peter-bab opened this issue Dec 8, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@peter-bab
Copy link

Description
When testing a form which uses formsnap & superforms the test fails with an error as soon as I try to submit the form (in browser everything works fine). Tried with both SPA: true mode and without it.

Relevant dependencies:

  "sveltekit-superforms": "^2.21.1",
  "formsnap": "^2.0.0-next.1",
  "@sveltejs/kit": "^2.9.0",
  "svelte": "^5.0.0",

Tested form:

<script lang="ts">
  import * as Form from "$lib/components/ui/form/index.js";
  import { Input } from "$lib/components/ui/input/index.js";
  import { formSchema, type FormSchema } from "./schema";
  import {
   type SuperValidated,
   type Infer,
   superForm,
  } from "sveltekit-superforms";
  import { zodClient } from "sveltekit-superforms/adapters";
  
  export let data: SuperValidated<Infer<FormSchema>>;
  
  const form = superForm(data, {
    validators: zodClient(formSchema),
  }); 
  
  const { form: formData, enhance } = form;
 </script>
  
 <form method="POST" use:enhance>
  <Form.Field {form} name="username">
   <Form.Control>
    {#snippet children({ props })}
     <Form.Label>Username</Form.Label>
     <Input {...props} bind:value={$formData.username} />
    {/snippet}
   </Form.Control>
   <Form.Description>This is your public display name.</Form.Description>
   <Form.FieldErrors />
  </Form.Field>
  <Form.Button>Submit</Form.Button>
 </form>

Error:

Vitest caught 1 unhandled error during the test run.
This might cause false positive tests. Resolve unhandled errors to make sure your tests are not affected.

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Rejection ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
TypeError: Cannot read properties of undefined (reading '$set')
 ❯ Module.applyAction node_modules/@sveltejs/kit/src/runtime/client/client.js:1990:8
    1988|   _goto(result.location, { invalidateAll: true }, 0);
    1989|  } else {
    1990|   /** @type {Record<string, any>} */
       |        ^
    1991|   root.$set({
    1992|    // this brings Svelte's view of the world in line with SvelteKit's
 ❯ validationResponse node_modules/sveltekit-superforms/dist/client/superForm.js:1396:62
 ❯ Timeout._onTimeout node_modules/sveltekit-superforms/dist/client/superForm.js:1125:34
 ❯ listOnTimeout node:internal/timers:569:17
 ❯ processTimers node:internal/timers:512:7

MRE

All the code with test itself is available here:
https://github.com/peterbabinec/svelte-form/blob/main/src/routes/settings/page.test.ts
Just clone it, install and run the test.

@peter-bab peter-bab added the bug Something isn't working label Dec 8, 2024
@ciscoheat
Copy link
Owner

You need to mock the SvelteKit environment to make it work, see if this helps: #394 (comment)

@peter-bab
Copy link
Author

peter-bab commented Dec 9, 2024

@ciscoheat yes I checked that issue and copy pasted mocking from your test but unfortunately still I get the same error and I am not sure how to mock / resolve it 🫤 The only "workaround" is to pass applyAction: false to superForm() options but that's not optimal for obvious reasons.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants