forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.ts
652 lines (562 loc) · 14.5 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
import type { AliasOptions, CommonServerOptions } from 'vite'
import type { PrettyFormatOptions } from 'pretty-format'
import type { FakeTimerInstallOpts } from '@sinonjs/fake-timers'
import type { BuiltinReporters } from '../node/reporters'
import type { TestSequencerConstructor } from '../node/sequencers/types'
import type { CoverageOptions, ResolvedCoverageOptions } from './coverage'
import type { JSDOMOptions } from './jsdom-options'
import type { Reporter } from './reporter'
import type { SnapshotStateOptions } from './snapshot'
import type { Arrayable } from './general'
import type { BenchmarkUserOptions } from './benchmark'
export type BuiltinEnvironment = 'node' | 'jsdom' | 'happy-dom' | 'edge-runtime'
// Record is used, so user can get intellisense for builtin environments, but still allow custom environments
export type VitestEnvironment = BuiltinEnvironment | (string & Record<never, never>)
export type CSSModuleScopeStrategy = 'stable' | 'scoped' | 'non-scoped'
export type SequenceHooks = 'stack' | 'list' | 'parallel'
export type ApiConfig = Pick<CommonServerOptions, 'port' | 'strictPort' | 'host'>
export { JSDOMOptions }
export interface EnvironmentOptions {
/**
* jsdom options.
*/
jsdom?: JSDOMOptions
[x: string]: unknown
}
export type VitestRunMode = 'test' | 'benchmark' | 'typecheck'
export interface InlineConfig {
/**
* Name of the project. Will be used to display in the reporter.
*/
name?: string
/**
* Benchmark options.
*
* @default {}
*/
benchmark?: BenchmarkUserOptions
/**
* Include globs for test files
*
* @default ['**\/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}']
*/
include?: string[]
/**
* Exclude globs for test files
* @default ['node_modules', 'dist', '.idea', '.git', '.cache']
*/
exclude?: string[]
/**
* Include globs for in-source test files
*
* @default []
*/
includeSource?: string[]
/**
* Handling for dependencies inlining or externalizing
*/
deps?: {
/**
* Externalize means that Vite will bypass the package to native Node.
*
* Externalized dependencies will not be applied Vite's transformers and resolvers.
* And does not support HMR on reload.
*
* Typically, packages under `node_modules` are externalized.
*/
external?: (string | RegExp)[]
/**
* Vite will process inlined modules.
*
* This could be helpful to handle packages that ship `.js` in ESM format (that Node can't handle).
*
* If `true`, every dependency will be inlined
*/
inline?: (string | RegExp)[] | true
/**
* Interpret CJS module's default as named exports
*
* @default true
*/
interopDefault?: boolean
/**
* When a dependency is a valid ESM package, try to guess the cjs version based on the path.
* This will significantly improve the performance in huge repo, but might potentially
* cause some misalignment if a package have different logic in ESM and CJS mode.
*
* @default false
*/
fallbackCJS?: boolean
/**
* Use experimental Node loader to resolve imports inside node_modules using Vite resolve algorithm.
* @default false
*/
registerNodeLoader?: boolean
}
/**
* Base directory to scan for the test files
*
* @default `config.root`
*/
dir?: string
/**
* Register apis globally
*
* @default false
*/
globals?: boolean
/**
* Running environment
*
* Supports 'node', 'jsdom', 'happy-dom', 'edge-runtime'
*
* If used unsupported string, will try to load the package `vitest-environment-${env}`
*
* @default 'node'
*/
environment?: VitestEnvironment
/**
* Environment options.
*/
environmentOptions?: EnvironmentOptions
/**
* Automatically assign environment based on globs. The first match will be used.
*
* Format: [glob, environment-name]
*
* @default []
* @example [
* // all tests in tests/dom will run in jsdom
* ['tests/dom/**', 'jsdom'],
* // all tests in tests/ with .edge.test.ts will run in edge-runtime
* ['**\/*.edge.test.ts', 'edge-runtime'],
* // ...
* ]
*/
environmentMatchGlobs?: [string, VitestEnvironment][]
/**
* Update snapshot
*
* @default false
*/
update?: boolean
/**
* Watch mode
*
* @default true
*/
watch?: boolean
/**
* Project root
*
* @default process.cwd()
*/
root?: string
/**
* Custom reporter for output. Can contain one or more built-in report names, reporter instances,
* and/or paths to custom reporters.
*/
reporters?: Arrayable<BuiltinReporters | 'html' | Reporter | Omit<string, BuiltinReporters>>
/**
* Truncates lines in the output to the given length.
* @default stdout.columns || 80
*/
outputTruncateLength?: number
/**
* Maximum number of line to show in a single diff.
* @default 15
*/
outputDiffLines?: number
/**
* The maximum number of characters allowed in a single object before doing a diff.
* Vitest tries to stringify an object before doing a diff, but if the object is too large,
* it will reduce the depth of the object to fit within this limit.
* Because of this if object is too big or nested, you might not see the diff.
* @default 10000
*/
outputDiffMaxSize?: number
/**
* Maximum number of lines in a diff overall.
* @default 50
*/
outputDiffMaxLines?: number
/**
* Write test results to a file when the --reporter=json` or `--reporter=junit` option is also specified.
* Also definable individually per reporter by using an object instead.
*/
outputFile?: string | (Partial<Record<BuiltinReporters, string>> & Record<string, string>)
/**
* Enable multi-threading
*
* @default true
*/
threads?: boolean
/**
* Maximum number of threads
*
* @default available CPUs
*/
maxThreads?: number
/**
* Minimum number of threads
*
* @default available CPUs
*/
minThreads?: number
/**
* Default timeout of a test in milliseconds
*
* @default 5000
*/
testTimeout?: number
/**
* Default timeout of a hook in milliseconds
*
* @default 10000
*/
hookTimeout?: number
/**
* Default timeout to wait for close when Vitest shuts down, in milliseconds
*
* @default 1000
*/
teardownTimeout?: number
/**
* Silent mode
*
* @default false
*/
silent?: boolean
/**
* Hide logs for skipped tests
*
* @default false
*/
silentSkip?: boolean
/**
* Path to setup files
*/
setupFiles?: string | string[]
/**
* Path to global setup files
*/
globalSetup?: string | string[]
/**
* Glob pattern of file paths to be ignore from triggering watch rerun
*/
watchExclude?: string[]
/**
* Glob patter of file paths that will trigger the whole suite rerun
*
* Useful if you are testing calling CLI commands
*
* @default []
*/
forceRerunTriggers?: string[]
/**
* Isolate environment for each test file
*
* @default true
*/
isolate?: boolean
/**
* Coverage options
*/
coverage?: CoverageOptions
/**
* run test names with the specified pattern
*/
testNamePattern?: string | RegExp
/**
* Will call `.mockClear()` on all spies before each test
* @default false
*/
clearMocks?: boolean
/**
* Will call `.mockReset()` on all spies before each test
* @default false
*/
mockReset?: boolean
/**
* Will call `.mockRestore()` on all spies before each test
* @default false
*/
restoreMocks?: boolean
/**
* Will restore all global stubs to their original values before each test
* @default false
*/
unstubGlobals?: boolean
/**
* Will restore all env stubs to their original values before each test
* @default false
*/
unstubEnvs?: boolean
/**
* Serve API options.
*
* When set to true, the default port is 51204.
*
* @default false
*/
api?: boolean | number | ApiConfig
/**
* Enable Vitest UI
* @internal WIP
*/
ui?: boolean
/**
* Use in browser environment
* @experimental
*/
browser?: boolean
/**
* Open UI automatically.
*
* @default true
*/
open?: boolean
/**
* Base url for the UI
*
* @default '/__vitest__/'
*/
uiBase?: string
/**
* Determine the transform method of modules
*/
transformMode?: {
/**
* Use SSR transform pipeline for the specified files.
* Vite plugins will receive `ssr: true` flag when processing those files.
*
* @default [/\.([cm]?[jt]sx?|json)$/]
*/
ssr?: RegExp[]
/**
* First do a normal transform pipeline (targeting browser),
* then then do a SSR rewrite to run the code in Node.
* Vite plugins will receive `ssr: false` flag when processing those files.
*
* @default other than `ssr`
*/
web?: RegExp[]
}
/**
* Format options for snapshot testing.
*/
snapshotFormat?: PrettyFormatOptions
/**
* Resolve custom snapshot path
*/
resolveSnapshotPath?: (path: string, extension: string) => string
/**
* Pass with no tests
*/
passWithNoTests?: boolean
/**
* Allow tests and suites that are marked as only
*/
allowOnly?: boolean
/**
* Show heap usage after each test. Useful for debugging memory leaks.
*/
logHeapUsage?: boolean
/**
* Custom environment variables assigned to `process.env` before running tests.
*/
env?: Record<string, string>
/**
* Options for @sinon/fake-timers
*/
fakeTimers?: FakeTimerInstallOpts
/**
* Custom handler for console.log in tests.
*
* Return `false` to ignore the log.
*/
onConsoleLog?: (log: string, type: 'stdout' | 'stderr') => false | void
/**
* Indicates if CSS files should be processed.
*
* When excluded, the CSS files will be replaced with empty strings to bypass the subsequent processing.
*
* @default { include: [], modules: { classNameStrategy: false } }
*/
css?: boolean | {
include?: RegExp | RegExp[]
exclude?: RegExp | RegExp[]
modules?: {
classNameStrategy?: CSSModuleScopeStrategy
}
}
/**
* A number of tests that are allowed to run at the same time marked with `test.concurrent`.
* @default 5
*/
maxConcurrency?: number
/**
* Options for configuring cache policy.
* @default { dir: 'node_modules/.vitest' }
*/
cache?: false | {
dir?: string
}
/**
* Options for configuring the order of running tests.
*/
sequence?: {
/**
* Class that handles sorting and sharding algorithm.
* If you only need to change sorting, you can extend
* your custom sequencer from `BaseSequencer` from `vitest/node`.
* @default BaseSequencer
*/
sequencer?: TestSequencerConstructor
/**
* Should tests run in random order.
* @default false
*/
shuffle?: boolean
/**
* Seed for the random number generator.
* @default Date.now()
*/
seed?: number
/**
* Defines how hooks should be ordered
* - `stack` will order "after" hooks in reverse order, "before" hooks will run sequentially
* - `list` will order hooks in the order they are defined
* - `parallel` will run hooks in a single group in parallel
* @default 'parallel'
*/
hooks?: SequenceHooks
}
/**
* Specifies an `Object`, or an `Array` of `Object`,
* which defines aliases used to replace values in `import` or `require` statements.
* Will be merged with the default aliases inside `resolve.alias`.
*/
alias?: AliasOptions
/**
* Ignore any unhandled errors that occur
*/
dangerouslyIgnoreUnhandledErrors?: boolean
/**
* Options for configuring typechecking test environment.
*/
typecheck?: Partial<TypecheckConfig>
/**
* The number of milliseconds after which a test is considered slow and reported as such in the results.
*
* @default 300
*/
slowTestThreshold?: number
/**
* Path to a custom test runner.
*/
runner?: string
}
export interface TypecheckConfig {
/**
* What tools to use for type checking.
*/
checker: 'tsc' | 'vue-tsc' | (string & Record<never, never>)
/**
* Pattern for files that should be treated as test files
*/
include: string[]
/**
* Pattern for files that should not be treated as test files
*/
exclude: string[]
/**
* Check JS files that have `@ts-check` comment.
* If you have it enabled in tsconfig, this will not overwrite it.
*/
allowJs?: boolean
/**
* Do not fail, if Vitest found errors outside the test files.
*/
ignoreSourceErrors?: boolean
/**
* Path to tsconfig, relative to the project root.
*/
tsconfig?: string
}
export interface UserConfig extends InlineConfig {
/**
* Path to the config file.
*
* Default resolving to `vitest.config.*`, `vite.config.*`
*/
config?: string | undefined
/**
* Use happy-dom
*/
dom?: boolean
/**
* Run tests that cover a list of source files
*/
related?: string[] | string
/**
* Overrides Vite mode
* @default 'test'
*/
mode?: string
/**
* Runs tests that are affected by the changes in the repository, or between specified branch or commit hash
* Requires initialized git repository
* @default false
*/
changed?: boolean | string
/**
* Test suite shard to execute in a format of <index>/<count>.
* Will divide tests into a `count` numbers, and run only the `indexed` part.
* Cannot be used with enabled watch.
* @example --shard=2/3
*/
shard?: string
}
export interface ResolvedConfig extends Omit<Required<UserConfig>, 'config' | 'filters' | 'coverage' | 'testNamePattern' | 'related' | 'api' | 'reporters' | 'resolveSnapshotPath' | 'benchmark' | 'shard' | 'cache' | 'sequence' | 'typecheck' | 'runner'> {
mode: VitestRunMode
base?: string
config?: string
filters?: string[]
testNamePattern?: RegExp
related?: string[]
coverage: ResolvedCoverageOptions
snapshotOptions: SnapshotStateOptions
reporters: (Reporter | BuiltinReporters)[]
defines: Record<string, any>
api?: ApiConfig
benchmark?: Required<Omit<BenchmarkUserOptions, 'outputFile'>> & {
outputFile?: BenchmarkUserOptions['outputFile']
}
shard?: {
index: number
count: number
}
cache: {
dir: string
} | false
sequence: {
sequencer: TestSequencerConstructor
hooks: SequenceHooks
shuffle?: boolean
seed?: number
}
typecheck: TypecheckConfig
runner?: string
}
export type RuntimeConfig = Pick<
UserConfig,
| 'allowOnly'
| 'testTimeout'
| 'hookTimeout'
| 'clearMocks'
| 'mockReset'
| 'restoreMocks'
| 'fakeTimers'
| 'maxConcurrency'
> & { sequence?: { hooks?: SequenceHooks } }