Skip to content

Commit 5562f9e

Browse files
committed
test: wait for watcher to be ready
1 parent 72d0b38 commit 5562f9e

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/core/RoutesFolderWatcher.spec.ts

+18
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { resolveOptions, RoutesFolderOption } from '../options'
1616
import pathe from 'pathe'
1717
import fs from 'node:fs/promises'
1818
import { tmpdir } from 'node:os'
19+
import { type FSWatcher } from 'chokidar'
1920

2021
const FIXTURES_ROOT = pathe.resolve(
2122
pathe.join(tmpdir(), 'vue-router-' + Date.now())
@@ -42,6 +43,7 @@ describe('RoutesFolderWatcher', () => {
4243
await fs.mkdir(srcDir, { recursive: true })
4344

4445
const watcher = new RoutesFolderWatcher(options)
46+
await waitForWatcher(watcher.watcher)
4547
watcherList.push(watcher)
4648

4749
return { watcher, options, rootDir, srcDir }
@@ -73,6 +75,22 @@ describe('RoutesFolderWatcher', () => {
7375
})
7476
}
7577

78+
function waitForWatcher(watcher: FSWatcher) {
79+
return new Promise<void>((resolve, reject) => {
80+
const timeout = setTimeout(() => {
81+
reject(new Error('timeout'))
82+
}, TEST_TIMEOUT)
83+
watcher.on('error', (...args) => {
84+
clearTimeout(timeout)
85+
reject(...args)
86+
})
87+
watcher.on('ready', (...args) => {
88+
clearTimeout(timeout)
89+
resolve(...args)
90+
})
91+
})
92+
}
93+
7694
it('triggers when new pages are added', async () => {
7795
const { watcher, srcDir } = await createWatcher({ src: 'src/pages' })
7896

0 commit comments

Comments
 (0)