@@ -4,83 +4,87 @@ title: Setup
4
4
sidebar_label : Setup
5
5
---
6
6
7
- We recommend using [ Vitest] ( https://vitest.dev/ ) but you're free to use the
8
- library with any testing framework and runner you're comfortable with .
7
+ We recommend using [ Vitest] [ ] , but you're free to use the library with any test
8
+ runner that's ESM compatible .
9
9
10
- ## Vitest
11
-
12
- 1 . Install Vitest and jsdom
10
+ [ vitest ] : https://vitest.dev/
13
11
14
- We're using ` jsdom ` here as the test environment, but you can use any other
15
- options e.g ` happy-dom ` .
12
+ ## Vitest
16
13
17
- ``` bash npm2yarn
18
- npm install --save-dev vitest jsdom
19
- ```
14
+ 1 . Add development dependencies
20
15
21
- Optionally install ` @vitest/ui ` , which opens a UI within a browser window to
22
- follow the progress and interact with your tests.
16
+ - [ ` @testing-library/svelte ` ] [ @testing-library/svelte ]
17
+ - [ ` @testing-library/jest-dom ` ] [ @testing-library/jest-dom ] (Optional, but
18
+ highly recommended)
19
+ - [ ` @sveltejs/vite-plugin-svelte ` ] [ @sveltejs/vite-plugin-svelte ]
20
+ - [ ` vitest ` ] [ vitest ]
21
+ - [ ` jsdom ` ] [ jsdom ] , [ ` happy-dom ` ] [ happy-dom ] , or other [ Vitest DOM
22
+ environment] [ vitest dom ]
23
23
24
- ``` bash npm2yarn
25
- npm install --save-dev @vitest/ui
26
- ```
24
+ ``` bash npm2yarn
25
+ npm install --save-dev \
26
+ @testing-library/svelte \
27
+ @testing-library/jest-dom \
28
+ @sveltejs/vite-plugin-svelte \
29
+ vitest \
30
+ jsdom
31
+ ```
27
32
28
- 1 . Add the test scipts to your ` package.json ` to run the tests with Vitest
33
+ Optionally install [ ` @vitest/ui ` ] [ @vitest/ui ] , which opens a UI within a
34
+ browser window to follow the progress and interact with your tests.
29
35
30
- ``` json
31
- {
32
- "scripts" : {
33
- "test" : " vitest run" ,
34
- "test:ui" : " vitest --ui" ,
35
- "test:watch" : " vitest"
36
- }
37
- }
36
+ ``` bash npm2yarn
37
+ npm install --save-dev @vitest/ui
38
38
```
39
39
40
- 2 . To compile the Svelte components before using them in Vitest, you need to
41
- install
42
- [ @sveltejs/vite-plugin-svelte ] ( https://github.com/sveltejs/vite-plugin-svelte )
43
- and Vite
40
+ 2 . Add a ` vitest-setup.js ` file to optionally set up automatic post-test cleanup
41
+ and [ ` @testing-library/jest-dom ` ] [ @testing-library/jest-dom ] expect matchers.
44
42
45
- ``` bash npm2yarn
46
- npm install --save-dev @sveltejs/vite-plugin-svelte vite
43
+ ``` ts title="vitest-setup.js"
44
+ import ' @testing-library/svelte/vitest'
45
+ import ' @testing-library/jest-dom/vitest'
47
46
```
48
47
49
- 3 . Add a ` vitest.config.ts ` configuration file to the root of your project. Add
50
- ` globals: true ` so ` cleanup() ` runs after each test.
48
+ 3 . Add ` vitest.config.js ` , or update your existing ` vite.config.js ` , to process
49
+ Svelte files, resolve browser exports during tests, use the [ jsdom] [ ] (or
50
+ [ happy-dom] [ ] ) environment, and run your setup file.
51
51
52
- ``` js
52
+ ``` js title="vitest.config.js"
53
53
import {defineConfig } from ' vitest/config'
54
54
import {svelte } from ' @sveltejs/vite-plugin-svelte'
55
55
56
- export default defineConfig ({
57
- plugins: [svelte ({hot: ! process .env .VITEST })],
56
+ export default defineConfig (({mode}) => ({
57
+ plugins: [svelte ()],
58
+ resolve: {
59
+ conditions: mode === ' test' ? [' browser' ] : [],
60
+ },
58
61
test: {
59
- globals: true ,
60
62
environment: ' jsdom' ,
63
+ setupFiles: [' ./vitest-setup.js' ],
61
64
},
62
- })
65
+ }))
63
66
```
64
67
65
- 4 . Optionally install [ vitest-dom] ( https://github.com/chaance/vitest-dom ) to add
66
- handy assertions to Vitest
67
-
68
- 4.1 Install ` vitest-dom `
68
+ :::note
69
69
70
- ``` bash npm2yarn
71
- npm install --save-dev vitest-dom
72
- ```
70
+ Prepending the ` browser ` resolve condition to Vite's default conditions may
71
+ cause issues if you have a complex Vite configuration or dependencies that
72
+ cannot be loaded into Node.js
73
73
74
- 4.2 import ` vitest-dom ` at within the vitest setup file (usually
75
- ` vitest-setup.(js|ts) ` )
74
+ See [ testing-library/svelte-testing-library #222 ] [ ] for more information and
75
+ alternative configuration options to ensure Svelte's browser bundle is used.
76
+ :::
76
77
77
- ``` js
78
- import * as matchers from ' vitest-dom/matchers'
79
- import {expect } from ' vitest'
80
- expect .extend (matchers)
78
+ 4 . Add test scripts to your ` package.json ` to run the tests with Vitest
81
79
82
- // or:
83
- import ' vitest-dom/extend-expect'
80
+ ``` json title="package.json"
81
+ {
82
+ "scripts" : {
83
+ "test" : " vitest run" ,
84
+ "test:ui" : " vitest --ui" ,
85
+ "test:watch" : " vitest"
86
+ }
87
+ }
84
88
```
85
89
86
90
5 . Create your component and a test file (checkout the rest of the docs to see
@@ -90,108 +94,95 @@ npm install --save-dev @vitest/ui
90
94
npm test
91
95
```
92
96
93
- ## Jest
94
-
95
- 1 . Install Jest & jest-environment-jsdom
96
-
97
- ``` bash npm2yarn
98
- npm install --save-dev jest jest-environment-jsdom
99
- ```
100
-
101
- 2 . Add the following to your ` package.json `
102
-
103
- ``` json
104
- {
105
- "scripts" : {
106
- "test" : " jest src" ,
107
- "test:watch" : " jest src --watch"
108
- }
109
- }
110
- ```
111
-
112
- 3 . You'll need to compile the Svelte components before using them in Jest, so
113
- we need to install
114
- [ svelte-jester] ( https://github.com/mihar-22/svelte-jester )
97
+ [ @testing-library/svelte ] :
98
+ https://github.com/testing-library/svelte-testing-library
99
+ [ @testing-library/jest-dom ] : https://github.com/testing-library/jest-dom
100
+ [ @sveltejs/vite-plugin-svelte ] : https://github.com/sveltejs/vite-plugin-svelte
101
+ [ jsdom ] : https://github.com/jsdom/jsdom
102
+ [ happy-dom ] : https://github.com/capricorn86/happy-dom
103
+ [ @vitest/ui ] : https://vitest.dev/guide/ui.html
104
+ [ vitest dom ] : https://vitest.dev/guide/environment.html
105
+ [ testing-library/svelte-testing-library #222 ] :
106
+ https://github.com/testing-library/svelte-testing-library/issues/222
115
107
116
- ``` bash npm2yarn
117
- npm install --save-dev svelte-jester
118
- ```
119
-
120
- 4 . Add the following Jest configuration to your ` package.json `
121
-
122
- ``` json
123
- {
124
- "jest" : {
125
- "transform" : {
126
- "^.+\\ .svelte$" : " svelte-jester"
127
- },
128
- "moduleFileExtensions" : [" js" , " svelte" ],
129
- "testEnvironment" : " jsdom"
130
- }
131
- }
132
- ```
133
-
134
- 5 . If you are using ES6 modules in your project you have to add Jest's babel
135
- transform setting (it is set by default, but since we are overriding the
136
- transform config, we have to add it explicitly)
108
+ ## Jest
137
109
138
- 5.1 Install ` babel-jest `
110
+ [ ` @testing-library/svelte ` ] [ @testing-library/svelte ] is ESM-only, which means
111
+ you must use Jest in [ ESM mode] [ jest esm mode ] .
139
112
140
- ``` bash npm2yarn
141
- npm install --save-dev babel-jest
142
- ```
113
+ 1 . Add development dependencies
143
114
144
- 5.2. Add a basic ` .babelrc ` configuration
115
+ - [ ` @testing-library/svelte ` ] [ @testing-library/svelte ]
116
+ - [ ` @testing-library/jest-dom ` ] [ @testing-library/jest-dom ] (Optional, but
117
+ highly recommended)
118
+ - [ ` svelte-jester ` ] [ svelte-jester ]
119
+ - [ ` jest ` ] [ vitest ]
120
+ - [ ` jest-environment-jsdom ` ] [ jest-environment-jsdom ]
145
121
146
- ``` json
147
- {
148
- "presets" : [[" @babel/preset-env" , {"targets" : {"node" : " current" }}]]
149
- }
150
- ```
122
+ ``` bash npm2yarn
123
+ npm install --save-dev \
124
+ @testing-library/svelte \
125
+ @testing-library/jest-dom \
126
+ svelte-jester \
127
+ jest \
128
+ jest-environment-jsdom
129
+ ```
151
130
152
- 5.3. Update the Jest transform configuration
131
+ 2 . Add a ` jest-setup.js ` file to configure
132
+ [ ` @testing-library/jest-dom ` ] [ @testing-library/jest-dom ] , if using.
153
133
154
- ``` json
155
- "transform" : {
156
- "^.+\\ .js$" : " babel-jest" ,
157
- "^.+\\ .svelte$" : " svelte-jester"
158
- },
159
- ```
134
+ ``` ts title="jest-setup.js"
135
+ import ' @testing-library/jest-dom'
136
+ ```
160
137
161
- 6 . This is optional but it is recommended, you can install
162
- [ jest-dom] ( https://github.com/testing-library/jest-dom ) to add handy
163
- assertions to Jest
138
+ 3 . Configure Jest to use jsdom, transform Svelte files, and use your setup file
164
139
165
- 6.1 Install ` jest-dom `
140
+ ``` js title="jest.config.js"
141
+ module .exports = {
142
+ " transform" : {
143
+ " ^.+\\ .svelte$" : " svelte-jester"
144
+ },
145
+ " moduleFileExtensions" : [" js" , " svelte" ],
146
+ " extensionsToTreatAsEsm" : [" svelte" ]
147
+ " testEnvironment" : " jsdom" ,
148
+ " setupFilesAfterEnv" : [" <rootDir>/jest-setup.js" ]
149
+ }
150
+ ```
166
151
167
- ``` bash npm2yarn
168
- npm install --save-dev @testing-library/jest-dom
169
- ```
152
+ 4 . Add the following to your ` package.json `
170
153
171
- 6.2 Add the following to your Jest configuration in ` package.json `
154
+ ``` json title="package.json"
155
+ {
156
+ "scripts" : {
157
+ "test" : " npx --node-options=\" --experimental-vm-modules\" jest src" ,
158
+ "test:watch" : " npx --node-options=\" --experimental-vm-modules\" jest src --watch"
159
+ }
160
+ }
161
+ ```
172
162
173
- ``` json
174
- {
175
- "setupFilesAfterEnv" : [" @testing-library/jest-dom/extend-expect" ]
176
- }
177
- ```
163
+ 5 . Create your component + test file (checkout the rest of the docs to see how)
164
+ and run it
178
165
179
- 7 . Create your component + test file (checkout the rest of the docs to see how)
180
- and run it
166
+ ``` bash npm2yarn
167
+ npm test
168
+ ```
181
169
182
- ``` bash npm2yarn
183
- npm test
184
- ```
170
+ [ jest esm mode ] : https://jestjs.io/docs/ecmascript-modules
171
+ [ svelte-jester ] : https://github.com/svelteness/svelte-jester
172
+ [ jest-environment-jsdom] :
173
+ https://jestjs.io/docs/configuration#testenvironment-string
185
174
186
- ### TypeScript
175
+ ### TypeScript and preprocessors
187
176
188
177
To use TypeScript with Jest, you'll need to install and configure
189
178
` svelte-preprocess ` and ` ts-jest ` . For full instructions, see the
190
- [ ` svelte-jester ` ] ( https://github.com/mihar-22/ svelte-jester#typescript ) docs .
179
+ [ ` svelte-jester ` docs ] [ svelte-jester typescript ] .
191
180
192
- ### Preprocessors
181
+ If you'd like include any other [ Svelte preprocessors] [ svelte-preprocess ] , see
182
+ the [ ` svelte-jester ` docs] [ svelte-jester preprocess ] .
193
183
194
- If you'd like to also include any
195
- [ Svelte preprocessors] ( https://github.com/sveltejs/svelte-preprocess ) then
196
- simply follow the instructions over at
197
- [ svelte-jester] ( https://github.com/mihar-22/svelte-jester#babel ) .
184
+ [ svelte-preprocess ] : https://github.com/sveltejs/svelte-preprocess
185
+ [ svelte-jester typescript] :
186
+ https://github.com/svelteness/svelte-jester#typescript
187
+ [ svelte-jester preprocess] :
188
+ https://github.com/svelteness/svelte-jester#preprocess
0 commit comments