How to use svelte.config.js
with Vitest similar to vite.config.js
#331
-
Im working on a Sveltekit project where I'm using Vitest to implement unit tests. import adapter from '@sveltejs/adapter-auto';
import preprocess from 'svelte-preprocess';
const config = {
preprocess: preprocess(),
kit: {
adapter: adapter(),
target: '#svelte',
vite: {
resolve: {
alias: {
$i18n: path.resolve('./src/i18n'),
$lib: path.resolve('./src/lib'),
},
},
},
},
}; Where import { LL } from '$i18n/i18n-svelte'; But when executing tests with Vitest I get the following error:
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. I can see in the docs, under the Configure Vitest section:
I don't see a way to provide custom alias to |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 11 replies
-
I think it requires support from Svelte directly to provide their internal plugins. Similar to how iles does it https://twitter.com/ilesjs/status/1470821030289629195 |
Beta Was this translation helpful? Give feedback.
-
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 './svelte.config.js'
export default defineConfig({
...sveltekitViteConfig as UserConfig,
plugins: [
svelte({ hot: !process.env.VITEST }),
],
test: {
// vitest config here
global: true,
environment: 'jsdom',
},
}) this only works for self-contained components though, routes and other things that rely on sveltekit internals won't work out of the box |
Beta Was this translation helpful? Give feedback.
-
Hey all, I've published a package that essentially automates this configuration, plus a few niceties around |
Beta Was this translation helpful? Give feedback.
-
I've refactored SvelteKit such that it's essentially just a Vite plugin in dev mode. I will add support for preview mode as well, but that depends on Vite 3. I'd love for folks to try it out with Vitest. See here: sveltejs/kit#5094 |
Beta Was this translation helpful? Give feedback.
for component testing you can use a separate vite+svelte setup and share aliases like this
svelte.config.js
vitest.config.ts