How do I solve " Failed to resolve import "$app/paths" with Svelte and Vitest

I have been working on Svelte library, and I just started configuring and writing unit tests for it. Vitest was set up and configured, but when running tests I Was getting an error like this:

view plain print about
1FAIL src/tests/my-class.spec.ts [ src/tests/my-class.spec.ts ]
2Error: Failed to resolve import "$app/paths" from "src/lib/my-class.ts". Does the file exist?
3 Plugin: vite:import-analysis
4 File: C:/Dev/src/lib/my-class.ts:5:21
5 1 | import { base } from "$app/paths";

The $app/paths import is part of SvelteKit, a server side rendering tool often used with the Svelte UI Framework.

My problem came from the vitest.config.ts file. I was setting up some plugins like this:

view plain print about
1import { svelte } from '@sveltejs/vite-plugin-svelte';
2import {svelteTesting} from "@testing-library/svelte/vite";
3
4export default defineConfig({
5 plugins: [svelte(), svelteTesting()],
6// other configs
7})

I needed to replace the svelte() plugin with a svelteKit plugin. Super easy fix:

view plain print about
1import { sveltekit } from '@sveltejs/kit/vite';
2import {svelteTesting} from "@testing-library/svelte/vite";
3
4export default defineConfig({
5 plugins: [sveltekit(), svelteTesting()],
6// other configs
7})

Then my code started running properly.

Six out of the seven Copilot generated tests failed, but that is perhaps a story for another day.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)

Comments are not allowed for this entry.

More Entries