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

+15
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

+4
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

+1-1
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

+1-1
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

+1-1
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

+3-2
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

+3-2
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

+1-1
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

+1-1
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

+3-2
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

src/__tests__/multi-base.test.js

+1-1
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('multi-base', () => {

src/__tests__/render.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { VERSION as SVELTE_VERSION } from 'svelte/compiler'
22
import { beforeEach, describe, expect, test } from 'vitest'
33

4-
import { act, render as stlRender } from '..'
4+
import { act, render as stlRender } from '@testing-library/svelte'
55
import Comp from './fixtures/Comp.svelte'
66
import CompDefault from './fixtures/Comp2.svelte'
77

@@ -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/__tests__/rerender.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/**
22
* @jest-environment jsdom
33
*/
4-
import { describe, expect, test, vi } from 'vitest'
5-
import { writable } from 'svelte/store'
4+
import { expect, test, vi } from 'vitest'
5+
6+
import { render, waitFor } from '@testing-library/svelte'
67

7-
import { act, render, waitFor } from '..'
88
import Comp from './fixtures/Rerender.svelte'
99

1010
test('mounts new component successfully', async () => {

src/__tests__/transition.test.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { userEvent } from '@testing-library/user-event'
2-
import { VERSION as SVELTE_VERSION } from 'svelte/compiler'
32
import { beforeEach, describe, expect, test, vi } from 'vitest'
43

5-
import { render, screen, waitFor } from '..'
4+
import { IS_JSDOM, IS_SVELTE_5 } from './utils.js'
5+
6+
import { render, screen, waitFor } from '@testing-library/svelte'
67
import Transitioner from './fixtures/Transitioner.svelte'
78

8-
describe.runIf(SVELTE_VERSION < '5')('transitions', () => {
9+
describe.runIf(!IS_SVELTE_5)('transitions', () => {
910
beforeEach(() => {
10-
if (window.navigator.userAgent.includes('jsdom')) {
11-
const raf = (fn) => setTimeout(() => fn(new Date()), 16)
12-
vi.stubGlobal('requestAnimationFrame', raf)
13-
}
11+
if (!IS_JSDOM) return
12+
13+
const raf = (fn) => setTimeout(() => fn(new Date()), 16)
14+
vi.stubGlobal('requestAnimationFrame', raf)
1415
})
1516

1617
test('on:introend', async () => {

src/__tests__/utils.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { VERSION as SVELTE_VERSION } from 'svelte/compiler'
2+
3+
export const IS_JSDOM = window.navigator.userAgent.includes('jsdom')
4+
5+
export const IS_HAPPYDOM = !IS_JSDOM // right now it's happy or js
6+
7+
export const IS_SVELTE_5 = SVELTE_VERSION >= '5'

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ if (typeof afterEach === 'function' && !process.env.STL_SKIP_AUTO_CLEANUP) {
1313
}
1414

1515
export * from './pure.js'
16+
export * from '@testing-library/dom'

0 commit comments

Comments
 (0)