Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions documentation/docs/07-misc/02-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,27 @@ test('Multiplier', () => {
});
```

```js
/// file: multiplier.js
/**
* @param {number} initial
* @param {number} mult
*/
export function multiplier(initial, mult) {
let count = initial;

return {
get value() {
return count * mult;
},
/** @param {number} c */
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Member Author

Choose a reason for hiding this comment

The 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 lang is always 'ts' - I have no idea how this worked until now, or if only by chance TS did fine so far. What was the reason for switching to hard-coding that as part of the shiki update in #288?

Copy link
Member Author

Choose a reason for hiding this comment

The 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.
Expand All @@ -75,6 +96,21 @@ test('Multiplier', () => {
});
```

```js
/// file: multiplier.svelte.js
/**
* @param {() => number} getCount
* @param {number} mult
*/
export function multiplier(getCount, mult) {
return {
get value() {
return getCount() * mult;
}
};
}
```

If the code being tested uses effects, you need to wrap the test inside `$effect.root`:

```js
Expand Down Expand Up @@ -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.
Expand Down
Loading