@@ -274,16 +274,52 @@ describe('exportJSON', function () {
274
274
expect ( result . docsWritten ) . to . equal ( 0 ) ;
275
275
expect ( result . aborted ) . to . be . true ;
276
276
277
- let resultText ;
278
277
try {
279
- resultText = await fs . promises . readFile ( resultPath , 'utf8' ) ;
278
+ await fs . promises . readFile ( resultPath , 'utf8' ) ;
279
+ expect . fail ( 'Expected file to not exist' ) ;
280
280
} catch ( err ) {
281
- console . log ( resultPath ) ;
282
- throw err ;
281
+ // noop
283
282
}
283
+ // close the stream so that afterEach hook can clear the tmpdir
284
+ // otherwise it will throw an error (for windows)
285
+ output . close ( ) ;
286
+ } ) ;
284
287
285
- const expectedText = '' ;
286
- expect ( resultText ) . to . deep . equal ( expectedText ) ;
288
+ it ( 'responds to abortSignal.aborted - with delayed abort' , async function ( ) {
289
+ const abortController = new AbortController ( ) ;
290
+
291
+ const resultPath = path . join ( tmpdir , 'test-abort.exported.ejson' ) ;
292
+
293
+ const importedText = await fs . promises . readFile (
294
+ fixtures . json . complex ,
295
+ 'utf8'
296
+ ) ;
297
+ const ejsonToInsert = EJSON . parse ( importedText ) ;
298
+ await dataService . insertMany ( testNS , ejsonToInsert ) ;
299
+
300
+ const output = fs . createWriteStream ( resultPath ) ;
301
+ const promise = exportJSONFromQuery ( {
302
+ dataService,
303
+ ns : testNS ,
304
+ output,
305
+ variant : 'default' ,
306
+ abortSignal : abortController . signal ,
307
+ } ) ;
308
+ abortController . abort ( ) ;
309
+
310
+ const result = await promise ;
311
+ const data = await fs . promises . readFile ( resultPath , 'utf8' ) ;
312
+
313
+ try {
314
+ JSON . parse ( data ) ;
315
+ expect . fail ( 'Expected file to not be valid JSON' ) ;
316
+ } catch ( err ) {
317
+ // With signal part of streams pipeline the file is created and if
318
+ // the signal is aborted the stream is destroyed and file is not
319
+ // writable anymore and as a result its not able to write trailing ] to the file.
320
+ }
321
+ expect ( result . aborted ) . to . be . true ;
322
+ expect ( result . docsWritten ) . to . equal ( 0 ) ;
287
323
} ) ;
288
324
289
325
it ( 'exports aggregations' , async function ( ) {
0 commit comments