Skip to content

Commit

Permalink
docs: add code of files being tested
Browse files Browse the repository at this point in the history
closes #14900
  • Loading branch information
dummdidumm committed Jan 7, 2025
1 parent 7737868 commit b2fc58d
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 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,26 @@ 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;
},
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 +95,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 +140,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

0 comments on commit b2fc58d

Please sign in to comment.