-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
docs: add code of files being tested #14925
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
b2fc58d
e6ff92d
c938248
897fe52
25d36c0
3cbf363
2cee806
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,6 +53,27 @@ test('Multiplier', () => { | |
| }); | ||
| ``` | ||
|
|
||
| ```js | ||
| /// file: multiplier.js | ||
| /** | ||
| * @param {number} initial | ||
| * @param {number} mult | ||
| */ | ||
| export function multiplier(initial, mult) { | ||
dummdidumm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let count = initial; | ||
dummdidumm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return { | ||
| get value() { | ||
| return count * mult; | ||
dummdidumm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }, | ||
| /** @param {number} c */ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The JSDoc -> TS conversion code here doesn't know how to deal with annotated methods, only function and variable declarations. We'll need to update it
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adjusted that, still fails - turns out it's because of https://github.com/sveltejs/svelte.dev/blob/main/packages/site-kit/src/lib/markdown/renderer.ts#L702 where
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it's because twoslash does not run on JS files by default - will workaround it |
||
| set: (c) => { | ||
| count = c; | ||
| } | ||
| }; | ||
| } | ||
| ``` | ||
|
|
||
| ### Using runes inside your test files | ||
|
|
||
| It is possible to use runes inside your test files. First ensure your bundler knows to route the file through the Svelte compiler before running the test by adding `.svelte` to the filename (e.g `multiplier.svelte.test.js`). After that, you can use runes inside your tests. | ||
dummdidumm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
@@ -75,6 +96,21 @@ test('Multiplier', () => { | |
| }); | ||
| ``` | ||
|
|
||
| ```js | ||
| /// file: multiplier.svelte.js | ||
| /** | ||
| * @param {() => number} getCount | ||
| * @param {number} mult | ||
dummdidumm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| export function multiplier(getCount, mult) { | ||
dummdidumm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return { | ||
| get value() { | ||
| return getCount() * mult; | ||
dummdidumm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| }; | ||
| } | ||
| ``` | ||
|
|
||
| If the code being tested uses effects, you need to wrap the test inside `$effect.root`: | ||
|
|
||
| ```js | ||
|
|
@@ -105,6 +141,27 @@ test('Effect', () => { | |
| }); | ||
| ``` | ||
|
|
||
| ```js | ||
| /// file: logger.svelte.js | ||
| /** | ||
| * @param {() => any} getValue | ||
| */ | ||
| export function logger(getValue) { | ||
| /** @type {any[]} */ | ||
| let log = $state([]); | ||
|
|
||
| $effect(() => { | ||
| log.push(getValue()); | ||
| }); | ||
|
|
||
| return { | ||
| get value() { | ||
| return log; | ||
| } | ||
| }; | ||
| } | ||
| ``` | ||
|
|
||
| ### Component testing | ||
|
|
||
| It is possible to test your components in isolation using Vitest. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.