@testing-library/svelte 5.1.0 → 5.2.0-next.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/README.md +65 -74
  2. package/package.json +27 -22
  3. package/src/core/index.js +27 -0
  4. package/src/core/legacy.js +46 -0
  5. package/src/core/modern.svelte.js +50 -0
  6. package/src/core/validate-options.js +39 -0
  7. package/src/index.js +5 -4
  8. package/src/pure.js +146 -129
  9. package/types/core/index.d.ts +8 -0
  10. package/types/core/index.d.ts.map +1 -0
  11. package/types/core/legacy.d.ts +19 -0
  12. package/types/core/legacy.d.ts.map +1 -0
  13. package/types/core/modern.svelte.d.ts +15 -0
  14. package/types/core/modern.svelte.d.ts.map +1 -0
  15. package/types/core/validate-options.d.ts +5 -0
  16. package/types/core/validate-options.d.ts.map +1 -0
  17. package/types/index.d.ts +5 -82
  18. package/types/index.d.ts.map +1 -0
  19. package/types/pure.d.ts +95 -0
  20. package/types/pure.d.ts.map +1 -0
  21. package/types/vite.d.ts +5 -12
  22. package/types/vite.d.ts.map +1 -0
  23. package/types/vitest.d.ts +2 -0
  24. package/types/vitest.d.ts.map +1 -0
  25. package/src/__tests__/__snapshots__/auto-cleanup-skip.test.js.snap +0 -3
  26. package/src/__tests__/_vitest-setup.js +0 -1
  27. package/src/__tests__/act.test.js +0 -33
  28. package/src/__tests__/auto-cleanup-skip.test.js +0 -23
  29. package/src/__tests__/auto-cleanup.test.js +0 -31
  30. package/src/__tests__/cleanup.test.js +0 -35
  31. package/src/__tests__/context.test.js +0 -14
  32. package/src/__tests__/debug.test.js +0 -18
  33. package/src/__tests__/events.test.js +0 -32
  34. package/src/__tests__/fixtures/Comp.svelte +0 -17
  35. package/src/__tests__/fixtures/Context.svelte +0 -7
  36. package/src/__tests__/fixtures/Mounter.svelte +0 -19
  37. package/src/__tests__/fixtures/Simple.svelte +0 -7
  38. package/src/__tests__/fixtures/Transitioner.svelte +0 -18
  39. package/src/__tests__/mount.test.js +0 -33
  40. package/src/__tests__/multi-base.test.js +0 -42
  41. package/src/__tests__/render.test.js +0 -85
  42. package/src/__tests__/rerender.test.js +0 -50
  43. package/src/__tests__/transition.test.js +0 -31
  44. package/src/__tests__/utils.js +0 -7
  45. package/src/svelte5-index.js +0 -23
  46. package/src/svelte5.js +0 -30
  47. package/types/types.test-d.ts +0 -65
package/README.md CHANGED
@@ -12,17 +12,18 @@
12
12
 
13
13
  <p>Simple and complete Svelte testing utilities that encourage good testing practices.</p>
14
14
 
