forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.ts
216 lines (181 loc) · 6.14 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
import { resolveModule } from 'local-pkg'
import { normalize, resolve } from 'pathe'
import c from 'picocolors'
import type { ResolvedConfig as ResolvedViteConfig } from 'vite'
import type { ApiConfig, ResolvedConfig, UserConfig, VitestRunMode } from '../types'
import { defaultPort } from '../constants'
import { benchmarkConfigDefaults, configDefaults } from '../defaults'
import { toArray } from '../utils'
import { VitestCache } from './cache'
import { BaseSequencer } from './sequencers/BaseSequencer'
import { RandomSequencer } from './sequencers/RandomSequencer'
const extraInlineDeps = [
/^(?!.*(?:node_modules)).*\.mjs$/,
/^(?!.*(?:node_modules)).*\.cjs\.js$/,
// Vite client
/vite\w*\/dist\/client\/env.mjs/,
// Vitest
/\/vitest\/dist\//,
// yarn's .store folder
/vitest-virtual-\w+\/dist/,
// cnpm
/@vitest\/dist/,
// Nuxt
'@nuxt/test-utils',
]
export function resolveApiConfig<Options extends ApiConfig & UserConfig>(
options: Options,
): ApiConfig | undefined {
let api: ApiConfig | undefined
if ((options.ui || options.browser) && !options.api)
api = { port: defaultPort }
else if (options.api === true)
api = { port: defaultPort }
else if (typeof options.api === 'number')
api = { port: options.api }
if (typeof options.api === 'object') {
if (api) {
if (options.api.port)
api.port = options.api.port
if (options.api.strictPort)
api.strictPort = options.api.strictPort
if (options.api.host)
api.host = options.api.host
}
else {
api = { ...options.api }
}
}
if (api) {
if (!api.port)
api.port = defaultPort
}
return api
}
export function resolveConfig(
mode: VitestRunMode,
options: UserConfig,
viteConfig: ResolvedViteConfig,
): ResolvedConfig {
if (options.dom) {
if (
viteConfig.test?.environment != null
&& viteConfig.test!.environment !== 'happy-dom'
) {
console.warn(
c.yellow(
`${c.inverse(c.yellow(' Vitest '))} Your config.test.environment ("${
viteConfig.test.environment
}") conflicts with --dom flag ("happy-dom"), ignoring "${
viteConfig.test.environment
}"`,
),
)
}
options.environment = 'happy-dom'
}
const resolved = {
...configDefaults,
...options,
root: viteConfig.root,
mode,
} as ResolvedConfig
if (viteConfig.base !== '/')
resolved.base = viteConfig.base
if (options.shard) {
if (resolved.watch)
throw new Error('You cannot use --shard option with enabled watch')
const [indexString, countString] = options.shard.split('/')
const index = Math.abs(parseInt(indexString, 10))
const count = Math.abs(parseInt(countString, 10))
if (isNaN(count) || count <= 0)
throw new Error('--shard <count> must be a positive number')
if (isNaN(index) || index <= 0 || index > count)
throw new Error('--shard <index> must be a positive number less then <count>')
resolved.shard = { index, count }
}
resolved.deps = resolved.deps || {}
// vitenode will try to import such file with native node,
// but then our mocker will not work properly
if (resolved.deps.inline !== true) {
// eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error
// @ts-ignore ssr is not typed in Vite 2, but defined in Vite 3, so we can't use expect-error
const ssrOptions = viteConfig.ssr
if (ssrOptions?.noExternal === true && resolved.deps.inline == null) {
resolved.deps.inline = true
}
else {
resolved.deps.inline ??= []
resolved.deps.inline.push(...extraInlineDeps)
}
}
// disable loader for Yarn PnP until Node implements chain loader
// https://github.com/nodejs/node/pull/43772
resolved.deps.registerNodeLoader ??= false
resolved.testNamePattern = resolved.testNamePattern
? resolved.testNamePattern instanceof RegExp
? resolved.testNamePattern
: new RegExp(resolved.testNamePattern)
: undefined
const CI = !!process.env.CI
const UPDATE_SNAPSHOT = resolved.update || process.env.UPDATE_SNAPSHOT
resolved.snapshotOptions = {
snapshotFormat: resolved.snapshotFormat || {},
updateSnapshot: CI && !UPDATE_SNAPSHOT
? 'none'
: UPDATE_SNAPSHOT
? 'all'
: 'new',
resolveSnapshotPath: options.resolveSnapshotPath,
}
if (options.resolveSnapshotPath)
delete (resolved as UserConfig).resolveSnapshotPath
if (process.env.VITEST_MAX_THREADS)
resolved.maxThreads = parseInt(process.env.VITEST_MAX_THREADS)
if (process.env.VITEST_MIN_THREADS)
resolved.minThreads = parseInt(process.env.VITEST_MIN_THREADS)
if (mode === 'benchmark') {
resolved.benchmark = {
...benchmarkConfigDefaults,
...resolved.benchmark,
}
// override test config
resolved.coverage.enabled = false
resolved.include = resolved.benchmark.include
resolved.exclude = resolved.benchmark.exclude
resolved.includeSource = resolved.benchmark.includeSource
}
resolved.setupFiles = toArray(resolved.setupFiles || []).map(file =>
normalize(
resolveModule(file, { paths: [resolved.root] })
?? resolve(resolved.root, file),
),
)
// the server has been created, we don't need to override vite.server options
resolved.api = resolveApiConfig(options)
if (options.related)
resolved.related = toArray(options.related).map(file => resolve(resolved.root, file))
resolved.reporters = Array.from(new Set([
...toArray(resolved.reporters),
// @ts-expect-error from CLI
...toArray(resolved.reporter),
])).filter(Boolean)
if (!resolved.reporters.length)
resolved.reporters.push('default')
if (resolved.changed)
resolved.passWithNoTests ??= true
resolved.css ??= {}
if (typeof resolved.css === 'object')
resolved.css.include ??= [/\.module\./]
resolved.cache ??= { dir: '' }
if (resolved.cache)
resolved.cache.dir = VitestCache.resolveCacheDir(resolved.root, resolved.cache.dir)
if (!resolved.sequence?.sequencer) {
resolved.sequence ??= {} as any
// CLI flag has higher priority
resolved.sequence.sequencer = resolved.sequence.shuffle
? RandomSequencer
: BaseSequencer
}
return resolved
}