Skip to content

Commit 40973c5

Browse files
authored
fix: $destroy and createRoot are no more (#328)
* 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 40973c5

21 files changed

+208
-94
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,21 @@ This library has `peerDependencies` listings for `svelte >= 3`.
8080
You may also be interested in installing `@testing-library/jest-dom` so you can use
8181
[the custom jest matchers](https://github.com/testing-library/jest-dom).
8282

83+
### Svelte 5 support
84+
85+
If you are riding the bleeding edge of Svelte 5, you'll need to either
86+
import from `@testing-library/svelte/svelte5` instead of `@testing-library/svelte`, or have your `vite.config.js` contains the following alias:
87+
88+
```
89+
export default defineConfig(({ }) => ({
90+
test: {
91+
alias: {
92+
'@testing-library/svelte': '@testing-library/svelte/svelte5'
93+
}
94+
},
95+
}))
96+
```
97+
8398
## Docs
8499

85100
See the [**docs**](https://testing-library.com/docs/svelte-testing-library/intro) over at the Testing Library website.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
"types": "./types/index.d.ts",
99
"default": "./src/index.js"
1010
},
11+
"./svelte5": {
12+
"types": "./types/index.d.ts",
13+
"default": "./src/svelte5-index.js"
14+
},
1115
"./vitest": {
1216
"default": "./src/vitest.js"
1317
}

src/__tests__/act.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { beforeEach, describe, expect, test } from 'vitest'
22

3-
import { act, fireEvent, render as stlRender } from '..'
3+
import { act, fireEvent, render as stlRender } from '@testing-library/svelte'
44
import Comp from './fixtures/Comp.svelte'
55

66
describe('act', () => {

src/__tests__/auto-cleanup-skip.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ describe('auto-cleanup-skip', () => {
77

88
beforeAll(async () => {
99
process.env.STL_SKIP_AUTO_CLEANUP = 'true'
10-
const stl = await import('..')
10+
const stl = await import('@testing-library/svelte')
1111
render = stl.render
1212
})
1313

src/__tests__/auto-cleanup.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, test } from 'vitest'
22

3-
import { render } from '..'
3+
import { render } from '@testing-library/svelte'
44
import Comp from './fixtures/Comp.svelte'
55

66
describe('auto-cleanup', () => {

src/__tests__/cleanup.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, test, vi } from 'vitest'
2+
import { VERSION as SVELTE_VERSION } from 'svelte/compiler'
23

3-
import { act, cleanup, render } from '..'
4+
import { act, cleanup, render } from '@testing-library/svelte'
45
import Mounter from './fixtures/Mounter.svelte'
56

67
const onExecuted = vi.fn()
@@ -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__/context.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { expect, test } from 'vitest'
22

3-
import { render } from '..'
3+
import { render } from '@testing-library/svelte'
44
import Comp from './fixtures/Context.svelte'
5+
import { IS_HAPPYDOM, IS_SVELTE_5 } from './utils.js'
56

6-
test('can set a context', () => {
7+
test.skipIf(IS_SVELTE_5 && IS_HAPPYDOM)('can set a context', () => {
78
const message = 'Got it'
89

910
const { getByText } = render(Comp, {

src/__tests__/debug.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { prettyDOM } from '@testing-library/dom'
22
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
33

4-
import { render } from '..'
4+
import { render } from '@testing-library/svelte'
55
import Comp from './fixtures/Comp.svelte'
66

77
describe('debug', () => {

src/__tests__/events.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, test } from 'vitest'
22

3-
import { fireEvent, render } from '..'
3+
import { fireEvent, render } from '@testing-library/svelte'
44
import Comp from './fixtures/Comp.svelte'
55

66
describe('events', () => {

src/__tests__/mount.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { describe, expect, test, vi } from 'vitest'
22

3-
import { act, render, screen } from '..'
3+
import { act, render, screen } from '@testing-library/svelte'
44
import Mounter from './fixtures/Mounter.svelte'
5+
import { IS_HAPPYDOM, IS_SVELTE_5 } from './utils.js'
56

67
const onMounted = vi.fn()
78
const onDestroyed = vi.fn()
89
const renderSubject = () => render(Mounter, { onMounted, onDestroyed })
910

10-
describe('mount and destroy', () => {
11+
describe.skipIf(IS_SVELTE_5 && IS_HAPPYDOM)('mount and destroy', () => {
1112
test('component is mounted', async () => {
1213
renderSubject()
1314

0 commit comments

Comments
 (0)