@@ -31,20 +31,16 @@ describe('fs', () => {
3131 } ) ;
3232 } ) ;
3333
34- it ( 're-throws any error that occurs, assigning it the same code, a wrapped message, and a new stack ' , async ( ) => {
34+ it ( 're-throws any error that occurs as a new error that points to the original ' , async ( ) => {
3535 await withSandbox ( async ( sandbox ) => {
3636 const filePath = path . join ( sandbox . directoryPath , 'nonexistent' ) ;
3737
3838 await expect ( readFile ( filePath ) ) . rejects . toThrow (
3939 expect . objectContaining ( {
40- message : expect . stringMatching (
41- new RegExp (
42- `^Could not read file '${ filePath } ': ENOENT: no such file or directory, open '${ filePath } '` ,
43- 'u' ,
44- ) ,
45- ) ,
46- code : 'ENOENT' ,
47- stack : expect . anything ( ) ,
40+ message : `Could not read file '${ filePath } '` ,
41+ cause : expect . objectContaining ( {
42+ message : `ENOENT: no such file or directory, open '${ filePath } '` ,
43+ } ) ,
4844 } ) ,
4945 ) ;
5046 } ) ;
@@ -64,21 +60,17 @@ describe('fs', () => {
6460 } ) ;
6561 } ) ;
6662
67- it ( 're-throws any error that occurs, assigning it the same code, a wrapped message, and a new stack ' , async ( ) => {
63+ it ( 're-throws any error that occurs as a new error that points to the original ' , async ( ) => {
6864 await withSandbox ( async ( sandbox ) => {
6965 await promisifiedRimraf ( sandbox . directoryPath ) ;
7066 const filePath = path . join ( sandbox . directoryPath , 'test' ) ;
7167
7268 await expect ( writeFile ( filePath , 'some content 😄' ) ) . rejects . toThrow (
7369 expect . objectContaining ( {
74- message : expect . stringMatching (
75- new RegExp (
76- `^Could not write file '${ filePath } ': ENOENT: no such file or directory, open '${ filePath } '` ,
77- 'u' ,
78- ) ,
79- ) ,
80- code : 'ENOENT' ,
81- stack : expect . anything ( ) ,
70+ message : `Could not write file '${ filePath } '` ,
71+ cause : expect . objectContaining ( {
72+ message : `ENOENT: no such file or directory, open '${ filePath } '` ,
73+ } ) ,
8274 } ) ,
8375 ) ;
8476 } ) ;
@@ -97,20 +89,17 @@ describe('fs', () => {
9789 } ) ;
9890 } ) ;
9991
100- it ( 're-throws any error that occurs, assigning it the same code, a wrapped message, and a new stack ' , async ( ) => {
92+ it ( 're-throws any error that occurs as a new error that points to the original ' , async ( ) => {
10193 const filePath = '/some/file' ;
102- const error : any = new Error ( 'oops' ) ;
103- error . code = 'ESOMETHING' ;
104- error . stack = 'some stack' ;
94+ const error = new Error ( 'oops' ) ;
10595 when ( jest . spyOn ( actionUtils , 'readJsonObjectFile' ) )
10696 . calledWith ( filePath )
10797 . mockRejectedValue ( error ) ;
10898
10999 await expect ( readJsonObjectFile ( filePath ) ) . rejects . toThrow (
110100 expect . objectContaining ( {
111- message : `Could not read JSON file '${ filePath } ': oops` ,
112- code : 'ESOMETHING' ,
113- stack : 'some stack' ,
101+ message : `Could not read JSON file '${ filePath } '` ,
102+ cause : error ,
114103 } ) ,
115104 ) ;
116105 } ) ;
@@ -126,20 +115,17 @@ describe('fs', () => {
126115 expect ( await writeJsonFile ( filePath , { some : 'object' } ) ) . toBeUndefined ( ) ;
127116 } ) ;
128117
129- it ( 're-throws any error that occurs, assigning it the same code, a wrapped message, and a new stack ' , async ( ) => {
118+ it ( 're-throws any error that occurs as a new error that points to the original ' , async ( ) => {
130119 const filePath = '/some/file' ;
131- const error : any = new Error ( 'oops' ) ;
132- error . code = 'ESOMETHING' ;
133- error . stack = 'some stack' ;
120+ const error = new Error ( 'oops' ) ;
134121 when ( jest . spyOn ( actionUtils , 'writeJsonFile' ) )
135122 . calledWith ( filePath , { some : 'object' } )
136123 . mockRejectedValue ( error ) ;
137124
138125 await expect ( writeJsonFile ( filePath , { some : 'object' } ) ) . rejects . toThrow (
139126 expect . objectContaining ( {
140- message : `Could not write JSON file '${ filePath } ': oops` ,
141- code : 'ESOMETHING' ,
142- stack : 'some stack' ,
127+ message : `Could not write JSON file '${ filePath } '` ,
128+ cause : error ,
143129 } ) ,
144130 ) ;
145131 } ) ;
@@ -183,9 +169,23 @@ describe('fs', () => {
183169
184170 await expect ( fileExists ( entryPath ) ) . rejects . toThrow (
185171 expect . objectContaining ( {
186- message : `Could not determine if file exists '${ entryPath } ': oops` ,
187- code : 'ESOMETHING' ,
188- stack : 'some stack' ,
172+ message : `Could not determine if file exists '${ entryPath } '` ,
173+ cause : error ,
174+ } ) ,
175+ ) ;
176+ } ) ;
177+
178+ it ( 're-throws any error that occurs as a new error that points to the original' , async ( ) => {
179+ const entryPath = '/some/file' ;
180+ const error = new Error ( 'oops' ) ;
181+ when ( jest . spyOn ( fs . promises , 'stat' ) )
182+ . calledWith ( entryPath )
183+ . mockRejectedValue ( error ) ;
184+
185+ await expect ( fileExists ( entryPath ) ) . rejects . toThrow (
186+ expect . objectContaining ( {
187+ message : `Could not determine if file exists '${ entryPath } '` ,
188+ cause : error ,
189189 } ) ,
190190 ) ;
191191 } ) ;
@@ -237,18 +237,15 @@ describe('fs', () => {
237237
238238 it ( 're-throws any error that occurs, assigning it the same code, a wrapped message, and a new stack' , async ( ) => {
239239 const directoryPath = '/some/directory' ;
240- const error : any = new Error ( 'oops' ) ;
241- error . code = 'ESOMETHING' ;
242- error . stack = 'some stack' ;
240+ const error = new Error ( 'oops' ) ;
243241 when ( jest . spyOn ( fs . promises , 'mkdir' ) )
244242 . calledWith ( directoryPath , { recursive : true } )
245243 . mockRejectedValue ( error ) ;
246244
247245 await expect ( ensureDirectoryPathExists ( directoryPath ) ) . rejects . toThrow (
248246 expect . objectContaining ( {
249- message : `Could not create directory path '${ directoryPath } ': oops` ,
250- code : 'ESOMETHING' ,
251- stack : 'some stack' ,
247+ message : `Could not create directory path '${ directoryPath } '` ,
248+ cause : error ,
252249 } ) ,
253250 ) ;
254251 } ) ;
@@ -273,18 +270,15 @@ describe('fs', () => {
273270
274271 it ( 're-throws any error that occurs, assigning it the same code, a wrapped message, and a new stack' , async ( ) => {
275272 const filePath = '/some/file' ;
276- const error : any = new Error ( 'oops' ) ;
277- error . code = 'ESOMETHING' ;
278- error . stack = 'some stack' ;
273+ const error = new Error ( 'oops' ) ;
279274 when ( jest . spyOn ( fs . promises , 'rm' ) )
280275 . calledWith ( filePath , { force : true } )
281276 . mockRejectedValue ( error ) ;
282277
283278 await expect ( removeFile ( filePath ) ) . rejects . toThrow (
284279 expect . objectContaining ( {
285- message : `Could not remove file '${ filePath } ': oops` ,
286- code : 'ESOMETHING' ,
287- stack : 'some stack' ,
280+ message : `Could not remove file '${ filePath } '` ,
281+ cause : error ,
288282 } ) ,
289283 ) ;
290284 } ) ;
0 commit comments