Skip to content
\n

Where config.kit.vite defines vite.config.js related configuration.
\nIn my tests I import a Svelte component which imports the following module using the $i18n alias in the path:

\n
import { LL } from '$i18n/i18n-svelte';
\n

But when executing tests with Vitest I get the following error:

\n
 Failed to resolve import \"$i18n/i18n-svelte\" from \"{file directory}\". Does the file exist?\n
\n

Even though the module gets resolved when running the project using the development server, I can't get to resolve it using the alias configuration via Vitest.

\n

I can see in the docs, under the Configure Vitest section:

\n
vitest will read your root vite.config.ts to match with the plugins and setup as your Vite app. For example, your Vite resolve.alias and plugins configuration will work out-of-the-box.\n
\n

I don't see a way to provide custom alias to vitest.config.js (https://vitest.dev/config/), If I could import the config from svelte.config.js and provide the config.kit.vite configuration would be great.

","upvoteCount":1,"answerCount":4,"acceptedAnswer":{"@type":"Answer","text":"

for component testing you can use a separate vite+svelte setup and share aliases like this

\n

svelte.config.js

\n
export const sveltekitViteConfig = {\n  resolve: {\n    alias: {\n      $i18n: path.resolve('./src/i18n'),\n      $lib: path.resolve('./src/lib'),\n    },\n  },\n}\n\n/** @type {import('@sveltejs/kit').Config} */\nconst config = {\n  kit: {\n    adapter: adapter(),\n\n    // hydrate the <div id=\"svelte\"> element in src/app.html\n    target: '#svelte',\n    vite: sveltekitViteConfig,\n  },\n}\n\nexport default config
\n

vitest.config.ts

\n
import type { UserConfig } from 'vite'\nimport { defineConfig } from 'vite'\nimport { svelte } from '@sveltejs/vite-plugin-svelte'\nimport { sveltekitViteConfig } from './svelte.config.js'\n\nexport default defineConfig({\n  ...sveltekitViteConfig as UserConfig,\n  plugins: [\n    svelte({ hot: !process.env.VITEST }),\n  ],\n  test: {\n    // vitest config here\n    global: true,\n    environment: 'jsdom',\n  },\n})
\n

this only works for self-contained components though, routes and other things that rely on sveltekit internals won't work out of the box

","upvoteCount":4,"url":"https://github.com/vitest-dev/vitest/discussions/331#discussioncomment-1875348"}}}

How to use svelte.config.js with Vitest similar to vite.config.js #331

Answered by dominikg
EstebanBorai asked this question in Q&A
Discussion options

You must be logged in to vote

for component testing you can use a separate vite+svelte setup and share aliases like this

svelte.config.js

export const sveltekitViteConfig = {
  resolve: {
    alias: {
      $i18n: path.resolve('./src/i18n'),
      $lib: path.resolve('./src/lib'),
    },
  },
}

/** @type {import('@sveltejs/kit').Config} */
const config = {
  kit: {
    adapter: adapter(),

    // hydrate the <div id="svelte"> element in src/app.html
    target: '#svelte',
    vite: sveltekitViteConfig,
  },
}

export default config

vitest.config.ts

import type { UserConfig } from 'vite'
import { defineConfig } from 'vite'
import { svelte } from '@sveltejs/vite-plugin-svelte'
import { sveltekitViteConfig } from './svel…

Replies: 4 comments 11 replies

Comment options

You must be logged in to vote
2 replies
@EstebanBorai
Comment options

@benmccann
Comment options

Comment options

You must be logged in to vote
7 replies
@benmccann
Comment options

@EstebanBorai
Comment options

@cibernox
Comment options

@EstebanBorai
Comment options

@benmccann
Comment options

Answer selected by EstebanBorai
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@nickbreaton
Comment options

@benmccann
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
6 participants