1
- import { describe , expect , it } from 'vitest'
1
+ import { resolve } from 'node:path'
2
+ import {
3
+ type MockInstance ,
4
+ afterEach ,
5
+ beforeEach ,
6
+ describe ,
7
+ expect ,
8
+ it ,
9
+ vi ,
10
+ } from 'vitest'
11
+ import chokidar from 'chokidar'
2
12
import { createServer } from '../index'
3
13
4
14
const stubGetWatchedCode = / g e t W a t c h e d \( \) \{ .+ ?r e t u r n \{ \} ; .+ ?\} / s
5
15
16
+ let watchSpy : MockInstance <
17
+ Parameters < typeof chokidar . watch > ,
18
+ ReturnType < typeof chokidar . watch >
19
+ >
20
+
21
+ vi . mock ( '../../config' , async ( ) => {
22
+ const config : typeof import ( '../../config' ) =
23
+ await vi . importActual ( '../../config' )
24
+ const resolveConfig = config . resolveConfig
25
+ vi . spyOn ( config , 'resolveConfig' ) . mockImplementation ( async ( ...args ) => {
26
+ const resolved : Awaited < ReturnType < typeof resolveConfig > > =
27
+ await resolveConfig . call ( config , ...args )
28
+ resolved . configFileDependencies . push (
29
+ resolve ( 'fake/config/dependency.js' ) . replace ( / \\ / g, '/' ) ,
30
+ )
31
+ return resolved
32
+ } )
33
+ return config
34
+ } )
35
+
6
36
describe ( 'watcher configuration' , ( ) => {
37
+ beforeEach ( ( ) => {
38
+ watchSpy = vi . spyOn ( chokidar , 'watch' )
39
+ } )
40
+
41
+ afterEach ( ( ) => {
42
+ watchSpy . mockRestore ( )
43
+ } )
44
+
7
45
it ( 'when watcher is disabled, return noop watcher' , async ( ) => {
8
46
const server = await createServer ( {
9
47
server : {
@@ -21,4 +59,27 @@ describe('watcher configuration', () => {
21
59
} )
22
60
expect ( server . watcher . getWatched . toString ( ) ) . not . toMatch ( stubGetWatchedCode )
23
61
} )
62
+
63
+ it ( 'should watch the root directory, config file dependencies, dotenv files, and the public directory' , async ( ) => {
64
+ await createServer ( {
65
+ server : {
66
+ watch : { } ,
67
+ } ,
68
+ publicDir : '__test_public__' ,
69
+ } )
70
+ expect ( watchSpy ) . toHaveBeenLastCalledWith (
71
+ expect . arrayContaining (
72
+ [
73
+ process . cwd ( ) ,
74
+ resolve ( 'fake/config/dependency.js' ) ,
75
+ resolve ( '.env' ) ,
76
+ resolve ( '.env.local' ) ,
77
+ resolve ( '.env.development' ) ,
78
+ resolve ( '.env.development.local' ) ,
79
+ resolve ( '__test_public__' ) ,
80
+ ] . map ( ( file ) => file . replace ( / \\ / g, '/' ) ) ,
81
+ ) ,
82
+ expect . anything ( ) ,
83
+ )
84
+ } )
24
85
} )
0 commit comments