1
1
import * as path from 'path' ;
2
- import { fs , wait } from '@modern-js/utils' ;
2
+ import { wait } from '@modern-js/utils' ;
3
3
import { createFileWatcher } from '../src/utils' ;
4
4
import { IAppContext } from '../src' ;
5
5
6
6
const mockAppDirectory = path . join ( __dirname , './fixtures/index-test' ) ;
7
- const mockSrcDirectory = path . join ( mockAppDirectory , './src' ) ;
8
7
9
8
describe ( 'createFileWatcher' , ( ) => {
10
9
const argv = process . argv . slice ( 0 ) ;
@@ -15,53 +14,46 @@ describe('createFileWatcher', () => {
15
14
} ) ;
16
15
17
16
afterAll ( ( ) => {
18
- const file = path . join ( mockSrcDirectory , './index.ts' ) ;
19
- if ( fs . existsSync ( file ) ) {
20
- fs . unlinkSync ( file ) ;
21
- }
22
17
process . argv = argv ;
23
18
} ) ;
24
19
25
20
it ( 'will trigger add event' , async ( ) => {
26
21
let triggeredType = '' ;
27
22
let triggeredFile = '' ;
23
+
28
24
const appContext : IAppContext = {
29
25
appDirectory : '' ,
30
26
distDirectory : '' ,
31
27
packageName : '' ,
32
28
serverConfigFile : '' ,
33
29
configFile : '' ,
34
30
} as IAppContext ;
31
+
35
32
const hooksRunner = {
36
- watchFiles : async ( ) => [ mockSrcDirectory ] ,
33
+ watchFiles : async ( ) => [ mockAppDirectory ] ,
37
34
fileChange : jest . fn ( ( { filename, eventType } ) => {
38
35
triggeredType = eventType ;
39
36
triggeredFile = filename ;
40
37
} ) ,
41
38
} ;
42
39
43
- if ( await fs . pathExists ( mockSrcDirectory ) ) {
44
- await fs . remove ( mockSrcDirectory ) ;
45
- }
46
-
47
40
const watcher = await createFileWatcher (
48
41
appContext as any ,
49
42
hooksRunner as any ,
50
43
) ;
51
44
await wait ( 100 ) ;
52
45
53
- const file = path . join ( mockSrcDirectory , './index.ts' ) ;
54
- await fs . outputFile ( file , '' ) ;
55
- await wait ( 500 ) ;
56
- // expect(hooksRunner.fileChange).toBeCalledTimes(1 );
57
- // expect(triggeredType).toBe('add' );
46
+ // Add a file
47
+ const file = path . join ( mockAppDirectory , './package.json ' ) ;
48
+ watcher ?. emit ( 'add' , file ) ;
49
+ expect ( triggeredType ) . toBe ( 'add' ) ;
50
+ expect ( hooksRunner . fileChange ) . toBeCalledTimes ( 1 ) ;
58
51
expect ( file . includes ( triggeredFile ) ) . toBeTruthy ( ) ;
59
52
60
- await wait ( 100 ) ;
61
- await fs . remove ( file ) ;
62
- await wait ( 2000 ) ;
63
- expect ( hooksRunner . fileChange ) . toBeCalledTimes ( 2 ) ;
53
+ // Remove a file
54
+ watcher ?. emit ( 'unlink' , file ) ;
64
55
expect ( triggeredType ) . toBe ( 'unlink' ) ;
56
+ expect ( hooksRunner . fileChange ) . toBeCalledTimes ( 2 ) ;
65
57
expect ( file . includes ( triggeredFile ) ) . toBeTruthy ( ) ;
66
58
67
59
watcher ?. close ( ) ;
0 commit comments