|
1 | 1 | import { resolve } from 'node:path' |
2 | 2 | import { fileURLToPath } from 'node:url' |
3 | 3 | import { afterEach, describe, expect, it, vi } from 'vitest' |
| 4 | +import { promiseWithResolvers } from '../../../shared/utils' |
4 | 5 | import { type ViteDevServer, createServer } from '../index' |
5 | 6 |
|
6 | 7 | const stubGetWatchedCode = /\(\)\s*\{\s*return this;\s*\}/ |
| 8 | +const root = fileURLToPath( |
| 9 | + new URL('./fixtures/watcher/nested-root', import.meta.url), |
| 10 | +) |
| 11 | +const watchedFile = fileURLToPath(import.meta.url) |
| 12 | + |
| 13 | +function expectWatcherToBeClosed(watcher: ViteDevServer['watcher']) { |
| 14 | + // Chokidar updates its private `closed` property synchronously in `add()`, |
| 15 | + // while `getWatched()` is populated asynchronously. Rely on the private |
| 16 | + // property here to keep these regression tests deterministic. |
| 17 | + expect(watcher).toHaveProperty('closed', true) |
| 18 | +} |
7 | 19 |
|
8 | 20 | describe('watcher configuration', () => { |
9 | 21 | let server: ViteDevServer | undefined |
@@ -33,10 +45,81 @@ describe('watcher configuration', () => { |
33 | 45 | expect(server.watcher.add.toString()).not.toMatch(stubGetWatchedCode) |
34 | 46 | }) |
35 | 47 |
|
| 48 | + it('keeps the watcher closed when add is called after server close', async () => { |
| 49 | + server = await createServer() |
| 50 | + await server.close() |
| 51 | + |
| 52 | + try { |
| 53 | + expect(server.watcher.add(watchedFile)).toBe(server.watcher) |
| 54 | + expectWatcherToBeClosed(server.watcher) |
| 55 | + } finally { |
| 56 | + await server.watcher.close() |
| 57 | + } |
| 58 | + }) |
| 59 | + |
| 60 | + it('keeps the watcher closed when add is called while server close is pending', async () => { |
| 61 | + const closeStarted = promiseWithResolvers<void>() |
| 62 | + const finishClose = promiseWithResolvers<void>() |
| 63 | + server = await createServer({ |
| 64 | + plugins: [ |
| 65 | + { |
| 66 | + name: 'delay-close', |
| 67 | + async buildEnd() { |
| 68 | + closeStarted.resolve() |
| 69 | + await finishClose.promise |
| 70 | + }, |
| 71 | + }, |
| 72 | + ], |
| 73 | + }) |
| 74 | + |
| 75 | + const closePromise = server.close() |
| 76 | + await closeStarted.promise |
| 77 | + |
| 78 | + try { |
| 79 | + server.watcher.add(watchedFile) |
| 80 | + expectWatcherToBeClosed(server.watcher) |
| 81 | + } finally { |
| 82 | + finishClose.resolve() |
| 83 | + await closePromise |
| 84 | + await server.watcher.close() |
| 85 | + } |
| 86 | + }) |
| 87 | + |
| 88 | + it('keeps the watcher closed when addWatchFile captured during buildStart is called while closing', async () => { |
| 89 | + const closeStarted = promiseWithResolvers<void>() |
| 90 | + const finishClose = promiseWithResolvers<void>() |
| 91 | + let addWatch!: (id: string) => void |
| 92 | + server = await createServer({ |
| 93 | + root, |
| 94 | + plugins: [ |
| 95 | + { |
| 96 | + name: 'capture-add-watch-file', |
| 97 | + buildStart() { |
| 98 | + addWatch = (id: string) => this.addWatchFile(id) |
| 99 | + }, |
| 100 | + async buildEnd() { |
| 101 | + closeStarted.resolve() |
| 102 | + await finishClose.promise |
| 103 | + }, |
| 104 | + }, |
| 105 | + ], |
| 106 | + }) |
| 107 | + |
| 108 | + await server.environments.client.pluginContainer.buildStart() |
| 109 | + const closePromise = server.close() |
| 110 | + await closeStarted.promise |
| 111 | + |
| 112 | + try { |
| 113 | + addWatch(watchedFile) |
| 114 | + expectWatcherToBeClosed(server.watcher) |
| 115 | + } finally { |
| 116 | + finishClose.resolve() |
| 117 | + await closePromise |
| 118 | + await server.watcher.close() |
| 119 | + } |
| 120 | + }) |
| 121 | + |
36 | 122 | it('should watch the root directory, config file dependencies, dotenv files, and the public directory', async () => { |
37 | | - const root = fileURLToPath( |
38 | | - new URL('./fixtures/watcher/nested-root', import.meta.url), |
39 | | - ) |
40 | 123 | server = await createServer({ root }) |
41 | 124 | await new Promise((resolve) => server!.watcher.once('ready', resolve)) |
42 | 125 | // Perform retries here as chokidar may still not be completely watching all directories |
|
0 commit comments