Skip to content

Commit 996ff5f

Browse files
authored
fix: ensure importing $app/navigation in unit tests work (#14195)
* add fix and test * changeset * format * can't colocate vitest and playwright tests * Apply suggestion from @eltigerchino * Apply suggestion from @eltigerchino * Apply suggestion from @eltigerchino * clean up comment
1 parent 1fc2a73 commit 996ff5f

File tree

8 files changed

+240
-76
lines changed

8 files changed

+240
-76
lines changed

.changeset/thin-badgers-cough.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: ensure importing from `$app/navigation` works in test files

packages/kit/src/runtime/client/client.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,10 @@ let target;
185185
export let app;
186186

187187
/** @type {Record<string, any>} */
188-
export const remote_responses = __SVELTEKIT_PAYLOAD__.data ?? {};
188+
// we have to conditionally access the properties of `__SVELTEKIT_PAYLOAD__`
189+
// because it will be `undefined` when users import the exports from this module.
190+
// It's only defined when the server renders a page.
191+
export const remote_responses = __SVELTEKIT_PAYLOAD__?.data ?? {};
189192

190193
/** @type {Array<((url: URL) => boolean)>} */
191194
const invalidated = [];

packages/kit/test/apps/basics/package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,28 @@
88
"preview": "vite preview",
99
"prepare": "svelte-kit sync",
1010
"check": "svelte-kit sync && tsc && svelte-check",
11-
"test": "node test/setup.js && pnpm test:dev && pnpm test:build",
11+
"test": "node test/setup.js && pnpm test:unit && pnpm test:dev && pnpm test:build",
1212
"test:dev": "rm -rf test/errors.json && DEV=true playwright test",
1313
"test:build": "rm -rf test/errors.json && PUBLIC_PRERENDERING=false playwright test",
1414
"test:cross-platform:dev": "node test/setup.js && rm -rf test/errors.json && DEV=true playwright test test/cross-platform/",
1515
"test:cross-platform:build": "node test/setup.js && rm -rf test/errors.json && playwright test test/cross-platform/",
1616
"test:server-side-route-resolution:dev": "node test/setup.js && rm -rf test/errors.json && DEV=true ROUTER_RESOLUTION=server playwright test",
17-
"test:server-side-route-resolution:build": "node test/setup.js && rm -rf test/errors.json && PUBLIC_PRERENDERING=false ROUTER_RESOLUTION=server playwright test"
17+
"test:server-side-route-resolution:build": "node test/setup.js && rm -rf test/errors.json && PUBLIC_PRERENDERING=false ROUTER_RESOLUTION=server playwright test",
18+
"test:unit": "vitest run"
1819
},
1920
"devDependencies": {
2021
"@opentelemetry/api": "^1.9.0",
2122
"@opentelemetry/sdk-node": "^0.203.0",
2223
"@opentelemetry/sdk-trace-node": "^2.0.1",
2324
"@sveltejs/kit": "workspace:^",
2425
"@sveltejs/vite-plugin-svelte": "catalog:",
26+
"@vitest/browser": "^3.2.4",
2527
"svelte": "^5.35.5",
2628
"svelte-check": "^4.1.1",
2729
"test-redirect-importer": "workspace:*",
2830
"typescript": "^5.5.4",
29-
"vite": "catalog:"
31+
"vite": "catalog:",
32+
"vitest": "catalog:"
3033
},
3134
"type": "module"
3235
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { expect, test } from 'vitest';
2+
3+
// some users will import these modules to mock them in Vitest so we need to test
4+
// that importing them works
5+
test('$app/navigation can be imported', async () => {
6+
await expect(import('$app/navigation')).resolves.not.toThrow();
7+
});

packages/kit/test/apps/basics/vite.config.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as path from 'node:path';
22
import { sveltekit } from '@sveltejs/kit/vite';
3+
import { defineConfig } from 'vitest/config';
34

4-
/** @type {import('vite').UserConfig} */
5-
const config = {
5+
export default defineConfig({
66
build: {
77
minify: false
88
},
@@ -17,7 +17,25 @@ const config = {
1717
fs: {
1818
allow: [path.resolve('../../../src')]
1919
}
20+
},
21+
test: {
22+
expect: { requireAssertions: true },
23+
projects: [
24+
{
25+
extends: './vite.config.js',
26+
test: {
27+
name: 'client',
28+
environment: 'browser',
29+
browser: {
30+
enabled: true,
31+
provider: 'playwright',
32+
instances: [{ browser: 'chromium' }],
33+
headless: true
34+
},
35+
include: ['unit-test/**/*.spec.js'],
36+
setupFiles: ['./vitest-setup-client.ts']
37+
}
38+
}
39+
]
2040
}
21-
};
22-
23-
export default config;
41+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/// <reference types="@vitest/browser/matchers" />
2+
/// <reference types="@vitest/browser/providers/playwright" />

0 commit comments

Comments
 (0)