Skip to content

Commit 715a4ea

Browse files
committed
test(e2e): Add tests to SvelteKit 1.x E2E test app
1 parent 6173846 commit 715a4ea

File tree

27 files changed

+628
-80
lines changed

27 files changed

+628
-80
lines changed

packages/e2e-tests/test-applications/sveltekit/event-proxy-server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ async function registerCallbackServerPort(serverName: string, port: string): Pro
247247
await writeFile(tmpFilePath, port, { encoding: 'utf8' });
248248
}
249249

250-
async function retrieveCallbackServerPort(serverName: string): Promise<string> {
250+
function retrieveCallbackServerPort(serverName: string): Promise<string> {
251251
const tmpFilePath = path.join(os.tmpdir(), `${TEMP_FILE_PREFIX}${serverName}`);
252-
return await readFile(tmpFilePath, 'utf8');
252+
return readFile(tmpFilePath, 'utf8');
253253
}

packages/e2e-tests/test-applications/sveltekit/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@
1010
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
1111
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
1212
"test:prod": "TEST_ENV=production playwright test",
13-
"test:dev": "TEST_ENV=development playwright test",
1413
"test:build": "pnpm install && pnpm build",
15-
"test:assert": "pnpm -v"
14+
"test:assert": "pnpm test:prod"
1615
},
1716
"dependencies": {
1817
"@sentry/sveltekit": "latest || *"
1918
},
2019
"devDependencies": {
21-
"@playwright/test": "^1.27.1",
20+
"@playwright/test": "^1.36.2",
2221
"@sentry/types": "latest || *",
2322
"@sentry/utils": "latest || *",
2423
"@sveltejs/adapter-auto": "^2.0.0",

packages/e2e-tests/test-applications/sveltekit/playwright.config.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
1-
import type { PlaywrightTestConfig } from '@playwright/test';
2-
import { devices } from '@playwright/test';
1+
import type { PlaywrightTestConfig } from "@playwright/test";
2+
import { devices } from "@playwright/test";
3+
4+
// Fix urls not resolving to localhost on Node v17+
5+
// See: https://github.com/axios/axios/issues/3821#issuecomment-1413727575
6+
import { setDefaultResultOrder } from "dns";
7+
setDefaultResultOrder("ipv4first");
38

49
const testEnv = process.env.TEST_ENV;
510

611
if (!testEnv) {
7-
throw new Error('No test env defined');
12+
throw new Error("No test env defined");
813
}
914

10-
const port = 3030;
15+
const svelteKitPort = 3030;
16+
const eventProxyPort = 3031;
1117

1218
/**
1319
* See https://playwright.dev/docs/test-configuration.
1420
*/
1521
const config: PlaywrightTestConfig = {
16-
testDir: './test',
22+
testDir: "./test",
1723
/* Maximum time one test can run for. */
1824
timeout: 150_000,
1925
expect: {
@@ -28,42 +34,44 @@ const config: PlaywrightTestConfig = {
2834
/* Fail the build on CI if you accidentally left test.only in the source code. */
2935
forbidOnly: !!process.env.CI,
3036
/* `next dev` is incredibly buggy with the app dir */
31-
retries: testEnv === 'development' ? 3 : 0,
37+
retries: testEnv === "development" ? 3 : 0,
3238
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
33-
reporter: 'list',
39+
reporter: "list",
3440
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
3541
use: {
3642
/* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */
3743
actionTimeout: 0,
3844
/* Base URL to use in actions like `await page.goto('/')`. */
39-
baseURL: `http://localhost:${port}`,
45+
baseURL: `http://localhost:${svelteKitPort}`,
4046

4147
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
42-
trace: 'on-first-retry',
48+
trace: "on-first-retry",
4349
},
4450

4551
/* Configure projects for major browsers */
4652
projects: [
4753
{
48-
name: 'chromium',
54+
name: "chromium",
4955
use: {
50-
...devices['Desktop Chrome'],
56+
...devices["Desktop Chrome"],
5157
},
5258
},
5359
],
5460

5561
/* Run your local dev server before starting the tests */
5662
webServer: [
5763
{
58-
command: 'pnpm ts-node --esm start-event-proxy.ts',
59-
port: 3031,
64+
command: "pnpm ts-node-script start-event-proxy.ts",
65+
port: eventProxyPort,
66+
reuseExistingServer: false,
6067
},
6168
{
6269
command:
63-
testEnv === 'development'
64-
? `pnpm wait-port ${port} && pnpm dev --port ${port}`
65-
: `pnpm wait-port ${port} && pnpm preview --port ${port}`,
66-
port,
70+
testEnv === "development"
71+
? `pnpm wait-port ${eventProxyPort} && pnpm dev --port ${svelteKitPort}`
72+
: `pnpm wait-port ${eventProxyPort} && pnpm preview --port ${svelteKitPort}`,
73+
port: svelteKitPort,
74+
reuseExistingServer: false,
6775
},
6876
],
6977
};
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
import { env } from '$env/dynamic/public';
2-
import * as Sentry from '@sentry/sveltekit';
1+
import { env } from "$env/dynamic/public";
2+
import * as Sentry from "@sentry/sveltekit";
3+
4+
console.log("Sentry DSN:", env.PUBLIC_E2E_TEST_DSN);
35

46
Sentry.init({
5-
environment: 'qa', // dynamic sampling bias to keep transactions
7+
environment: "qa", // dynamic sampling bias to keep transactions
68
dsn: env.PUBLIC_E2E_TEST_DSN,
79
debug: true,
810
tunnel: `http://localhost:3031/`, // proxy server
911
tracesSampleRate: 1.0,
1012
});
1113

1214
const myErrorHandler = ({ error, event }: any) => {
13-
console.error('An error occurred on the client side:', error, event);
15+
console.error("An error occurred on the client side:", error, event);
1416
};
1517

1618
export const handleError = Sentry.handleErrorWithSentry(myErrorHandler);

packages/e2e-tests/test-applications/sveltekit/src/hooks.server.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
import { env } from '$env/dynamic/private';
2-
import * as Sentry from '@sentry/sveltekit';
1+
import { env } from "$env/dynamic/private";
2+
import * as Sentry from "@sentry/sveltekit";
33

44
Sentry.init({
5-
environment: 'qa', // dynamic sampling bias to keep transactions
5+
environment: "qa", // dynamic sampling bias to keep transactions
66
dsn: env.E2E_TEST_DSN,
77
debug: true,
88
tunnel: `http://localhost:3031/`, // proxy server
99
tracesSampleRate: 1.0,
1010
});
1111

12-
const myErrorHandler = ({ error, event }: any) => {
13-
console.error('An error occurred on the server side:', error, event);
14-
};
12+
// not logging anything to console to avoid noise in the test output
13+
const myErrorHandler = ({ error, event }: any) => {};
1514

1615
export const handleError = Sentry.handleErrorWithSentry(myErrorHandler);
1716

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script lang="ts">
2+
import { onMount } from "svelte";
3+
4+
onMount(() => {
5+
// Indicate that the SvelteKit app was hydrated
6+
document.body.classList.add("hydrated");
7+
});
8+
</script>
9+
10+
<h1>Sveltekit E2E Test app</h1>
11+
<div data-sveltekit-preload-data="off">
12+
<slot></slot>
13+
</div>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,26 @@
11
<h1>Welcome to SvelteKit</h1>
22
<p>Visit <a href="https://kit.svelte.dev">kit.svelte.dev</a> to read the documentation</p>
3+
4+
<ul>
5+
<li>
6+
<a href="/client-error">Client error</a>
7+
</li>
8+
<li>
9+
<a href="/universal-load-error">Universal Load error</a>
10+
</li>
11+
<li>
12+
<a href="/server-load-error">Server Load error</a>
13+
</li>
14+
<li>
15+
<a href="/server-route-error">Server Route error</a>
16+
</li>
17+
<li>
18+
<a href="/users/123abc">Route with Params</a>
19+
</li>
20+
<li>
21+
<a href="/users">Route with Server Load</a>
22+
</li>
23+
<li>
24+
<a href="/universal-load-fetch">Route with fetch in universal load</a>
25+
</li>
26+
</ul>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const GET = () => {
2+
return new Response(JSON.stringify({ users: ["alice", "bob", "carol"] }));
3+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script lang="ts">
2+
function throwError() {
3+
throw new Error('Click Error');
4+
}
5+
</script>
6+
7+
<h1>Client error</h1>
8+
9+
<button on:click={throwError}>Throw error</button>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const load = async () => {
2+
throw new Error("Server Load Error");
3+
return {
4+
msg: "Hello World",
5+
};
6+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script lang="ts">
2+
export let data
3+
</script>
4+
5+
<h1>Server load error</h1>
6+
7+
<p>
8+
Message: {data.msg}
9+
</p>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script lang="ts">
2+
export let data;
3+
</script>
4+
5+
<h1>Server Route error</h1>
6+
7+
<p>
8+
Message: {data.msg}
9+
</p>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const load = async ({ fetch }) => {
2+
const res = await fetch("/server-route-error");
3+
const data = await res.json();
4+
return {
5+
msg: data,
6+
};
7+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const GET = async () => {
2+
throw new Error("Server Route Error");
3+
return {
4+
msg: "Hello World",
5+
};
6+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<script lang="ts">
2+
export let data
3+
</script>
4+
5+
<h1>Universal load error</h1>
6+
7+
<p>
8+
To trigger from client: Load on another route, then navigate to this route.
9+
</p>
10+
11+
<p>
12+
To trigger from server: Load on this route
13+
</p>
14+
15+
<p>
16+
Message: {data.msg}
17+
</p>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { browser } from "$app/environment";
2+
3+
export const load = async () => {
4+
throw new Error(`Universal Load Error (${browser ? "browser" : "server"})`);
5+
return {
6+
msg: "Hello World",
7+
};
8+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<script lang="ts">
2+
export let data;
3+
console.log(data);
4+
</script>
5+
6+
<h2>Fetching in universal load</h2>
7+
8+
<p>Here's a list of a few users:</p>
9+
10+
<ul>
11+
{#each data.users as user}
12+
<li>{user}</li>
13+
{/each}
14+
</ul>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const load = async ({ fetch }) => {
2+
const usersRes = await fetch("/api/users");
3+
const data = await usersRes.json();
4+
return { users: data.users };
5+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const load = async () => {
2+
return {
3+
msg: "Hi everyone!",
4+
};
5+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script lang="ts">
2+
export let data;
3+
</script>
4+
<h2>
5+
All Users:
6+
</h2>
7+
8+
<p>
9+
message: {data.msg}
10+
</p>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const load = async ({ params }) => {
2+
return {
3+
msg: `This is a special message for user ${params.id}`,
4+
};
5+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<script lang="ts">
2+
import { page } from '$app/stores';
3+
export let data;
4+
</script>
5+
6+
<h1>Route with dynamic params</h1>
7+
8+
<p>
9+
User id: {$page.params.id}
10+
</p>
11+
12+
<p>
13+
Secret message for user: {data.msg}
14+
</p>

0 commit comments

Comments
 (0)