Skip to content

Commit 3fa5a67

Browse files
committed
add tests
1 parent 4d693d1 commit 3fa5a67

File tree

14 files changed

+205
-1
lines changed

14 files changed

+205
-1
lines changed

packages/svelte/src/compiler/phases/3-transform/client/visitors/SvelteBoundary.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ export function SvelteBoundary(node, context) {
5959
const init = [];
6060
const block_state = { ...context.state, init };
6161
context.visit(child, block_state);
62-
const current = props_and_spreads.at(-1) ?? props_and_spreads.push([]);
6362
push_prop(b.prop('init', b.id('failed'), b.id('failed')));
6463
snippet_statements.push(...init);
6564
} else {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
test({ assert, target, logs }) {
6+
const btn = target.querySelector('button');
7+
8+
btn?.click();
9+
flushSync();
10+
11+
assert.deepEqual(logs, ['error caught']);
12+
}
13+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script>
2+
function throw_error() {
3+
throw new Error('test')
4+
}
5+
6+
let count = $state(0);
7+
</script>
8+
9+
<svelte:boundary onerror={(e) => console.log('error caught')}>
10+
{count > 0 ? throw_error() : null}
11+
</svelte:boundary>
12+
13+
<button onclick={() => count++}>+</button>
14+
15+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
test({ assert, target, logs }) {
6+
const btn = target.querySelector('button');
7+
8+
btn?.click();
9+
flushSync();
10+
11+
assert.deepEqual(logs, ['error caught']);
12+
assert.htmlEqual(target.innerHTML, `<div>Fallback!</div><button>+</button>`);
13+
}
14+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<script>
2+
function throw_error() {
3+
throw new Error('test')
4+
}
5+
6+
let count = $state(0);
7+
</script>
8+
9+
<svelte:boundary onerror={(e) => console.log('error caught')}>
10+
{count > 0 ? throw_error() : null}
11+
12+
{#snippet failed()}
13+
<div>Fallback!</div>
14+
{/snippet}
15+
</svelte:boundary>
16+
17+
<button onclick={() => count++}>+</button>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
test({ assert, target, logs }) {
6+
const btn = target.querySelector('button');
7+
8+
btn?.click();
9+
flushSync();
10+
11+
assert.deepEqual(logs, ['error caught']);
12+
assert.htmlEqual(target.innerHTML, `<div>Fallback!</div><button>+</button>`);
13+
}
14+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<script>
2+
function throw_error() {
3+
throw new Error('test')
4+
}
5+
6+
let count = $state(0);
7+
</script>
8+
9+
{#snippet failed()}
10+
<div>Fallback!</div>
11+
{/snippet}
12+
13+
<svelte:boundary {failed} onerror={(e) => console.log('error caught')}>
14+
{count > 0 ? throw_error() : null}
15+
</svelte:boundary>
16+
17+
<button onclick={() => count++}>+</button>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script>
2+
let count = $state(0);
3+
4+
$effect.pre(() => {
5+
if (count > 1) {
6+
throw new Error('too high');
7+
}
8+
});
9+
</script>
10+
11+
{count}
12+
13+
<button onclick={() => count++}>+</button>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
test({ assert, target, logs }) {
6+
let btn = target.querySelector('button');
7+
8+
btn?.click();
9+
btn?.click();
10+
flushSync();
11+
12+
assert.deepEqual(logs, ['error caught']);
13+
assert.htmlEqual(target.innerHTML, `<div>too high</div><button>Retry</button>`);
14+
15+
const [btn2] = target.querySelectorAll('button');
16+
17+
btn2?.click();
18+
flushSync();
19+
20+
assert.htmlEqual(target.innerHTML, `0\n<button>+</button>`);
21+
22+
btn = target.querySelector('button');
23+
24+
btn?.click();
25+
btn?.click();
26+
flushSync();
27+
28+
assert.deepEqual(logs, ['error caught', 'error caught']);
29+
assert.htmlEqual(target.innerHTML, `<div>too high</div><button>Retry</button>`);
30+
}
31+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<script>
2+
import Child from './Child.svelte';
3+
</script>
4+
5+
<svelte:boundary onerror={(e) => console.log('error caught')}>
6+
<svelte:boundary onerror={(e) => { throw e }}>
7+
<svelte:boundary>
8+
<Child />
9+
</svelte:boundary>
10+
</svelte:boundary>
11+
12+
{#snippet failed(e, retry)}
13+
<div>{e.message}</div>
14+
<button onclick={retry}>Retry</button>
15+
{/snippet}
16+
</svelte:boundary>
17+
18+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
test({ assert, target }) {
6+
const btn = target.querySelector('button');
7+
8+
btn?.click();
9+
flushSync();
10+
11+
assert.htmlEqual(target.innerHTML, `<button>+</button><div>There is an error!</div>`);
12+
}
13+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<script>
2+
function throw_error() {
3+
throw new Error('test')
4+
}
5+
6+
let count = $state(0);
7+
let error = $state();
8+
</script>
9+
10+
<svelte:boundary onerror={(e) => error = e}>
11+
{count > 0 ? throw_error() : null}
12+
</svelte:boundary>
13+
14+
<button onclick={() => count++}>+</button>
15+
16+
{#if error}
17+
<div>There is an error!</div>
18+
{/if}
19+
20+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
mode: ['client'],
5+
6+
test({ assert, logs }) {
7+
assert.deepEqual(logs, ['error caught']);
8+
}
9+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
function throw_error() {
3+
throw new Error('test')
4+
}
5+
</script>
6+
7+
<svelte:boundary onerror={(e) => console.log('error caught')}>
8+
{throw_error()}
9+
</svelte:boundary>
10+
11+

0 commit comments

Comments
 (0)