Skip to content

Commit

Permalink
chore: simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Jun 13, 2024
1 parent 62bdf6f commit d9edc0c
Showing 1 changed file with 4 additions and 16 deletions.
20 changes: 4 additions & 16 deletions packages/vite/src/node/server/__tests__/watcher.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { afterEach, describe, expect, it } from 'vitest'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { type ViteDevServer, createServer } from '../index'

const stubGetWatchedCode = /getWatched\(\) \{.+?return \{\};.+?\}/s
Expand Down Expand Up @@ -39,9 +39,9 @@ describe('watcher configuration', () => {
)
server = await createServer({ root })
await new Promise((resolve) => server!.watcher.once('ready', resolve))
// At this point, there's still a chance that chokidar has not watch all the necessary directories yet
// so we have to retry here for a bit
await withRetry(() => {
// Perform retries here as chokidar may still not be completely watching all directories
// after the `ready` event
await vi.waitFor(() => {
const watchedDirs = Object.keys(server!.watcher.getWatched())
expect(watchedDirs).toEqual(
expect.arrayContaining([
Expand All @@ -54,15 +54,3 @@ describe('watcher configuration', () => {
})
})
})

async function withRetry(func: () => Promise<void> | void): Promise<void> {
const maxTries = process.env.CI ? 3 : 1
for (let tries = 0; tries < maxTries; tries++) {
try {
await func()
return
} catch {}
await new Promise((r) => setTimeout(r, 50))
}
await func()
}

0 comments on commit d9edc0c

Please sign in to comment.