@@ -10,6 +10,7 @@ const CONTENTS = {
10
10
'bat/baz/bar' : 'console.log("bat/baz/bar")' ,
11
11
} ;
12
12
13
+ const cwdCopy = process . cwd ( ) ;
13
14
const tmpDirs = new Set < tmp . DirResult > ( ) ;
14
15
afterEach ( ( ) => {
15
16
// stop watching the files and folders
@@ -18,6 +19,9 @@ afterEach(() => {
18
19
// clean up the temporary files and folders
19
20
tmpDirs . forEach ( t => t . removeCallback ( ) ) ;
20
21
tmpDirs . clear ( ) ;
22
+
23
+ // restore original cwd
24
+ process . chdir ( cwdCopy ) ;
21
25
} ) ;
22
26
23
27
function writeTSConfig ( dirName : string , config : Record < string , unknown > ) : void {
@@ -54,14 +58,24 @@ function setup(tsconfig: Record<string, unknown>, writeBar = true): string {
54
58
return tmpDir . name ;
55
59
}
56
60
57
- function parseFile ( filename : keyof typeof CONTENTS , tmpDir : string ) : void {
58
- parseAndGenerateServices ( CONTENTS . foo , {
61
+ function parseFile (
62
+ filename : keyof typeof CONTENTS ,
63
+ tmpDir : string ,
64
+ relative ?: boolean ,
65
+ ) : void {
66
+ parseAndGenerateServices ( CONTENTS [ filename ] , {
59
67
project : './tsconfig.json' ,
60
68
tsconfigRootDir : tmpDir ,
<
A935
tr class="diff-line-row">61
- filePath : path . join ( tmpDir , 'src' , `${ filename } .ts` ) ,
69
+ filePath : relative
70
+ ? path . join ( 'src' , `${ filename } .ts` )
71
+ : path . join ( tmpDir , 'src' , `${ filename } .ts` ) ,
62
72
} ) ;
63
73
}
64
74
75
+ function existsSync ( filename : keyof typeof CONTENTS , tmpDir = '' ) : boolean {
76
+ return fs . existsSync ( path . join ( tmpDir , 'src' , `${ filename } .ts` ) ) ;
77
+ }
78
+
65
79
function baseTests (
66
80
tsConfigExcludeBar : Record < string , unknown > ,
67
81
tsConfigIncludeAll : Record < string , unknown > ,
@@ -161,6 +175,27 @@ function baseTests(
161
175
expect ( ( ) => parseFile ( 'foo' , PROJECT_DIR ) ) . not . toThrow ( ) ;
162
176
expect ( ( ) => parseFile ( 'bar' , PROJECT_DIR ) ) . not . toThrow ( ) ;
163
177
} ) ;
178
+
179
+ it ( 'should work with relative paths' , ( ) => {
180
+ const PROJECT_DIR = setup ( tsConfigIncludeAll , false ) ;
181
+ process . chdir ( PROJECT_DIR ) ;
182
+
183
+ // parse once to: assert the config as correct, and to make sure the program is setup
184
+ expect ( ( ) => parseFile ( 'foo' , PROJECT_DIR , true ) ) . not . toThrow ( ) ;
185
+ // bar should throw because it doesn't exist yet
186
+ expect ( ( ) => parseFile ( 'bar' , PROJECT_DIR , true ) ) . toThrow ( ) ;
187
+
188
+ // write a new file and attempt to parse it
189
+ writeFile ( PROJECT_DIR , 'bar' ) ;
190
+
191
+ // make sure that file is correctly created
192
+ expect ( existsSync ( 'bar' ) ) . toEqual ( true ) ;
193
+ expect ( existsSync ( 'bar' , PROJECT_DIR ) ) . toEqual ( true ) ;
194
+
195
+ // both files should parse fine now
196
+ expect ( ( ) => parseFile ( 'foo' , PROJECT_DIR , true ) ) . not . toThrow ( ) ;
197
+ expect ( ( ) => parseFile ( 'bar' , PROJECT_DIR , true ) ) . not . toThrow ( ) ;
198
+ } ) ;
164
199
}
165
200
166
201
describe ( 'persistent parse' , ( ) => {
0 commit comments