vitest 0.0.70 → 0.0.74
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.
- package/README.gh.md +10 -276
- package/dist/cli.js +55 -1432
- package/dist/entry.js +548 -81
- package/dist/error-48107470.js +1429 -0
- package/dist/index.d.ts +118 -2
- package/dist/worker.js +14 -0
- package/global.d.ts +0 -1
- package/package.json +3 -3
package/README.gh.md
CHANGED
|
@@ -11,6 +11,14 @@ A blazing fast unit test framework powered by Vite.
|
|
|
11
11
|
<p align="center">
|
|
12
12
|
<a href="https://www.npmjs.com/package/vitest"><img src="https://img.shields.io/npm/v/vitest?color=a1b858&label="></a>
|
|
13
13
|
<p>
|
|
14
|
+
<h2 align="center">
|
|
15
|
+
<a href="https://preview.vitest.dev">Open the Docs</a>
|
|
16
|
+
</h2>
|
|
17
|
+
<h3 align="center">
|
|
18
|
+
<a href=https://discord.com/invite/2zYZNngd7y"><i>Get involved!</i></a>
|
|
19
|
+
</h3>
|
|
20
|
+
<br>
|
|
21
|
+
<br>
|
|
14
22
|
|
|
15
23
|
> 💖 **This project is currently in closed beta exclusively for Sponsors.**<br>
|
|
16
24
|
> Become a Sponsor of [@patak-dev](https://github.com/sponsors/patak-dev) or [@antfu](https://github.com/sponsors/antfu) to access the source code and issues tracker.
|
|
@@ -19,7 +27,8 @@ A blazing fast unit test framework powered by Vite.
|
|
|
19
27
|
|
|
20
28
|
> Vitest requires Vite v2.7 and Node v16
|
|
21
29
|
|
|
22
|
-
|
|
30
|
+
|
|
31
|
+
Switch to Vitest by following the [Getting Started Guide](https://preview.vitest.dev/guide) or learn [why we are building a new test runner](https://preview.vitest.dev/guide).
|
|
23
32
|
|
|
24
33
|
## Features
|
|
25
34
|
|
|
@@ -74,281 +83,6 @@ $ npx vitest
|
|
|
74
83
|
- [unplugin-vue-components](https://github.com/antfu/unplugin-vue-components)
|
|
75
84
|
- [vitesse-lite](https://github.com/antfu/vitesse-lite)
|
|
76
85
|
|
|
77
|
-
## Configuration
|
|
78
|
-
|
|
79
|
-
`vitest` will read your root `vite.config.ts` when it present to match with the plugins and setup as your Vite app. If you want to it to have a different configuration for testing, you could either:
|
|
80
|
-
|
|
81
|
-
- Create `vitest.config.ts`, which will have the higher priority
|
|
82
|
-
- Pass `--config` option to CLI, e.g. `vitest --config ./path/to/vitest.config.ts`
|
|
83
|
-
- Use `process.env.VITEST` to conditionally apply different configuration in `vite.config.ts`
|
|
84
|
-
|
|
85
|
-
To configure `vitest` itself, add `test` property in your Vite config
|
|
86
|
-
|
|
87
|
-
```ts
|
|
88
|
-
// vite.config.ts
|
|
89
|
-
import { defineConfig } from 'vite'
|
|
90
|
-
|
|
91
|
-
export default defineConfig({
|
|
92
|
-
test: {
|
|
93
|
-
// ...
|
|
94
|
-
}
|
|
95
|
-
})
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
## Global APIs
|
|
99
|
-
|
|
100
|
-
By default, `vitest` does not provide global APIs for explicitness. If you prefer to use the APIs globally like Jest, you can pass the `--global` option to CLI or add `global: true` in the config.
|
|
101
|
-
|
|
102
|
-
```ts
|
|
103
|
-
// vite.config.ts
|
|
104
|
-
import { defineConfig } from 'vite'
|
|
105
|
-
|
|
106
|
-
export default defineConfig({
|
|
107
|
-
test: {
|
|
108
|
-
global: true
|
|
109
|
-
}
|
|
110
|
-
})
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
To get TypeScript working with the global APIs, add `vitest/global` to the `types` filed in your `tsconfig.json`
|
|
114
|
-
|
|
115
|
-
```jsonc
|
|
116
|
-
// tsconfig.json
|
|
117
|
-
{
|
|
118
|
-
"compilerOptions": {
|
|
119
|
-
"types": [
|
|
120
|
-
"vitest/global"
|
|
121
|
-
]
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
If you are already using [`unplugin-auto-import`](https://github.com/antfu/unplugin-vue-components) in your project, you can also use it directly for auto importing those APIs.
|
|
127
|
-
|
|
128
|
-
```ts
|
|
129
|
-
// vite.config.ts
|
|
130
|
-
import { defineConfig } from 'vite'
|
|
131
|
-
import AutoImport from 'unplugin-auto-import/vite'
|
|
132
|
-
|
|
133
|
-
export default defineConfig({
|
|
134
|
-
plugins: [
|
|
135
|
-
AutoImport({
|
|
136
|
-
imports: ['vitest'],
|
|
137
|
-
dts: true // generate TypeScript declaration
|
|
138
|
-
})
|
|
139
|
-
]
|
|
140
|
-
})
|
|
141
|
-
```
|
|
142
|
-
|
|
143
|
-
## Browser Mocking
|
|
144
|
-
|
|
145
|
-
Vitest supports both [happy-dom](https://github.com/capricorn86/happy-dom) or [jsdom](https://github.com/jsdom/jsdom) for mocking DOM and browser APIs. They don't come with Vitest, you might need to install them:
|
|
146
|
-
|
|
147
|
-
```bash
|
|
148
|
-
$ npm i -D happy-dom
|
|
149
|
-
# or
|
|
150
|
-
$ npm i -D jsdom
|
|
151
|
-
```
|
|
152
|
-
|
|
153
|
-
After that, change the `environment` option in your config file:
|
|
154
|
-
|
|
155
|
-
```ts
|
|
156
|
-
// vite.config.ts
|
|
157
|
-
import { defineConfig } from 'vite'
|
|
158
|
-
|
|
159
|
-
export default defineConfig({
|
|
160
|
-
test: {
|
|
161
|
-
environment: 'happy-dom' // or 'jsdom', 'node'
|
|
162
|
-
}
|
|
163
|
-
})
|
|
164
|
-
```
|
|
165
|
-
|
|
166
|
-
## Watch Mode
|
|
167
|
-
|
|
168
|
-
```bash
|
|
169
|
-
$ vitest -w
|
|
170
|
-
```
|
|
171
|
-
|
|
172
|
-
Vitest smartly searches the module graph and only rerun the related tests (just like how HMR works in Vite!).
|
|
173
|
-
|
|
174
|
-
## Coverage
|
|
175
|
-
|
|
176
|
-
Vitest works perfectly with [c8](https://github.com/bcoe/c8)
|
|
177
|
-
|
|
178
|
-
```bash
|
|
179
|
-
$ npm i -D c8
|
|
180
|
-
$ c8 vitest
|
|
181
|
-
```
|
|
182
|
-
|
|
183
|
-
```json
|
|
184
|
-
{
|
|
185
|
-
"scripts": {
|
|
186
|
-
"test": "vitest",
|
|
187
|
-
"coverage": "c8 vitest"
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
```
|
|
191
|
-
|
|
192
|
-
For convenience, we also provide a shorthand for passing `--coverage` option to CLI, which will wrap the process with `c8` for you. Note when using the shorthand, you will lose the ability to pass additional options to `c8`.
|
|
193
|
-
|
|
194
|
-
```bash
|
|
195
|
-
$ vitest --coverage
|
|
196
|
-
```
|
|
197
|
-
|
|
198
|
-
For more configuration available, please refer to [c8](https://github.com/bcoe/c8)'s documentation.
|
|
199
|
-
|
|
200
|
-
## Filtering
|
|
201
|
-
|
|
202
|
-
### CLI
|
|
203
|
-
|
|
204
|
-
You can use CLI to filter test files my name:
|
|
205
|
-
|
|
206
|
-
```bash
|
|
207
|
-
$ vitest basic
|
|
208
|
-
```
|
|
209
|
-
|
|
210
|
-
Will only execute test files that contain `basic`, e.g.
|
|
211
|
-
|
|
212
|
-
```
|
|
213
|
-
basic.test.ts
|
|
214
|
-
basic-foo.test.ts
|
|
215
|
-
```
|
|
216
|
-
|
|
217
|
-
### Specifying a Timeout
|
|
218
|
-
|
|
219
|
-
You can optionally pass a timeout in milliseconds as third argument to tests. The default is 5 seconds.
|
|
220
|
-
|
|
221
|
-
```ts
|
|
222
|
-
test('name', async() => { ... }, 1000)
|
|
223
|
-
```
|
|
224
|
-
|
|
225
|
-
Hooks also can receive a timeout, with the same 5 seconds default.
|
|
226
|
-
|
|
227
|
-
```ts
|
|
228
|
-
beforeAll( async() => { ... }, 1000)
|
|
229
|
-
```
|
|
230
|
-
|
|
231
|
-
### Skipping suites and tests
|
|
232
|
-
|
|
233
|
-
Use `.skip` to avoid running certain suites or tests
|
|
234
|
-
|
|
235
|
-
```ts
|
|
236
|
-
describe.skip('skipped suite', () => {
|
|
237
|
-
it('test', () => {
|
|
238
|
-
// Suite skipped, no error
|
|
239
|
-
assert.equal(Math.sqrt(4), 3)
|
|
240
|
-
})
|
|
241
|
-
})
|
|
242
|
-
|
|
243
|
-
describe('suite', () => {
|
|
244
|
-
it.skip('skipped test', () => {
|
|
245
|
-
// Test skipped, no error
|
|
246
|
-
assert.equal(Math.sqrt(4), 3)
|
|
247
|
-
})
|
|
248
|
-
})
|
|
249
|
-
```
|
|
250
|
-
|
|
251
|
-
### Selecting suites and tests to run
|
|
252
|
-
|
|
253
|
-
Use `.only` to only run certain suites or tests
|
|
254
|
-
|
|
255
|
-
```ts
|
|
256
|
-
// Only this suite (and others marked with only) are run
|
|
257
|
-
describe.only('suite', () => {
|
|
258
|
-
it('test', () => {
|
|
259
|
-
assert.equal(Math.sqrt(4), 3)
|
|
260
|
-
})
|
|
261
|
-
})
|
|
262
|
-
|
|
263
|
-
describe('another suite', () => {
|
|
264
|
-
it('skipped test', () => {
|
|
265
|
-
// Test skipped, as tests are running in Only mode
|
|
266
|
-
assert.equal(Math.sqrt(4), 3)
|
|
267
|
-
})
|
|
268
|
-
|
|
269
|
-
it.only('test', () => {
|
|
270
|
-
// Only this test (and others marked with only) are run
|
|
271
|
-
assert.equal(Math.sqrt(4), 2)
|
|
272
|
-
})
|
|
273
|
-
})
|
|
274
|
-
```
|
|
275
|
-
|
|
276
|
-
### Unimplemented suites and tests
|
|
277
|
-
|
|
278
|
-
Use `.todo` to stub suites and tests that should be implemented
|
|
279
|
-
|
|
280
|
-
```ts
|
|
281
|
-
// An entry will be shown in the report for this suite
|
|
282
|
-
describe.todo('unimplemented suite')
|
|
283
|
-
|
|
284
|
-
// An entry will be shown in the report for this test
|
|
285
|
-
describe('suite', () => {
|
|
286
|
-
it.todo('unimplemented test')
|
|
287
|
-
})
|
|
288
|
-
```
|
|
289
|
-
|
|
290
|
-
### Running tests concurrently
|
|
291
|
-
|
|
292
|
-
Use `.concurrent` in consecutive tests to run them in parallel
|
|
293
|
-
|
|
294
|
-
```ts
|
|
295
|
-
// The two tests marked with concurrent will be run in parallel
|
|
296
|
-
describe('suite', () => {
|
|
297
|
-
it('serial test', () => {
|
|
298
|
-
assert.equal(Math.sqrt(4), 3)
|
|
299
|
-
})
|
|
300
|
-
it.concurrent('concurrent test 1', () => {
|
|
301
|
-
assert.equal(Math.sqrt(4), 3)
|
|
302
|
-
})
|
|
303
|
-
it.concurrent('concurrent test 2', () => {
|
|
304
|
-
assert.equal(Math.sqrt(4), 3)
|
|
305
|
-
})
|
|
306
|
-
})
|
|
307
|
-
```
|
|
308
|
-
|
|
309
|
-
If you use `.concurrent` in a suite, every tests in it will be run in parallel
|
|
310
|
-
```ts
|
|
311
|
-
// The two tests marked with concurrent will be run in parallel
|
|
312
|
-
describe.concurrent('suite', () => {
|
|
313
|
-
it('concurrent test 1', () => {
|
|
314
|
-
assert.equal(Math.sqrt(4), 3)
|
|
315
|
-
})
|
|
316
|
-
it('concurrent test 2', () => {
|
|
317
|
-
assert.equal(Math.sqrt(4), 3)
|
|
318
|
-
})
|
|
319
|
-
// No effect, same as not using .concurrent
|
|
320
|
-
it.concurrent('concurrent test 3', () => {
|
|
321
|
-
assert.equal(Math.sqrt(4), 3)
|
|
322
|
-
})
|
|
323
|
-
})
|
|
324
|
-
```
|
|
325
|
-
|
|
326
|
-
You can also use `.skip`, `.only`, and `.todo` with concurrent suite and tests. All the following combinations are valid:
|
|
327
|
-
```js
|
|
328
|
-
describe.concurrent(...)
|
|
329
|
-
|
|
330
|
-
describe.skip.concurrent(...)
|
|
331
|
-
describe.concurrent.skip(...)
|
|
332
|
-
|
|
333
|
-
describe.only.concurrent(...)
|
|
334
|
-
describe.concurrent.only(...)
|
|
335
|
-
|
|
336
|
-
describe.todo.concurrent(...)
|
|
337
|
-
describe.concurrent.todo(...)
|
|
338
|
-
|
|
339
|
-
it.concurrent(...)
|
|
340
|
-
|
|
341
|
-
it.skip.concurrent(...)
|
|
342
|
-
it.concurrent.skip(...)
|
|
343
|
-
|
|
344
|
-
it.only.concurrent(...)
|
|
345
|
-
it.concurrent.only(...)
|
|
346
|
-
|
|
347
|
-
it.todo.concurrent(...)
|
|
348
|
-
it.concurrent.todo(...)
|
|
349
|
-
```
|
|
350
|
-
|
|
351
|
-
|
|
352
86
|
## Sponsors
|
|
353
87
|
|
|
354
88
|
<p align="center">
|