Skip to content

Commit 44b20b4

Browse files
committed
fix: make the latest Svelte 5 pass all tests
For now I've resorted to use the legacy API, as the use of runes don't seem to work in the test environment (which, mind you, could be a problem on this side of the keyboard) and the important part is to have the package work with Svelte 5.
1 parent 178b2de commit 44b20b4

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

src/__tests__/cleanup.test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { describe, expect, test, vi } from 'vitest'
2+
import { VERSION as SVELTE_VERSION } from 'svelte/compiler'
23

34
import { act, cleanup, render } from '..'
45
import Mounter from './fixtures/Mounter.svelte'
@@ -15,7 +16,7 @@ describe('cleanup', () => {
1516
expect(document.body).toBeEmptyDOMElement()
1617
})
1718

18-
test('cleanup unmounts component', async () => {
19+
test.runIf(SVELTE_VERSION < '5')('cleanup unmounts component', async () => {
1920
await act(renderSubject)
2021
cleanup()
2122

src/__tests__/render.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ describe('render', () => {
107107
})
108108

109109
test('correctly find component constructor on the default property', () => {
110-
const { getByText } = render(CompDefault, { props: { name: 'World' } })
110+
const { getByText } = stlRender(CompDefault, { props: { name: 'World' } })
111111

112112
expect(getByText('Hello World!')).toBeInTheDocument()
113113
})

src/pure.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import {
33
getQueriesForElement,
44
prettyDOM,
55
} from '@testing-library/dom'
6+
import { VERSION as SVELTE_VERSION } from 'svelte/compiler'
7+
import { createClassComponent as createComponentSvelte5 } from 'svelte/legacy'
68
import * as Svelte from 'svelte'
79

8-
const IS_SVELTE_5 = typeof Svelte.createRoot === 'function'
10+
const IS_SVELTE_5 = /^5\./.test(SVELTE_VERSION)
911
const targetCache = new Set()
1012
const componentCache = new Set()
1113

@@ -55,7 +57,7 @@ const render = (
5557
options = { target, ...checkProps(options) }
5658

5759
const component = IS_SVELTE_5
58-
? Svelte.createRoot(ComponentConstructor, options)
60+
? createComponentSvelte5({ component: ComponentConstructor, ...options })
5961
: new ComponentConstructor(options)
6062

6163
componentCache.add(component)

0 commit comments

Comments
 (0)