Getting this error in files where I am using $app/navigation:
Error: Failed to resolve import "$app/navigation" from "src/utils/navigationUtils.js". Does the file exist?.
Here is my setupTests.js file but now getting the same error in this file.
import "@testing-library/jest-dom";
import { vi } from "vitest";
import * as navigation from "$app/navigation";
// Mock SvelteKit runtime module $app/navigation
vi.mock("$app/navigation", () => ({
afterNavigate: () => {},
beforeNavigate: () => {},
disableScrollHandling: () => {},
goto: () => Promise.resolve(),
invalidate: () => Promise.resolve(),
invalidateAll: () => Promise.resolve(),
prefetch: () => Promise.resolve(),
prefetchRoutes: () => Promise.resolve(),
}));
After this configration now getting:
Error: Failed to resolve import "$app/navigation" from "setupTests.js". Does the file exist?
I have tried to mock the $app/navigation module in setupTests.js file but no success.
For any of the
$xxxpaths in sveltekit, e.g.$env,$app,$lib, you can create a__mocks__/xxxdirectory where you can put your navigation mocks to be returned, so something like__mocks__/appin your case. You can then put anavigation.tswith your mock which will be loaded for each of your tests. Then in yourvitest.config.tsaddThis will allow you to import a version you contrive into your tests.