@@ -31,20 +31,16 @@ describe('fs', () => {
31
31
} ) ;
32
32
} ) ;
33
33
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 ( ) => {
35
35
await withSandbox ( async ( sandbox ) => {
36
36
const filePath = path . join ( sandbox . directoryPath , 'nonexistent' ) ;
37
37
38
38
await expect ( readFile ( filePath ) ) . rejects . toThrow (
39
39
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
+ } ) ,
48
44
} ) ,
49
45
) ;
50
46
} ) ;
@@ -64,21 +60,17 @@ describe('fs', () => {
64
60
} ) ;
65
61
} ) ;
66
62
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 ( ) => {
68
64
await withSandbox ( async ( sandbox ) => {
69
65
await promisifiedRimraf ( sandbox . directoryPath ) ;
70
66
const filePath = path . join ( sandbox . directoryPath , 'test' ) ;
71
67
72
68
await expect ( writeFile ( filePath , 'some content 😄' ) ) . rejects . toThrow (
73
69
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
+ } ) ,
82
74
} ) ,
83
75
) ;
84
76
} ) ;
@@ -97,20 +89,17 @@ describe('fs', () => {
97
89
} ) ;
98
90
} ) ;
99
91
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 ( ) => {
101
93
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' ) ;
105
95
when ( jest . spyOn ( actionUtils , 'readJsonObjectFile' ) )
106
96
. calledWith ( filePath )
107
97
. mockRejectedValue ( error ) ;
108
98
109
99
await expect ( readJsonObjectFile ( filePath ) ) . rejects . toThrow (
110
100
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 ,
114
103
} ) ,
115
104
) ;
116
105
} ) ;
@@ -126,20 +115,17 @@ describe('fs', () => {
126
115
expect ( await writeJsonFile ( filePath , { some : 'object' } ) ) . toBeUndefined ( ) ;
127
116
} ) ;
128
117
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 ( ) => {
130
119
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' ) ;
134
121
when ( jest . spyOn ( actionUtils , 'writeJsonFile' ) )
135
122
. calledWith ( filePath , { some : 'object' } )
136
123
. mockRejectedValue ( error ) ;
137
124
138
125
await expect ( writeJsonFile ( filePath , { some : 'object' } ) ) . rejects . toThrow (
139
126
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 ,
143
129
} ) ,
144
130
) ;
145
131
} ) ;
@@ -183,9 +169,23 @@ describe('fs', () => {
183
169
184
170
await expect ( fileExists ( entryPath ) ) . rejects . toThrow (
185
171
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 ,
189
189
} ) ,
190
190
) ;
191
191
} ) ;
@@ -237,18 +237,15 @@ describe('fs', () => {
237
237
238
238
it ( 're-throws any error that occurs, assigning it the same code, a wrapped message, and a new stack' , async ( ) => {
239
239
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' ) ;
243
241
when ( jest . spyOn ( fs . promises , 'mkdir' ) )
244
242
. calledWith ( directoryPath , { recursive : true } )
245
243
. mockRejectedValue ( error ) ;
246
244
247
245
await expect ( ensureDirectoryPathExists ( directoryPath ) ) . rejects . toThrow (
248
246
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 ,
252
249
} ) ,
253
250
) ;
254
251
} ) ;
@@ -273,18 +270,15 @@ describe('fs', () => {
273
270
274
271
it ( 're-throws any error that occurs, assigning it the same code, a wrapped message, and a new stack' , async ( ) => {
275
272
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' ) ;
279
274
when ( jest . spyOn ( fs . promises , 'rm' ) )
280
275
. calledWith ( filePath , { force : true } )
281
276
. mockRejectedValue ( error ) ;
282
277
283
278
await expect ( removeFile ( filePath ) ) . rejects . toThrow (
284
279
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 ,
288
282
} ) ,
289
283
) ;
290
284
} ) ;
0 commit comments