Skip to content

Commit 75a3f38

Browse files
authoredJul 25, 2024
fix(svelte5): synchronously flush changes after mount and unmount (#396)
1 parent f5b7fe1 commit 75a3f38

File tree

2 files changed

+3
-5
lines changed

2 files changed

+3
-5
lines changed
 

‎src/__tests__/mount.test.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { act, render, screen } from '@testing-library/svelte'
1+
import { render, screen } from '@testing-library/svelte'
22
import { describe, expect, test, vi } from 'vitest'
33

44
import Mounter from './fixtures/Mounter.svelte'
@@ -14,20 +14,17 @@ describe('mount and destroy', () => {
1414
const content = screen.getByRole('button')
1515

1616
expect(content).toBeInTheDocument()
17-
await act()
1817
expect(onMounted).toHaveBeenCalledTimes(1)
1918
})
2019

2120
test('component is destroyed', async () => {
2221
const { unmount } = renderSubject()
2322

24-
await act()
2523
unmount()
2624

2725
const content = screen.queryByRole('button')
2826

2927
expect(content).not.toBeInTheDocument()
30-
await act()
3128
expect(onDestroyed).toHaveBeenCalledTimes(1)
3229
})
3330
})

‎src/core/modern.svelte.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const mount = (Component, options) => {
2626
const props = $state(options.props ?? {})
2727
const component = Svelte.mount(Component, { ...options, props })
2828

29+
Svelte.flushSync()
2930
propsByComponent.set(component, props)
3031

3132
return component
@@ -34,7 +35,7 @@ const mount = (Component, options) => {
3435
/** Remove the component from the DOM. */
3536
const unmount = (component) => {
3637
propsByComponent.delete(component)
37-
Svelte.unmount(component)
38+
Svelte.flushSync(() => Svelte.unmount(component))
3839
}
3940

4041
/**

0 commit comments

Comments
 (0)