@@ -244,6 +244,162 @@ describe('Loader hooks', { concurrency: true }, () => {
244
244
assert . strictEqual ( signal , null ) ;
245
245
} ) ;
246
246
247
+ describe ( 'should handle a throwing top-level body' , ( ) => {
248
+ it ( 'should handle regular Error object' , async ( ) => {
249
+ const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
250
+ '--no-warnings' ,
251
+ '--experimental-loader' ,
252
+ 'data:text/javascript,throw new Error("error message")' ,
253
+ fixtures . path ( 'empty.js' ) ,
254
+ ] ) ;
255
+
256
+ assert . match ( stderr , / E r r o r : e r r o r m e s s a g e \r ? \n / ) ;
257
+ assert . strictEqual ( stdout , '' ) ;
258
+ assert . strictEqual ( code , 1 ) ;
259
+ assert . strictEqual ( signal , null ) ;
260
+ } ) ;
261
+
262
+ it ( 'should handle null' , async ( ) => {
263
+ const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
264
+ '--no-warnings' ,
265
+ '--experimental-loader' ,
266
+ 'data:text/javascript,throw null' ,
267
+ fixtures . path ( 'empty.js' ) ,
268
+ ] ) ;
269
+
270
+ assert . match ( stderr , / \n n u l l \r ? \n / ) ;
271
+ assert . strictEqual ( stdout , '' ) ;
272
+ assert . strictEqual ( code , 1 ) ;
273
+ assert . strictEqual ( signal , null ) ;
274
+ } ) ;
275
+
276
+ it ( 'should handle undefined' , async ( ) => {
277
+ const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
278
+ '--no-warnings' ,
279
+ '--experimental-loader' ,
280
+ 'data:text/javascript,throw undefined' ,
281
+ fixtures . path ( 'empty.js' ) ,
282
+ ] ) ;
283
+
284
+ assert . match ( stderr , / \n u n d e f i n e d \r ? \n / ) ;
285
+ assert . strictEqual ( stdout , '' ) ;
286
+ assert . strictEqual ( code , 1 ) ;
287
+ assert . strictEqual ( signal , null ) ;
288
+ } ) ;
289
+
290
+ it ( 'should handle boolean' , async ( ) => {
291
+ const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
292
+ '--no-warnings' ,
293
+ '--experimental-loader' ,
294
+ 'data:text/javascript,throw true' ,
295
+ fixtures . path ( 'empty.js' ) ,
296
+ ] ) ;
297
+
298
+ assert . match ( stderr , / \n t r u e \r ? \n / ) ;
299
+ assert . strictEqual ( stdout , '' ) ;
300
+ assert . strictEqual ( code , 1 ) ;
301
+ assert . strictEqual ( signal , null ) ;
302
+ } ) ;
303
+
304
+ it ( 'should handle empty plain object' , async ( ) => {
305
+ const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
306
+ '--no-warnings' ,
307
+ '--experimental-loader' ,
308
+ 'data:text/javascript,throw {}' ,
309
+ fixtures . path ( 'empty.js' ) ,
310
+ ] ) ;
311
+
312
+ assert . match ( stderr , / \n \{ \} \r ? \n / ) ;
313
+ assert . strictEqual ( stdout , '' ) ;
314
+ assert . strictEqual ( code , 1 ) ;
315
+ assert . strictEqual ( signal , null ) ;
316
+ } ) ;
317
+
318
+ it ( 'should handle plain object' , async ( ) => {
319
+ const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
320
+ '--no-warnings' ,
321
+ '--experimental-loader' ,
322
+ 'data:text/javascript,throw {fn(){},symbol:Symbol("symbol"),u:undefined}' ,
323
+ fixtures . path ( 'empty.js' ) ,
324
+ ] ) ;
325
+
326
+ assert . match ( stderr , / \n \{ f n : \[ F u n c t i o n : f n \] , s y m b o l : S y m b o l \( s y m b o l \) , u : u n d e f i n e d \} \r ? \n / ) ;
327
+ assert . strictEqual ( stdout , '' ) ;
328
+ assert . strictEqual ( code , 1 ) ;
329
+ assert . strictEqual ( signal , null ) ;
330
+ } ) ;
331
+
332
+ it ( 'should handle number' , async ( ) => {
333
+ const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
334
+ '--no-warnings' ,
335
+ '--experimental-loader' ,
336
+ 'data:text/javascript,throw 1' ,
337
+ fixtures . path ( 'empty.js' ) ,
338
+ ] ) ;
339
+
340
+ assert . match ( stderr , / \n 1 \r ? \n / ) ;
341
+ assert . strictEqual ( stdout , '' ) ;
342
+ assert . strictEqual ( code , 1 ) ;
343
+ assert . strictEqual ( signal , null ) ;
344
+ } ) ;
345
+
346
+ it ( 'should handle bigint' , async ( ) => {
347
+ const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
348
+ '--no-warnings' ,
349
+ '--experimental-loader' ,
350
+ 'data:text/javascript,throw 1n' ,
351
+ fixtures . path ( 'empty.js' ) ,
352
+ ] ) ;
353
+
354
+ assert . match ( stderr , / \n 1 \r ? \n / ) ;
355
+ assert . strictEqual ( stdout , '' ) ;
356
+ assert . strictEqual ( code , 1 ) ;
357
+ assert . strictEqual ( signal , null ) ;
358
+ } ) ;
359
+
360
+ it ( 'should handle string' , async ( ) => {
361
+ const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
362
+ '--no-warnings' ,
363
+ '--experimental-loader' ,
364
+ 'data:text/javascript,throw "literal string"' ,
365
+ fixtures . path ( 'empty.js' ) ,
366
+ ] ) ;
367
+
368
+ assert . match ( stderr , / \n l i t e r a l s t r i n g \r ? \n / ) ;
369
+ assert . strictEqual ( stdout , '' ) ;
370
+ assert . strictEqual ( code , 1 ) ;
371
+ assert . strictEqual ( signal , null ) ;
372
+ } ) ;
373
+
374
+ it ( 'should handle symbol' , async ( ) => {
375
+ const { code, signal, stdout } = await spawnPromisified ( execPath , [
376
+ '--no-warnings' ,
377
+ '--experimental-loader' ,
378
+ 'data:text/javascript,throw Symbol("symbol descriptor")' ,
379
+ fixtures . path ( 'empty.js' ) ,
380
+ ] ) ;
381
+
382
+ // Throwing a symbol doesn't produce any output
383
+ assert . strictEqual ( stdout , '' ) ;
384
+ assert . strictEqual ( code , 1 ) ;
385
+ assert . strictEqual ( signal , null ) ;
386
+ } ) ;
387
+
388
+ it ( 'should handle function' , async ( ) => {
389
+ const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
390
+ '--no-warnings' ,
391
+ '--experimental-loader' ,
392
+ 'data:text/javascript,throw function fnName(){}' ,
393
+ fixtures . path ( 'empty.js' ) ,
394
+ ] ) ;
395
+
396
+ assert . match ( stderr , / \n \[ F u n c t i o n : f n N a m e \] \r ? \n / ) ;
397
+ assert . strictEqual ( stdout , '' ) ;
398
+ assert . strictEqual ( code , 1 ) ;
399
+ assert . strictEqual ( signal , null ) ;
400
+ } ) ;
401
+ } ) ;
402
+
247
403
it ( 'should be fine to call `process.removeAllListeners("beforeExit")` from the main thread' , async ( ) => {
248
404
const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
249
405
'--no-warnings' ,
0 commit comments