Skip to content

Commit 17917ee

Browse files
committed
test(e2e): add simple Svelte 5 suite
1 parent 577ec8d commit 17917ee

File tree

11 files changed

+314
-1
lines changed

11 files changed

+314
-1
lines changed

e2e/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,17 @@ test('should show the proper label', () => {
8181
npm run test
8282
```
8383

84+
## Svelte 5 (ESM)
85+
86+
A minimal Svelte 5 test to ensure Svelte 5 components and `.svelte.js` modules work with Jest.
87+
88+
### Bootstrapping the project
89+
90+
No special steps were taken to create this suite. It only contains a few components and their associated tests.
91+
92+
### Run the tests
93+
94+
```shell
95+
pnpm build
96+
pnpm e2e:svelte-5
97+
```

e2e/svelte-5/package.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "@svelte-jester-e2e/svelte-5",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module",
6+
"scripts": {
7+
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules --no-warnings\" jest --coverage",
8+
"test:watch": "pnpm run test --watch"
9+
},
10+
"devDependencies": {
11+
"jest": "^29.7.0",
12+
"jest-environment-jsdom": "^29.7.0",
13+
"svelte": "^5.0.0-next.136",
14+
"svelte-jester": "workspace:*"
15+
},
16+
"dependenciesMeta": {
17+
"svelte-jester": {
18+
"injected": true
19+
}
20+
},
21+
"jest": {
22+
"transform": {
23+
"^.+\\.svelte(?:\\.js)?$": [
24+
"svelte-jester"
25+
]
26+
},
27+
"moduleFileExtensions": [
28+
"js",
29+
"svelte"
30+
],
31+
"extensionsToTreatAsEsm": [
32+
".svelte"
33+
],
34+
"testEnvironment": "jsdom"
35+
}
36+
}

e2e/svelte-5/src/legacy.svelte

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
export let name;
3+
let showGreeting = false;
4+
5+
const handleClick = () => (showGreeting = true);
6+
</script>
7+
8+
<button on:click={handleClick}>greet</button>
9+
{#if showGreeting}
10+
<p>hello {name}</p>
11+
{/if}

e2e/svelte-5/src/legacy.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { test, afterEach } from "@jest/globals";
2+
import { mount, unmount, tick } from "svelte";
3+
4+
import Subject from "./legacy.svelte";
5+
6+
let component;
7+
8+
afterEach(() => {
9+
unmount(component);
10+
component = undefined;
11+
});
12+
13+
test("render", () => {
14+
component = mount(Subject, {
15+
target: document.body,
16+
props: { name: "alice" },
17+
});
18+
19+
const button = document.querySelector("button");
20+
21+
expect(button).toBeInstanceOf(HTMLButtonElement);
22+
});
23+
24+
test("interaction", async () => {
25+
component = mount(Subject, {
26+
target: document.body,
27+
props: { name: "alice" },
28+
});
29+
30+
const button = document.querySelector("button");
31+
32+
button.click();
33+
await tick();
34+
35+
const message = document.querySelector("p");
36+
37+
expect(message.textContent).toMatch(/hello alice/iu);
38+
});

e2e/svelte-5/src/modern.svelte

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
let { name } = $props();
3+
let showGreeting = $state(false);
4+
5+
const handleClick = () => (showGreeting = true);
6+
</script>
7+
8+
<button onclick={handleClick}>greet</button>
9+
{#if showGreeting}
10+
<p>hello {name}</p>
11+
{/if}

e2e/svelte-5/src/modern.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { test, afterEach } from "@jest/globals";
2+
import { mount, unmount, tick } from "svelte";
3+
4+
import Subject from "./modern.svelte";
5+
6+
let component;
7+
8+
afterEach(() => {
9+
unmount(component);
10+
component = undefined;
11+
});
12+
13+
test("render", () => {
14+
component = mount(Subject, {
15+
target: document.body,
16+
props: { name: "alice" },
17+
});
18+
19+
const button = document.querySelector("button");
20+
21+
expect(button).toBeInstanceOf(HTMLButtonElement);
22+
});
23+
24+
test("interaction", async () => {
25+
component = mount(Subject, {
26+
target: document.body,
27+
props: { name: "alice" },
28+
});
29+
30+
const button = document.querySelector("button");
31+
32+
button.click();
33+
await tick();
34+
35+
const message = document.querySelector("p");
36+
37+
expect(message.textContent).toMatch(/hello alice/iu);
38+
});

e2e/svelte-5/src/module.svelte.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const createCounter = () => {
2+
let count = $state(0);
3+
4+
return {
5+
get count() {
6+
return count;
7+
},
8+
9+
increment() {
10+
count = count + 1;
11+
},
12+
};
13+
};

e2e/svelte-5/src/module.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { test } from "@jest/globals";
2+
3+
import * as Subject from "./module.svelte.js";
4+
5+
test("get current count", () => {
6+
const subject = Subject.createCounter();
7+
const result = subject.count;
8+
9+
expect(result).toBe(0);
10+
});
11+
12+
test("increment", () => {
13+
const subject = Subject.createCounter();
14+
15+
subject.increment();
16+
const result = subject.count;
17+
18+
expect(result).toBe(1);
19+
});

e2e/svelte-5/svelte.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default {};

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@
3737
"test": "pnpm test:esm && pnpm test:cjs",
3838
"test:esm": "pnpm build && cross-env NODE_OPTIONS=--experimental-vm-modules jest src/__tests__/transformer.test.js",
3939
"test:cjs": "pnpm build && jest src/__tests__/transformer.test.cjs",
40-
"e2e": "pnpm e2e:svelte && pnpm e2e:sveltekit",
40+
"e2e": "pnpm e2e:svelte && pnpm e2e:sveltekit && pnpm e2e:svelte-5",
4141
"e2e:svelte": "cd e2e/svelte && pnpm test",
4242
"e2e:sveltekit": "cd e2e/sveltekit && pnpm test",
43+
"e2e:svelte-5": "cd e2e/svelte-5 && pnpm test",
4344
"test:nocache": "pnpm install && jest --clearCache && pnpm test -- --no-cache",
4445
"test:watch": "pnpm test -- --watch",
4546
"test:update": "pnpm test -- --updateSnapshot --coverage",

0 commit comments

Comments
 (0)