/
core.ts
516 lines (418 loc) · 14.2 KB
/
core.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
import { existsSync, promises as fs } from 'fs'
import readline from 'readline'
import type { ViteDevServer } from 'vite'
import { relative, toNamespacedPath } from 'pathe'
import fg from 'fast-glob'
import mm from 'micromatch'
import c from 'picocolors'
import { ViteNodeRunner } from 'vite-node/client'
import { ViteNodeServer } from 'vite-node/server'
import type { ArgumentsType, Reporter, ResolvedConfig, UserConfig } from '../types'
import { SnapshotManager } from '../integrations/snapshot/manager'
import { clearTimeout, deepMerge, hasFailed, noop, setTimeout, slash, toArray } from '../utils'
import { cleanCoverage, reportCoverage } from '../integrations/coverage'
import { createPool } from './pool'
import type { WorkerPool } from './pool'
import { createReporters } from './reporters/utils'
import { StateManager } from './state'
import { resolveConfig } from './config'
import { printError } from './error'
import { VitestGit } from './git'
const WATCHER_DEBOUNCE = 100
const CLOSE_TIMEOUT = 1_000
export class Vitest {
config: ResolvedConfig = undefined!
configOverride: Partial<ResolvedConfig> | undefined
server: ViteDevServer = undefined!
state: StateManager = undefined!
snapshot: SnapshotManager = undefined!
reporters: Reporter[] = undefined!
console: Console
pool: WorkerPool | undefined
outputStream = process.stdout
errorStream = process.stderr
vitenode: ViteNodeServer = undefined!
invalidates: Set<string> = new Set()
changedTests: Set<string> = new Set()
runningPromise?: Promise<void>
closingPromise?: Promise<void>
isFirstRun = true
restartsCount = 0
runner: ViteNodeRunner = undefined!
private _onRestartListeners: Array<() => void> = []
constructor() {
this.console = globalThis.console
}
async setServer(options: UserConfig, server: ViteDevServer) {
this.unregisterWatcher?.()
clearTimeout(this._rerunTimer)
this.restartsCount += 1
this.pool?.close()
this.pool = undefined
const resolved = resolveConfig(options, server.config)
this.server = server
this.config = resolved
this.state = new StateManager()
this.snapshot = new SnapshotManager({ ...resolved.snapshotOptions })
if (this.config.watch)
this.registerWatcher()
this.vitenode = new ViteNodeServer(server, this.config)
const node = this.vitenode
this.runner = new ViteNodeRunner({
root: server.config.root,
base: server.config.base,
fetchModule(id: string) {
return node.fetchModule(id)
},
resolveId(id: string, importer: string | undefined) {
return node.resolveId(id, importer)
},
})
this.reporters = await createReporters(resolved.reporters, this.runner.executeFile.bind(this.runner))
this.runningPromise = undefined
this._onRestartListeners.forEach(fn => fn())
if (resolved.coverage.enabled)
await cleanCoverage(resolved.coverage, resolved.coverage.clean)
}
getConfig() {
const hasCustomReporter = toArray(this.config.reporters)
.some(reporter => typeof reporter !== 'string')
// cannot be serialized for sending to workers
// reimplemented on rpc
if (this.config.snapshotOptions.resolveSnapshotPath)
this.config.snapshotOptions.resolveSnapshotPath = undefined
if (!hasCustomReporter && !this.configOverride)
return this.config
const config = deepMerge({}, this.config)
if (this.configOverride)
deepMerge(config, this.configOverride)
// Custom reporters cannot be serialized for sending to workers #614
// but workers don't need reporters anyway
if (hasCustomReporter)
config.reporters = []
return config as ResolvedConfig
}
async start(filters?: string[]) {
await this.report('onInit', this)
const files = await this.filterTestsBySource(
await this.globTestFiles(filters),
)
if (!files.length) {
const exitCode = this.config.passWithNoTests ? 0 : 1
const comma = c.dim(', ')
if (filters?.length)
this.console.error(c.dim('filter: ') + c.yellow(filters.join(comma)))
if (this.config.include)
this.console.error(c.dim('include: ') + c.yellow(this.config.include.join(comma)))
if (this.config.watchIgnore)
this.console.error(c.dim('ignore: ') + c.yellow(this.config.watchIgnore.join(comma)))
if (this.config.passWithNoTests)
this.log('No test files found, exiting with code 0\n')
else
this.error(c.red('\nNo test files found, exiting with code 1'))
process.exit(exitCode)
}
await this.runFiles(files)
if (this.config.coverage.enabled)
await reportCoverage(this)
if (this.config.watch)
await this.report('onWatcherStart')
}
private async getTestDependencies(filepath: string) {
const deps = new Set<string>()
const addImports = async (filepath: string) => {
const transformed = await this.vitenode.transformRequest(filepath)
if (!transformed)
return
const dependencies = [...transformed.deps || [], ...transformed.dynamicDeps || []]
for (const dep of dependencies) {
const path = await this.server.pluginContainer.resolveId(dep, filepath, { ssr: true })
const fsPath = path && !path.external && path.id.split('?')[0]
if (fsPath && !fsPath.includes('node_modules') && !deps.has(fsPath) && existsSync(fsPath)) {
deps.add(fsPath)
await addImports(fsPath)
}
}
}
await addImports(filepath)
return deps
}
async filterTestsBySource(tests: string[]) {
if (this.config.changed && !this.config.related) {
const vitestGit = new VitestGit(this.config.root)
const related = await vitestGit.findChangedFiles({
changedSince: this.config.changed,
})
if (!related) {
this.error(c.red('Could not find Git root. Have you initialized git with `git init`?\n'))
process.exit(1)
}
this.config.related = Array.from(new Set(related))
}
const related = this.config.related
if (!related)
return tests
// don't run anything if no related sources are found
if (!related.length)
return []
const testDeps = await Promise.all(
tests.map(async (filepath) => {
const deps = await this.getTestDependencies(filepath)
return [filepath, deps] as const
}),
)
const runningTests = []
for (const [filepath, deps] of testDeps) {
// if deps or the test itself were changed
if (deps.size && related.some(path => path === filepath || deps.has(path)))
runningTests.push(filepath)
}
return runningTests
}
async runFiles(files: string[]) {
await this.runningPromise
this.runningPromise = (async () => {
if (!this.pool)
this.pool = createPool(this)
const invalidates = Array.from(this.invalidates)
this.invalidates.clear()
this.snapshot.clear()
this.state.clearErrors()
try {
await this.pool.runTests(files, invalidates)
}
catch (err) {
this.state.catchError(err, 'Unhandled Error')
}
if (hasFailed(this.state.getFiles()))
process.exitCode = 1
await this.report('onFinished', this.state.getFiles(), this.state.getUnhandledErrors())
})()
.finally(() => {
this.runningPromise = undefined
})
return await this.runningPromise
}
async rerunFiles(files: string[] = this.state.getFilepaths(), trigger?: string) {
await this.report('onWatcherRerun', files, trigger)
await this.runFiles(files)
await this.report('onWatcherStart')
}
async changeNamePattern(pattern: string, files: string[] = this.state.getFilepaths(), trigger?: string) {
this.config.testNamePattern = pattern ? new RegExp(pattern) : undefined
await this.rerunFiles(files, trigger)
}
async returnFailed() {
await this.rerunFiles(this.state.getFailedFilepaths(), 'rerun failed')
}
async updateSnapshot(files?: string[]) {
// default to failed files
files = files || [
...this.state.getFailedFilepaths(),
...this.snapshot.summary.uncheckedKeysByFile.map(s => s.filePath),
]
this.configOverride = {
snapshotOptions: {
updateSnapshot: 'all',
},
}
try {
await this.rerunFiles(files, 'update snapshot')
}
finally {
this.configOverride = undefined
}
}
log(...args: any[]) {
this.console.log(...args)
}
error(...args: any[]) {
this.console.error(...args)
}
clearScreen() {
if (this.server.config.clearScreen === false)
return
const repeatCount = process.stdout.rows - 2
const blank = repeatCount > 0 ? '\n'.repeat(repeatCount) : ''
this.console.log(blank)
readline.cursorTo(process.stdout, 0, 0)
readline.clearScreenDown(process.stdout)
}
private _rerunTimer: any
private async scheduleRerun(triggerId: string) {
const currentCount = this.restartsCount
clearTimeout(this._rerunTimer)
await this.runningPromise
clearTimeout(this._rerunTimer)
// server restarted
if (this.restartsCount !== currentCount)
return
this._rerunTimer = setTimeout(async () => {
if (this.changedTests.size === 0) {
this.invalidates.clear()
return
}
// server restarted
if (this.restartsCount !== currentCount)
return
this.isFirstRun = false
// add previously failed files
// if (RERUN_FAILED) {
// ctx.state.getFiles().forEach((file) => {
// if (file.result?.state === 'fail')
// changedTests.add(file.filepath)
// })
// }
this.snapshot.clear()
const files = Array.from(this.changedTests)
this.changedTests.clear()
if (this.config.coverage.enabled && this.config.coverage.cleanOnRerun)
await cleanCoverage(this.config.coverage)
await this.report('onWatcherRerun', files, triggerId)
await this.runFiles(files)
if (this.config.coverage.enabled)
await reportCoverage(this)
await this.report('onWatcherStart')
}, WATCHER_DEBOUNCE)
}
private unregisterWatcher = noop
private registerWatcher() {
const onChange = (id: string) => {
id = slash(id)
const needsRerun = this.handleFileChanged(id)
if (needsRerun)
this.scheduleRerun(id)
}
const onUnlink = (id: string) => {
id = slash(id)
this.invalidates.add(id)
if (this.state.filesMap.has(id)) {
this.state.filesMap.delete(id)
this.changedTests.delete(id)
this.report('onTestRemoved', id)
}
}
const onAdd = async (id: string) => {
id = slash(id)
if (await this.isTargetFile(id)) {
this.changedTests.add(id)
this.scheduleRerun(id)
}
}
const watcher = this.server.watcher
watcher.on('change', onChange)
watcher.on('unlink', onUnlink)
watcher.on('add', onAdd)
this.unregisterWatcher = () => {
watcher.off('change', onChange)
watcher.off('unlink', onUnlink)
watcher.off('add', onAdd)
this.unregisterWatcher = noop
}
}
/**
* @returns A value indicating whether rerun is needed (changedTests was mutated)
*/
private handleFileChanged(id: string): boolean {
if (this.changedTests.has(id) || this.invalidates.has(id) || this.config.watchIgnore.some(i => id.match(i)))
return false
const mod = this.server.moduleGraph.getModuleById(id)
if (!mod)
return false
this.invalidates.add(id)
if (this.state.filesMap.has(id)) {
this.changedTests.add(id)
return true
}
let rerun = false
mod.importers.forEach((i) => {
if (!i.id)
return
const heedsRerun = this.handleFileChanged(i.id)
if (heedsRerun)
rerun = true
})
return rerun
}
async close() {
if (!this.closingPromise) {
this.closingPromise = Promise.allSettled([
this.pool?.close(),
this.server.close(),
].filter(Boolean)).then((results) => {
results.filter(r => r.status === 'rejected').forEach((err) => {
this.error('error during close', (err as PromiseRejectedResult).reason)
})
})
}
return this.closingPromise
}
async exit(force = false) {
setTimeout(() => {
console.warn(`close timed out after ${CLOSE_TIMEOUT}ms`)
process.exit()
}, CLOSE_TIMEOUT).unref()
await this.close()
if (force)
process.exit()
}
async report<T extends keyof Reporter>(name: T, ...args: ArgumentsType<Reporter[T]>) {
await Promise.all(this.reporters.map(r => r[name]?.(
// @ts-expect-error let me go
...args,
)))
}
async globTestFiles(filters: string[] = []) {
const globOptions = {
absolute: true,
cwd: this.config.dir || this.config.root,
ignore: this.config.exclude,
}
let testFiles = await fg(this.config.include, globOptions)
if (filters.length && process.platform === 'win32')
filters = filters.map(f => toNamespacedPath(f))
if (filters.length)
testFiles = testFiles.filter(i => filters.some(f => i.includes(f)))
if (this.config.includeSource) {
let files = await fg(this.config.includeSource, globOptions)
if (filters.length)
files = files.filter(i => filters.some(f => i.includes(f)))
await Promise.all(files.map(async (file) => {
try {
const code = await fs.readFile(file, 'utf-8')
if (this.isInSourceTestFile(code))
testFiles.push(file)
}
catch {
return null
}
}))
}
return testFiles
}
async isTargetFile(id: string, source?: string): Promise<boolean> {
const relativeId = relative(this.config.dir || this.config.root, id)
if (mm.isMatch(relativeId, this.config.exclude))
return false
if (mm.isMatch(relativeId, this.config.include))
return true
if (this.config.includeSource?.length && mm.isMatch(relativeId, this.config.includeSource)) {
source = source || await fs.readFile(id, 'utf-8')
return this.isInSourceTestFile(source)
}
return false
}
isInSourceTestFile(code: string) {
return code.includes('import.meta.vitest')
}
printError(err: unknown, fullStack = false, type?: string) {
return printError(err, this, {
fullStack,
type,
showCodeFrame: true,
})
}
onServerRestarted(fn: () => void) {
this._onRestartListeners.push(fn)
}
}