MCPcopy
hub / github.com/vitest-dev/vitest / VitestWatcher

Class VitestWatcher

packages/vitest/src/node/watcher.ts:8–240  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6import pm from 'picomatch'
7
8export class VitestWatcher {
9 /**
10 * Modules that will be invalidated on the next run.
11 */
12 public readonly invalidates: Set<string> = new Set()
13 /**
14 * Test files that have changed and need to be rerun.
15 */
16 public readonly changedTests: Set<string> = new Set()
17
18 private readonly _onRerun: ((file: string) => void)[] = []
19
20 constructor(private vitest: Vitest) {}
21
22 /**
23 * Register a handler that will be called when test files need to be rerun.
24 * The callback can receive several files in case the changed file is imported by several test files.
25 * Several invocations of this method will add multiple handlers.
26 * @internal
27 */
28 onWatcherRerun(cb: (file: string) => void): this {
29 this._onRerun.push(cb)
30 return this
31 }
32
33 public close(): void {
34 this.vitest.vite.watcher.close()
35 }
36
37 public unregisterWatcher: () => void = noop
38 public registerWatcher(): this {
39 const watcher = this.vitest.vite.watcher
40
41 if (this.vitest.config.forceRerunTriggers.length) {
42 watcher.add(this.vitest.config.forceRerunTriggers)
43 }
44
45 watcher.on('change', this.onFileChange)
46 watcher.on('unlink', this.onFileDelete)
47 watcher.on('add', this.onFileCreate)
48
49 this.unregisterWatcher = () => {
50 watcher.off('change', this.onFileChange)
51 watcher.off('unlink', this.onFileDelete)
52 watcher.off('add', this.onFileCreate)
53 this.unregisterWatcher = noop
54 }
55 return this
56 }
57
58 private scheduleRerun(file: string): void {
59 this._onRerun.forEach(cb => cb(file))
60 }
61
62 private getTestFilesFromWatcherTrigger(id: string): boolean {
63 if (!this.vitest.config.watchTriggerPatterns) {
64 return false
65 }

Callers

nothing calls this directly

Calls 15

scheduleRerunMethod · 0.95
handleFileChangedMethod · 0.95
slashFunction · 0.90
readFileSyncFunction · 0.85
clearHighlightCacheMethod · 0.80
invalidateFileMethod · 0.80
_removeCachedTestFileMethod · 0.80
removeFromCacheMethod · 0.80
removeStatsMethod · 0.80
reportMethod · 0.80
matchesTestGlobMethod · 0.80

Tested by

no test coverage detected