15
- [**Read The Docs**](https://testing-library.com/docs/svelte-testing-library/intro) |
16
- [Edit the docs](https://github.com/testing-library/testing-library-docs)
15
+ [**Read The Docs**][stl-docs] | [Edit the docs][stl-docs-repo]
17
16
 
18
17
  <!-- prettier-ignore-start -->
19
18
  [![Build Status][build-badge]][build]
20
19
  [![Code Coverage][coverage-badge]][coverage]
21
- [![version][version-badge]][package] [![downloads][downloads-badge]][npmtrends]
20
+ [![version][version-badge]][package]
21
+ [![downloads][downloads-badge]][downloads]
22
22
  [![MIT License][license-badge]][license]
23
23
 
24
- [![All Contributors](https://img.shields.io/badge/all_contributors-8-orange.svg?style=flat-square)](#contributors-)
25
- [![PRs Welcome][prs-badge]][prs] [![Code of Conduct][coc-badge]][coc]
24
+ [![All Contributors][contributors-badge]][contributors]
25
+ [![PRs Welcome][prs-badge]][prs]
26
+ [![Code of Conduct][coc-badge]][coc]
26
27
  [![Discord][discord-badge]][discord]
27
28
 
28
29
  [![Watch on GitHub][github-watch-badge]][github-watch]
@@ -33,6 +34,33 @@
33
34
 
34
35
  <hr />
35
36
 
37
+ [stl-docs]: https://testing-library.com/docs/svelte-testing-library/intro
38
+ [stl-docs-repo]: https://github.com/testing-library/testing-library-docs
39
+ [build-badge]: https://img.shields.io/github/actions/workflow/status/testing-library/svelte-testing-library/release.yml?style=flat-square
40
+ [build]: https://github.com/testing-library/svelte-testing-library/actions
41
+ [coverage-badge]: https://img.shields.io/codecov/c/github/testing-library/svelte-testing-library.svg?style=flat-square
42
+ [coverage]: https://codecov.io/github/testing-library/svelte-testing-library
43
+ [version-badge]: https://img.shields.io/npm/v/@testing-library/svelte.svg?style=flat-square
44
+ [package]: https://www.npmjs.com/package/@testing-library/svelte
45
+ [downloads-badge]: https://img.shields.io/npm/dm/@testing-library/svelte.svg?style=flat-square
46
+ [downloads]: http://www.npmtrends.com/@testing-library/svelte
47
+ [license-badge]: https://img.shields.io/github/license/testing-library/svelte-testing-library?color=b&style=flat-square
48
+ [license]: https://github.com/testing-library/svelte-testing-library/blob/main/LICENSE
49
+ [contributors-badge]: https://img.shields.io/badge/all_contributors-8-orange.svg?style=flat-square
50
+ [contributors]: #contributors
51
+ [prs-badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square
52
+ [prs]: http://makeapullrequest.com
53
+ [coc-badge]: https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square
54
+ [coc]: https://github.com/testing-library/svelte-testing-library/blob/main/CODE_OF_CONDUCT.md
55
+ [discord-badge]: https://img.shields.io/discord/723559267868737556.svg?color=7389D8&labelColor=6A7EC2&logo=discord&logoColor=ffffff&style=flat-square
56
+ [discord]: https://discord.gg/testing-library
57
+ [github-watch-badge]: https://img.shields.io/github/watchers/testing-library/svelte-testing-library.svg?style=social
58
+ [github-watch]: https://github.com/testing-library/svelte-testing-library/watchers
59
+ [github-star-badge]: https://img.shields.io/github/stars/testing-library/svelte-testing-library.svg?style=social
60
+ [github-star]: https://github.com/testing-library/svelte-testing-library/stargazers
61
+ [twitter]: https://twitter.com/intent/tweet?text=Check%20out%20svelte-testing-library%20by%20%40@TestingLib%20https%3A%2F%2Fgithub.com%2Ftesting-library%2Fsvelte-testing-library%20%F0%9F%91%8D
62
+ [twitter-badge]: https://img.shields.io/twitter/url/https/github.com/testing-library/svelte-testing-library.svg?style=social
63
+
36
64
  ## Table of Contents
37
65
 
38
66
  <!-- START doctoc generated TOC please keep comment here to allow auto update -->
@@ -41,48 +69,57 @@
41
69
  - [The Problem](#the-problem)
42
70
  - [This Solution](#this-solution)
43
71
  - [Installation](#installation)
72
+ - [Setup](#setup)
44
73
  - [Docs](#docs)
45
74
  - [Issues](#issues)
46
75
  - [🐛 Bugs](#-bugs)
47
76
  - [💡 Feature Requests](#-feature-requests)
48
77
  - [❓ Questions](#-questions)
49
78
  - [Contributors](#contributors)
50
- - [LICENSE](#license)
51
79
 
52
80
  <!-- END doctoc generated TOC please keep comment here to allow auto update -->
53
81
 
54
82
  ## The Problem
55
83
 
56
- You want to write tests for your Svelte components so that they avoid including implementation
57
- details, and are maintainable in the long run.
84
+ You want to write maintainable tests for your [Svelte][svelte] components.
85
+
86
+ [svelte]: https://svelte.dev/
58
87
 
59
88
  ## This Solution
60
89
 
61
- The `svelte-testing-library` is a very lightweight solution for testing Svelte
62
- components. It provides light utility functions on top of `svelte` and
63
- `dom-testing-library`, in a way that encourages better testing practices. Its
64
- primary guiding principle is:
90
+ `@testing-library/svelte` is a lightweight library for testing Svelte
91
+ components. It provides functions on top of `svelte` and
92
+ `@testing-library/dom` so you can mount Svelte components and query their
93
+ rendered output in the DOM. Its primary guiding principle is:
65
94
 
66
95
  > [The more your tests resemble the way your software is used, the more
67
96
  > confidence they can give you.][guiding-principle]
68
97
 
98
+ [guiding-principle]: https://twitter.com/kentcdodds/status/977018512689455106
99
+
69
100
  ## Installation
70
101
 
71
102
  This module is distributed via [npm][npm] which is bundled with [node][node] and
72
103
  should be installed as one of your project's `devDependencies`:
73
104
 
74
- ```
105
+ ```shell
75
106
  npm install --save-dev @testing-library/svelte
76
107
  ```
77
108
 
78
- This library has `peerDependencies` listings for `svelte >= 3`.
109
+ This library supports `svelte` versions `3`, `4`, and `5`.
110
+
111
+ You may also be interested in installing `@testing-library/jest-dom` so you can
112
+ use [the custom jest matchers][jest-dom].
79
113
 
80
- You may also be interested in installing `@testing-library/jest-dom` so you can use
81
- [the custom jest matchers](https://github.com/testing-library/jest-dom).
114
+ [npm]: https://www.npmjs.com/
115
+ [node]: https://nodejs.org
116
+ [jest-dom]: https://github.com/testing-library/jest-dom
82
117
 
83
118
  ## Setup
84
119
 
85
- We recommend using `@testing-library/svelte` with [Vitest][] as your test runner. To get started, add the `svelteTesting` plugin to your Vite or Vitest config.
120
+ We recommend using `@testing-library/svelte` with [Vitest][] as your test
121
+ runner. To get started, add the `svelteTesting` plugin to your Vite or Vitest
122
+ config.
86
123
 
87
124
  ```diff
88
125
  // vite.config.js
@@ -97,42 +134,31 @@ We recommend using `@testing-library/svelte` with [Vitest][] as your test runner
97
134
  });
98
135
  ```
99
136
 
100
- See the [setup docs][] for more detailed setup instructions, including for other test runners like Jest.
137
+ See the [setup docs][] for more detailed setup instructions, including for other
138
+ test runners like Jest.
101
139
 
102
140
  [vitest]: https://vitest.dev/
103
141
  [setup docs]: https://testing-library.com/docs/svelte-testing-library/setup
104
142
 
105
- ### Svelte 5 support
106
-
107
- If you are riding the bleeding edge of Svelte 5, you'll need to either
108
- import from `@testing-library/svelte/svelte5` instead of `@testing-library/svelte`, or add an alias to your `vite.config.js`:
109
-
110
- ```js
111
- export default defineConfig({
112
- plugins: [svelte(), svelteTesting()],
113
- test: {
114
- alias: {
115
- '@testing-library/svelte': '@testing-library/svelte/svelte5',
116
- },
117
- },
118
- })
119
- ```
120
-
121
143
  ## Docs
122
144
 
123
- See the [**docs**](https://testing-library.com/docs/svelte-testing-library/intro) over at the Testing Library website.
145
+ See the [**docs**][stl-docs] over at the Testing Library website.
124
146
 
125
147
  ## Issues
126
148
 
127
149
  _Looking to contribute? Look for the [Good First Issue][good-first-issue]
128
150
  label._
129
151
 
152
+ [good-first-issue]: https://github.com/testing-library/svelte-testing-library/issues?utf8=✓&q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc+label%3A"good+first+issue"+
153
+
130
154
  ### 🐛 Bugs
131
155
 
132
156
  Please file an issue for bugs, missing documentation, or unexpected behavior.
133
157
 
134
158
  [**See Bugs**][bugs]
135
159
 
160
+ [bugs]: https://github.com/testing-library/svelte-testing-library/issues?q=is%3Aissue+is%3Aopen+label%3Abug+sort%3Acreated-desc
161
+
136
162
  ### 💡 Feature Requests
137
163
 
138
164
  Please file an issue to suggest new features. Vote on feature requests by adding
@@ -140,6 +166,8 @@ a 👍. This helps maintainers prioritize what to work on.
140
166
 
141
167
  [**See Feature Requests**][requests]
142
168
 
169
+ [requests]: https://github.com/testing-library/svelte-testing-library/issues?q=is%3Aissue+sort%3Areactions-%2B1-desc+label%3Aenhancement+is%3Aopen
170
+
143
171
  ### ❓ Questions
144
172
 
145
173
  For questions related to using the library, please visit a support community
@@ -148,6 +176,8 @@ instead of filing an issue on GitHub.
148
176
  - [Discord][discord]
149
177
  - [Stack Overflow][stackoverflow]
150
178
 
179
+ [stackoverflow]: https://stackoverflow.com/questions/tagged/svelte-testing-library
180
+
151
181
  ## Contributors
152
182
 
153
183
  Thanks goes to these people ([emoji key][emojis]):
@@ -189,44 +219,5 @@ Thanks goes to these people ([emoji key][emojis]):
189
219
  This project follows the [all-contributors][all-contributors] specification.
190
220
  Contributions of any kind welcome!
191
221
 
192
- ## LICENSE
193
-
194
- [MIT](LICENSE)
195
-
196
- <!-- prettier-ignore-start -->
197
-
198
- [npm]: https://www.npmjs.com/
199
- [node]: https://nodejs.org
200
- [build-badge]: https://img.shields.io/github/actions/workflow/status/testing-library/svelte-testing-library/release.yml?style=flat-square
201
- [build]: https://github.com/testing-library/svelte-testing-library/actions
202
- [coverage-badge]: https://img.shields.io/codecov/c/github/testing-library/svelte-testing-library.svg?style=flat-square
203
- [coverage]: https://codecov.io/github/testing-library/svelte-testing-library
204
- [version-badge]: https://img.shields.io/npm/v/@testing-library/svelte.svg?style=flat-square
205
- [package]: https://www.npmjs.com/package/@testing-library/svelte
206
- [downloads-badge]: https://img.shields.io/npm/dm/@testing-library/svelte.svg?style=flat-square
207
- [npmtrends]: http://www.npmtrends.com/@testing-library/svelte
208
- [discord-badge]: https://img.shields.io/discord/723559267868737556.svg?color=7389D8&labelColor=6A7EC2&logo=discord&logoColor=ffffff&style=flat-square
209
- [discord]: https://discord.gg/testing-library
210
- [license-badge]: https://img.shields.io/github/license/testing-library/svelte-testing-library?color=b&style=flat-square
211
- [license]: https://github.com/testing-library/svelte-testing-library/blob/main/LICENSE
212
- [prs-badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square
213
- [prs]: http://makeapullrequest.com
214
- [donate-badge]: https://img.shields.io/badge/$-support-green.svg?style=flat-square
215
- [coc-badge]: https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square
216
- [coc]: https://github.com/testing-library/svelte-testing-library/blob/main/CODE_OF_CONDUCT.md
217
- [github-watch-badge]: https://img.shields.io/github/watchers/testing-library/svelte-testing-library.svg?style=social
218
- [github-watch]: https://github.com/testing-library/svelte-testing-library/watchers
219
- [github-star-badge]: https://img.shields.io/github/stars/testing-library/svelte-testing-library.svg?style=social
220
- [github-star]: https://github.com/testing-library/svelte-testing-library/stargazers
221
- [twitter]: https://twitter.com/intent/tweet?text=Check%20out%20svelte-testing-library%20by%20%40@TestingLib%20https%3A%2F%2Fgithub.com%2Ftesting-library%2Fsvelte-testing-library%20%F0%9F%91%8D
222
- [twitter-badge]: https://img.shields.io/twitter/url/https/github.com/testing-library/svelte-testing-library.svg?style=social
223
222
  [emojis]: https://github.com/all-contributors/all-contributors#emoji-key
224
223
  [all-contributors]: https://github.com/all-contributors/all-contributors
225
- [set-immediate]: https://developer.mozilla.org/en-US/docs/Web/API/Window/setImmediate
226
- [guiding-principle]: https://twitter.com/kentcdodds/status/977018512689455106
227
- [bugs]: https://github.com/testing-library/svelte-testing-library/issues?q=is%3Aissue+is%3Aopen+label%3Abug+sort%3Acreated-desc
228
- [requests]: https://github.com/testing-library/svelte-testing-library/issues?q=is%3Aissue+sort%3Areactions-%2B1-desc+label%3Aenhancement+is%3Aopen
229
- [good-first-issue]: https://github.com/testing-library/svelte-testing-library/issues?utf8=✓&q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc+label%3A"good+first+issue"+
230
- [stackoverflow]: https://stackoverflow.com/questions/tagged/svelte-testing-library
231
-
232
- <!-- prettier-ignore-end -->
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@testing-library/svelte",
3
- "version": "5.1.0",
3
+ "version": "5.2.0-next.2",
4
4
  "description": "Simple and complete Svelte testing utilities that encourage good testing practices.",
5
5
  "main": "src/index.js",
6
6
  "exports": {
@@ -10,9 +10,10 @@
10
10
  },
11
11
  "./svelte5": {
12
12
  "types": "./types/index.d.ts",
13
- "default": "./src/svelte5-index.js"
13
+ "default": "./src/index.js"
14
14
  },
15
15
  "./vitest": {
16
+ "types": "./types/vitest.d.ts",
16
17
  "default": "./src/vitest.js"
17
18
  },
18
19
  "./vite": {
@@ -26,7 +27,7 @@
26
27
  "homepage": "https://github.com/testing-library/svelte-testing-library#readme",
27
28
  "repository": {
28
29
  "type": "git",
29
- "url": "https://github.com/testing-library/svelte-testing-library"
30
+ "url": "git+https://github.com/testing-library/svelte-testing-library.git"
30
31
  },
31
32
  "bugs": {
32
33
  "url": "https://github.com/testing-library/svelte-testing-library/issues"
@@ -49,7 +50,6 @@
49
50
  "files": [
50
51
  "src",
51
52
  "types",
52
- "!*.test-d.ts",
53
53
  "!__tests__"
54
54
  ],
55
55
  "scripts": {
@@ -65,13 +65,15 @@
65
65
  "setup": "npm install && npm run validate",
66
66
  "test": "vitest run --coverage",
67
67
  "test:watch": "vitest",
68
- "test:update": "vitest run --update",
69
68
  "test:vitest:jsdom": "vitest run --coverage --environment jsdom",
70
69
  "test:vitest:happy-dom": "vitest run --coverage --environment happy-dom",
70
+ "test:jest": "npx --node-options=\"--experimental-vm-modules --no-warnings\" jest --coverage",
71
71
  "types": "svelte-check",
72
- "validate": "npm-run-all test:vitest:* types",
72
+ "validate": "npm-run-all test:vitest:* test:jest types build",
73
+ "build": "tsc -p tsconfig.build.json",
73
74
  "contributors:add": "all-contributors add",
74
- "contributors:generate": "all-contributors generate"
75
+ "contributors:generate": "all-contributors generate",
76
+ "preview-release": "./scripts/preview-release"
75
77
  },
76
78
  "peerDependencies": {
77
79
  "svelte": "^3 || ^4 || ^5",
@@ -87,38 +89,41 @@
87
89
  }
88
90
  },
89
91
  "dependencies": {
90
- "@testing-library/dom": "^9.3.1"
92
+ "@testing-library/dom": "^10.0.0"
91
93
  },
92
94
  "devDependencies": {
93
- "@sveltejs/vite-plugin-svelte": "^3.0.2",
95
+ "@jest/globals": "^29.7.0",
96
+ "@sveltejs/vite-plugin-svelte": "^3.1.1",
94
97
  "@testing-library/jest-dom": "^6.3.0",
95
98
  "@testing-library/user-event": "^14.5.2",
96
- "@typescript-eslint/eslint-plugin": "6.19.1",
97
- "@typescript-eslint/parser": "6.19.1",
98
- "@vitest/coverage-v8": "^0.33.0",
99
+ "@typescript-eslint/eslint-plugin": "7.8.0",
100
+ "@typescript-eslint/parser": "7.8.0",
101
+ "@vitest/coverage-v8": "^1.5.2",
99
102
  "all-contributors-cli": "^6.26.1",
100
103
  "doctoc": "^2.2.1",
101
- "eslint": "8.56.0",
104
+ "eslint": "8.57.0",
102
105
  "eslint-config-prettier": "9.1.0",
103
106
  "eslint-config-standard": "17.1.0",
104
107
  "eslint-plugin-import": "2.29.1",
105
108
  "eslint-plugin-json-files": "^4.1.0",
106
109
  "eslint-plugin-n": "16.6.2",
107
110
  "eslint-plugin-promise": "6.1.1",
108
- "eslint-plugin-simple-import-sort": "10.0.0",
109
- "eslint-plugin-svelte": "2.35.1",
110
- "eslint-plugin-vitest-globals": "1.4.0",
111
- "expect-type": "^0.17.3",
111
+ "eslint-plugin-simple-import-sort": "12.1.0",
112
+ "eslint-plugin-svelte": "2.38.0",
113
+ "eslint-plugin-vitest-globals": "1.5.0",
114
+ "expect-type": "^0.19.0",
112
115
  "happy-dom": "^14.7.1",
113
- "jsdom": "^22.1.0",
116
+ "jest": "^29.7.0",
117
+ "jest-environment-jsdom": "^29.7.0",
118
+ "jsdom": "^24.0.0",
114
119
  "npm-run-all": "^4.1.5",
115
- "prettier": "3.2.4",
116
- "prettier-plugin-svelte": "3.1.2",
120
+ "prettier": "3.2.5",
121
+ "prettier-plugin-svelte": "3.2.3",
117
122
  "svelte": "^3 || ^4 || ^5",
118
123
  "svelte-check": "^3.6.3",
119
- "svelte-jester": "^3.0.0",
124
+ "svelte-jester": "^5.0.0",
120
125
  "typescript": "^5.3.3",
121
126
  "vite": "^5.1.1",
122
- "vitest": "^0.33.0"
127
+ "vitest": "^1.5.2"
123
128
  }
124
129
  }
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Rendering core for svelte-testing-library.
3
+ *
4
+ * Defines how components are added to and removed from the DOM.
5
+ * Will switch to legacy, class-based mounting logic
6
+ * if it looks like we're in a Svelte <= 4 environment.
7
+ */
8
+ import * as LegacyCore from './legacy.js'
9
+ import * as ModernCore from './modern.svelte.js'
10
+ import {
11
+ createValidateOptions,
12
+ UnknownSvelteOptionsError,
13
+ } from './validate-options.js'
14
+
15
+ const { mount, unmount, updateProps, allowedOptions } =
16
+ ModernCore.IS_MODERN_SVELTE ? ModernCore : LegacyCore
17
+
18
+ /** Validate component options. */
19
+ const validateOptions = createValidateOptions(allowedOptions)
20
+
21
+ export {
22
+ mount,
23
+ UnknownSvelteOptionsError,
24
+ unmount,
25
+ updateProps,
26
+ validateOptions,
27
+ }
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Legacy rendering core for svelte-testing-library.
3
+ *
4
+ * Supports Svelte <= 4.
5
+ */
6
+
7
+ /** Allowed options for the component constructor. */
8
+ const allowedOptions = [
9
+ 'target',
10
+ 'accessors',
11
+ 'anchor',
12
+ 'props',
13
+ 'hydrate',
14
+ 'intro',
15
+ 'context',
16
+ ]
17
+
18
+ /**
19
+ * Mount the component into the DOM.
20
+ *
21
+ * The `onDestroy` callback is included for strict backwards compatibility
22
+ * with previous versions of this library. It's mostly unnecessary logic.
23
+ */
24
+ const mount = (Component, options, onDestroy) => {
25
+ const component = new Component(options)
26
+
27
+ if (typeof onDestroy === 'function') {
28
+ component.$$.on_destroy.push(() => {
29
+ onDestroy(component)
30
+ })
31
+ }
32
+
33
+ return component
34
+ }
35
+
36
+ /** Remove the component from the DOM. */
37
+ const unmount = (component) => {
38
+ component.$destroy()
39
+ }
40
+
41
+ /** Update the component's props. */
42
+ const updateProps = (component, nextProps) => {
43
+ component.$set(nextProps)
44
+ }
45
+
46
+ export { allowedOptions, mount, unmount, updateProps }
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Modern rendering core for svelte-testing-library.
3
+ *
4
+ * Supports Svelte >= 5.
5
+ */
6
+ import * as Svelte from 'svelte'
7
+
8
+ /** Props signals for each rendered component. */
9
+ const propsByComponent = new Map()
10
+
11
+ /** Whether we're using Svelte >= 5. */
12
+ const IS_MODERN_SVELTE = typeof Svelte.mount === 'function'
13
+
14
+ /** Allowed options to the `mount` call. */
15
+ const allowedOptions = [
16
+ 'target',
17
+ 'anchor',
18
+ 'props',
19
+ 'events',
20
+ 'context',
21
+ 'intro',
22
+ ]
23
+
24
+ /** Mount the component into the DOM. */
25
+ const mount = (Component, options) => {
26
+ const props = $state(options.props ?? {})
27
+ const component = Svelte.mount(Component, { ...options, props })
28
+
29
+ propsByComponent.set(component, props)
30
+
31
+ return component
32
+ }
33
+
34
+ /** Remove the component from the DOM. */
35
+ const unmount = (component) => {
36
+ propsByComponent.delete(component)
37
+ Svelte.unmount(component)
38
+ }
39
+
40
+ /**
41
+ * Update the component's props.
42
+ *
43
+ * Relies on the `$state` signal added in `mount`.
44
+ */
45
+ const updateProps = (component, nextProps) => {
46
+ const prevProps = propsByComponent.get(component)
47
+ Object.assign(prevProps, nextProps)
48
+ }
49
+
50
+ export { allowedOptions, IS_MODERN_SVELTE, mount, unmount, updateProps }
@@ -0,0 +1,39 @@
1
+ class UnknownSvelteOptionsError extends TypeError {
2
+ constructor(unknownOptions, allowedOptions) {
3
+ super(`Unknown options.
4
+
5
+ Unknown: [ ${unknownOptions.join(', ')} ]
6
+ Allowed: [ ${allowedOptions.join(', ')} ]
7
+
8
+ To pass both Svelte options and props to a component,
9
+ or to use props that share a name with a Svelte option,
10
+ you must place all your props under the \`props\` key:
11
+
12
+ render(Component, { props: { /** props here **/ } })
13
+ `)
14
+ this.name = 'UnknownSvelteOptionsError'
15
+ }
16
+ }
17
+
18
+ const createValidateOptions = (allowedOptions) => (options) => {
19
+ const isProps = !Object.keys(options).some((option) =>
20
+ allowedOptions.includes(option)
21
+ )
22
+
23
+ if (isProps) {
24
+ return { props: options }
25
+ }
26
+
27
+ // Check if any props and Svelte options were accidentally mixed.
28
+ const unknownOptions = Object.keys(options).filter(
29
+ (option) => !allowedOptions.includes(option)
30
+ )
31
+
32
+ if (unknownOptions.length > 0) {
33
+ throw new UnknownSvelteOptionsError(unknownOptions, allowedOptions)
34
+ }
35
+
36
+ return options
37
+ }
38
+
39
+ export { createValidateOptions, UnknownSvelteOptionsError }
package/src/index.js CHANGED
@@ -4,8 +4,7 @@ import { act, cleanup } from './pure.js'
4
4
  // If we're running in a test runner that supports afterEach
5
5
  // then we'll automatically run cleanup afterEach test
6
6
  // this ensures that tests run in isolation from each other
7
- // if you don't like this then either import the `pure` module
8
- // or set the STL_SKIP_AUTO_CLEANUP env variable to 'true'.
7
+ // if you don't like this then set the STL_SKIP_AUTO_CLEANUP env variable.
9
8
  if (typeof afterEach === 'function' && !process.env.STL_SKIP_AUTO_CLEANUP) {
10
9
  afterEach(async () => {
11
10
  await act()
@@ -17,5 +16,7 @@ if (typeof afterEach === 'function' && !process.env.STL_SKIP_AUTO_CLEANUP) {
17
16
  export * from '@testing-library/dom'
18
17
 
19
18
  // export svelte-specific functions and custom `fireEvent`
20
- // `fireEvent` must be a named export to take priority over wildcard export above
21
- export { act, cleanup, fireEvent, render } from './pure.js'
19
+ export { UnknownSvelteOptionsError } from './core/index.js'
20
+ export * from './pure.js'
21
+ // `fireEvent` must be named to take priority over wildcard from @testing-library/dom
22
+ export { fireEvent } from './pure.js'