I'm very new to svelte and TS, I'm trying to do a simple import and when I run vitest is fails with the following error
Unexpected token 1 | <script lang="ts"> 2 | import { createEventDispatcher } from 'svelte'; 3 | import type { Choice } from './../../types'; ^ 4 | export let choices: Choice[] 5 | const dispatch = createEventDispatcher<{handleChooseEvent: String}>();
The code complies and runs as expected on localhost but trying to write a test is causing the error.
this is my types.ts
// Define the Choice interfaceexport interface Choice { text: String; id: Number; nextScene?: String; //? makes it optional}
chatgtp told me to us this types.ts, I'm not sure if this is the right approach
I verified that typescript is installed, and added it to my vite.config.ts
import { sveltekit } from '@sveltejs/kit/vite';import typescript from '@rollup/plugin-typescript';import { defineConfig } from 'vitest/config';export default defineConfig({ plugins: [ sveltekit(), // Keep the SvelteKit plugin typescript() ], test: { include: ['**/*.{test,spec}.{js,ts}'] }});
and this is my vitetest.config.ts
import { defineConfig } from 'vitest/config';import { svelte } from '@sveltejs/vite-plugin-svelte'import typescript from '@rollup/plugin-typescript';import sveltePreprocess from 'svelte-preprocess'export default defineConfig({ plugins: [ svelte({ hot: !process.env.VITEST, preprocess: [sveltePreprocess({ typescript: true })] // <-- this line allow vitest to parse svelte component using typescript }), typescript() ], test: { include: ['imports/**/*.vitest.{js,ts}'], globals: true, environment: 'jsdom', }, })
I also verified that typescript is in my package.json