@@ -11,11 +11,13 @@ const serverApiPath = path.resolve(serverPath, apiPath);
11
11
const serverModelPath = path . resolve ( serverPath , modelPath ) ;
12
12
const mainPath = path . resolve ( __dirname , '../../mocks' ) ;
13
13
14
- beforeEach ( async ( ) : Promise < void > => {
15
- await emptyFolder ( serverPath ) ;
16
- await ensureDir ( serverApiPath ) ;
17
- await ensureDir ( serverModelPath ) ;
18
- } ) ;
14
+ beforeEach (
15
+ async ( ) : Promise < void > => {
16
+ await emptyFolder ( serverPath ) ;
17
+ await ensureDir ( serverApiPath ) ;
18
+ await ensureDir ( serverModelPath ) ;
19
+ } ,
20
+ ) ;
19
21
20
22
const apiFileContent = `import { Data } from '../model/ok';
21
23
@@ -42,44 +44,47 @@ export interface Data {
42
44
*/
43
45
test ( 'Unexisting server path will throw exception' , async ( ) : Promise < void > => {
44
46
const wrongPath = '/wrong/path/to/server' ;
45
- await waitForExpect ( async ( ) : Promise < void > => {
46
- expect ( initWatchers ( wrongPath , wrongPath ) ) . rejects . toBeInstanceOf ( Error ) ;
47
- } ) ;
47
+ await waitForExpect (
48
+ async ( ) : Promise < void > => {
49
+ expect ( initWatchers ( wrongPath , wrongPath ) ) . rejects . toBeInstanceOf ( Error ) ;
50
+ } ,
51
+ ) ;
48
52
} ) ;
49
53
50
54
/**
51
55
* Should work with existing server path and process files when added
52
56
*/
53
57
test ( 'Basic watchers flow' , async ( ) : Promise < void > => {
54
58
const watchers = await initWatchers ( serverPath , mainPath ) ;
55
- const handler = jest . fn ( ( ) : void => {
59
+ const handler : (
60
+ eventName : 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir' ,
61
+ eventPath : string ,
62
+ ) => void = jest . fn ( ( ) : void => {
56
63
// Empty function to check event handler
57
64
} ) ;
58
65
watchers . forEach ( ( watcher ) : void => {
66
+ watcher . on ( 'all' , ( eventName , eventPath ) : void => {
67
+ handler ( eventName , eventPath ) ;
68
+ } ) ;
59
69
watcher . on ( 'all' , handler ) ;
60
70
} ) ;
61
71
const fileName = 'ok.ts' ;
62
72
const serverApiFilePath = path . resolve ( serverApiPath , fileName ) ;
63
73
const serverModelFilePath = path . resolve ( serverModelPath , fileName ) ;
64
74
await writeFile ( serverApiFilePath , apiFileContent ) ;
65
75
await writeFile ( serverModelFilePath , modeFileContent ) ;
66
- await waitForExpect ( async ( ) : Promise < void > => {
67
- expect ( await exists ( serverApiFilePath ) ) . toBe ( true ) ;
68
- expect ( await exists ( serverModelFilePath ) ) . toBe ( true ) ;
69
- expect ( handler ) . toHaveBeenCalledWith (
70
- 'add' ,
71
- serverApiFilePath ,
72
- ) ;
73
- expect ( handler ) . toHaveBeenCalledWith (
74
- 'add' ,
75
- serverModelFilePath ,
76
- ) ;
77
- } ) ;
76
+ await waitForExpect (
77
+ async ( ) : Promise < void > => {
78
+ expect ( await exists ( serverApiFilePath ) ) . toBe ( true ) ;
79
+ expect ( await exists ( serverModelFilePath ) ) . toBe ( true ) ;
80
+ expect ( handler ) . toHaveBeenCalledWith ( 'add' , serverApiFilePath ) ;
81
+ expect ( handler ) . toHaveBeenCalledWith ( 'add' , serverModelFilePath ) ;
82
+ } ,
83
+ ) ;
78
84
79
- await Promise . all ( watchers . map ( w => w . close ( ) ) ) ;
85
+ await Promise . all ( watchers . map ( ( w ) => w . close ( ) ) ) ;
80
86
} ) ;
81
87
82
-
83
88
/**
84
89
* When disabling jest, watchers should process files when added
85
90
*/
@@ -91,19 +96,28 @@ test('Complete watchers flow', async (): Promise<void> => {
91
96
const serverModelFilePath = path . resolve ( serverModelPath , fileName ) ;
92
97
await writeFile ( serverApiFilePath , apiFileContent ) ;
93
98
await writeFile ( serverModelFilePath , modeFileContent ) ;
94
- await waitForExpect ( async ( ) : Promise < void > => {
95
- expect ( await exists ( serverApiFilePath ) ) . toBe ( true ) ;
96
- expect ( await exists ( serverModelFilePath ) ) . toBe ( true ) ;
97
- } ) ;
99
+ await waitForExpect (
100
+ async ( ) : Promise < void > => {
101
+ expect ( await exists ( serverApiFilePath ) ) . toBe ( true ) ;
102
+ expect ( await exists ( serverModelFilePath ) ) . toBe ( true ) ;
103
+ } ,
104
+ ) ;
98
105
99
106
const proxyIndexPath = path . resolve ( mainPath , proxyPath , 'api' , 'index.ts' ) ;
100
- const modelResultingPath = path . resolve ( mainPath , proxyPath , 'model' , fileName ) ;
107
+ const modelResultingPath = path . resolve (
108
+ mainPath ,
109
+ proxyPath ,
110
+ 'model' ,
111
+ fileName ,
112
+ ) ;
101
113
102
- await waitForExpect ( async ( ) : Promise < void > => {
103
- expect ( await exists ( proxyIndexPath ) ) . toBe ( true ) ;
104
- expect ( await exists ( modelResultingPath ) ) . toBe ( true ) ;
105
- } ) ;
114
+ await waitForExpect (
115
+ async ( ) : Promise < void > => {
116
+ expect ( await exists ( proxyIndexPath ) ) . toBe ( true ) ;
117
+ expect ( await exists ( modelResultingPath ) ) . toBe ( true ) ;
118
+ } ,
119
+ ) ;
106
120
107
121
setForceDisableJestDetection ( false ) ;
108
- await Promise . all ( watchers . map ( w => w . close ( ) ) ) ;
122
+ await Promise . all ( watchers . map ( ( w ) => w . close ( ) ) ) ;
109
123
} , 10000 ) ;
0 commit comments