8000 Fix/@sveltejs/svelte/16114 by raythurnvoid · Pull Request #16263 · sveltejs/svelte · GitHub
[go: up one dir, main page]

Skip to content

Fix/@sveltejs/svelte/16114 #16263

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

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add tests for derived old values in teardown functions
  • Loading branch information
raythurnvoid committed Jun 29, 2025
commit 1fc9f0801c1ec583da9c69986925887377b8fffa
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
async test({ assert, logs, target }) {
/** @type {HTMLButtonElement | null} */
const increment_btn = target.querySelector('#increment');
/** @type {HTMLButtonElement | null} */
const overwrite_btn = target.querySelector('#overwrite');

// Initial state: count=1, derived_value=1

// Click to increment count: count=2, der 8000 ived_value=4
flushSync(() => {
increment_btn?.click();
});

// Click to increment count: count=3, derived_value=9
flushSync(() => {
increment_btn?.click();
});

// Click to overwrite derived_value: count=3, derived_value=7
flushSync(() => {
overwrite_btn?.click();
});

// Should log old value during cleanup (4) and new value during setup (9)
assert.deepEqual(logs, [
'$effect: 1',
'$effect teardown: 1',
'$effect: 4',
'$effect teardown: 4',
'$effect: 9',
'$effect teardown: 9',
'$effect: 7'
]);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script>
let count = $state(1);
let derived_value = $derived(count * count);

$effect(() => {
console.log(`$effect: ${derived_value}`);
return () => {
console.log(`$effect teardown: ${derived_value}`);
};
});
</script>

<button id="increment" onclick={() => count++}>Increment s</button>
<button id="overwrite" onclick={() => (derived_value = 7)}>Overwrite derived_value</button>

<p>count: {count}</p>
<p>derived_value: {derived_value}</p>
0