diff --git a/src/watcher.ts b/src/watcher.ts index 1d9444e0..37287188 100644 --- a/src/watcher.ts +++ b/src/watcher.ts @@ -30,9 +30,7 @@ export class Watcher { this.mode = opts.mode this.chokidar = chokidarWatch(watchPath, { ignoreInitial: true }) - .on('all', (event, path) => - debugWatcher('Chokidar event: [%s] %s', event, path) - ) + .on('all', (event, path) => this.log(event, path)) .on('change', (f) => this.convert(f)) .on('add', (f) => this.convert(f)) .on('unlink', (f) => this.delete(f)) @@ -42,6 +40,10 @@ export class Watcher { notifier.start() } + private log(event: string, path: string) { + debugWatcher('Chokidar event: [%s] %s', event, path) + } + private async convert(filename: string) { const resolvedFn = path.resolve(filename) const mdFiles = (await this.finder()).filter( diff --git a/test/watcher.ts b/test/watcher.ts index b5ea611f..f60391c5 100644 --- a/test/watcher.ts +++ b/test/watcher.ts @@ -68,19 +68,25 @@ describe('Watcher', () => { // Chokidar events const on = watcher.chokidar.on as jest.Mock + expect(on).toHaveBeenCalledWith('all', expect.any(Function)) expect(on).toHaveBeenCalledWith('change', expect.any(Function)) expect(on).toHaveBeenCalledWith('add', expect.any(Function)) expect(on).toHaveBeenCalledWith('unlink', expect.any(Function)) + const onAll = on.mock.calls.find(([e]) => e === 'all')[1] const onChange = on.mock.calls.find(([e]) => e === 'change')[1] const onAdd = on.mock.calls.find(([e]) => e === 'add')[1] const onUnlink = on.mock.calls.find(([e]) => e === 'unlink')[1] // Callbacks + const log = jest.spyOn(watcher as any, 'log').mockImplementation() const conv = jest.spyOn(watcher as any, 'convert').mockImplementation() const del = jest.spyOn(watcher as any, 'delete').mockImplementation() try { + onAll('event', 'path') + expect(log).toHaveBeenCalledWith('event', 'path') + onChange('change') expect(conv).toHaveBeenCalledWith('change')