Skip to content

Commit

Permalink
Update test for Watcher logger
Browse files Browse the repository at this point in the history
  • Loading branch information
yhatt committed Feb 21, 2025
1 parent a3a1bc0 commit b747ae8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -42,6 +40,10 @@ export class Watcher {
notifier.start()
}

private log(event: string, path: string) {
debugWatcher('Chokidar event: [%s] %s', event, path)
}

Check warning on line 45 in src/watcher.ts

View check run for this annotation

Codecov / codecov/patch

src/watcher.ts#L44-L45

Added lines #L44 - L45 were not covered by tests

private async convert(filename: string) {
const resolvedFn = path.resolve(filename)
const mdFiles = (await this.finder()).filter(
Expand Down
6 changes: 6 additions & 0 deletions test/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down

0 comments on commit b747ae8

Please sign in to comment.