| 6 | import pm from 'picomatch' |
| 7 | |
| 8 | export 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 | } |
nothing calls this directly
no test coverage detected