@@ -192,6 +192,162 @@ describe('Loader hooks', { concurrency: true }, () => {
192192 assert . strictEqual ( signal , null ) ;
193193 } ) ;
194194
195+ describe ( 'should handle a throwing top-level body' , ( ) => {
196+ it ( 'should handle regular Error object' , async ( ) => {
197+ const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
198+ '--no-warnings' ,
199+ '--experimental-loader' ,
200+ 'data:text/javascript,throw new Error("error message")' ,
201+ fixtures . path ( 'empty.js' ) ,
202+ ] ) ;
203+
204+ assert . match ( stderr , / E r r o r : e r r o r m e s s a g e \r ? \n / ) ;
205+ assert . strictEqual ( stdout , '' ) ;
206+ assert . strictEqual ( code , 1 ) ;
207+ assert . strictEqual ( signal , null ) ;
208+ } ) ;
209+
210+ it ( 'should handle null' , async ( ) => {
211+ const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
212+ '--no-warnings' ,
213+ '--experimental-loader' ,
214+ 'data:text/javascript,throw null' ,
215+ fixtures . path ( 'empty.js' ) ,
216+ ] ) ;
217+
218+ assert . match ( stderr , / \n n u l l \r ? \n / ) ;
219+ assert . strictEqual ( stdout , '' ) ;
220+ assert . strictEqual ( code , 1 ) ;
221+ assert . strictEqual ( signal , null ) ;
222+ } ) ;
223+
224+ it ( 'should handle undefined' , async ( ) => {
225+ const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
226+ '--no-warnings' ,
227+ '--experimental-loader' ,
228+ 'data:text/javascript,throw undefined' ,
229+ fixtures . path ( 'empty.js' ) ,
230+ ] ) ;
231+
232+ assert . match ( stderr , / \n u n d e f i n e d \r ? \n / ) ;
233+ assert . strictEqual ( stdout , '' ) ;
234+ assert . strictEqual ( code , 1 ) ;
235+ assert . strictEqual ( signal , null ) ;
236+ } ) ;
237+
238+ it ( 'should handle boolean' , async ( ) => {
239+ const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
240+ '--no-warnings' ,
241+ '--experimental-loader' ,
242+ 'data:text/javascript,throw true' ,
243+ fixtures . path ( 'empty.js' ) ,
244+ ] ) ;
245+
246+ assert . match ( stderr , / \n t r u e \r ? \n / ) ;
247+ assert . strictEqual ( stdout , '' ) ;
248+ assert . strictEqual ( code , 1 ) ;
249+ assert . strictEqual ( signal , null ) ;
250+ } ) ;
251+
252+ it ( 'should handle empty plain object' , async ( ) => {
253+ const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
254+ '--no-warnings' ,
255+ '--experimental-loader' ,
256+ 'data:text/javascript,throw {}' ,
257+ fixtures . path ( 'empty.js' ) ,
258+ ] ) ;
259+
260+ assert . match ( stderr , / \n \{ \} \r ? \n / ) ;
261+ assert . strictEqual ( stdout , '' ) ;
262+ assert . strictEqual ( code , 1 ) ;
263+ assert . strictEqual ( signal , null ) ;
264+ } ) ;
265+
266+ it ( 'should handle plain object' , async ( ) => {
267+ const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
268+ '--no-warnings' ,
269+ '--experimental-loader' ,
270+ 'data:text/javascript,throw {fn(){},symbol:Symbol("symbol"),u:undefined}' ,
271+ fixtures . path ( 'empty.js' ) ,
272+ ] ) ;
273+
274+ 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 / ) ;
275+ assert . strictEqual ( stdout , '' ) ;
276+ assert . strictEqual ( code , 1 ) ;
277+ assert . strictEqual ( signal , null ) ;
278+ } ) ;
279+
280+ it ( 'should handle number' , async ( ) => {
281+ const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
282+ '--no-warnings' ,
283+ '--experimental-loader' ,
284+ 'data:text/javascript,throw 1' ,
285+ fixtures . path ( 'empty.js' ) ,
286+ ] ) ;
287+
288+ assert . match ( stderr , / \n 1 \r ? \n / ) ;
289+ assert . strictEqual ( stdout , '' ) ;
290+ assert . strictEqual ( code , 1 ) ;
291+ assert . strictEqual ( signal , null ) ;
292+ } ) ;
293+
294+ it ( 'should handle bigint' , async ( ) => {
295+ const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
296+ '--no-warnings' ,
297+ '--experimental-loader' ,
298+ 'data:text/javascript,throw 1n' ,
299+ fixtures . path ( 'empty.js' ) ,
300+ ] ) ;
301+
302+ assert . match ( stderr , / \n 1 \r ? \n / ) ;
303+ assert . strictEqual ( stdout , '' ) ;
304+ assert . strictEqual ( code , 1 ) ;
305+ assert . strictEqual ( signal , null ) ;
306+ } ) ;
307+
308+ it ( 'should handle string' , async ( ) => {
309+ const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
310+ '--no-warnings' ,
311+ '--experimental-loader' ,
312+ 'data:text/javascript,throw "literal string"' ,
313+ fixtures . path ( 'empty.js' ) ,
314+ ] ) ;
315+
316+ assert . match ( stderr , / \n l i t e r a l s t r i n g \r ? \n / ) ;
317+ assert . strictEqual ( stdout , '' ) ;
318+ assert . strictEqual ( code , 1 ) ;
319+ assert . strictEqual ( signal , null ) ;
320+ } ) ;
321+
322+ it ( 'should handle symbol' , async ( ) => {
323+ const { code, signal, stdout } = await spawnPromisified ( execPath , [
324+ '--no-warnings' ,
325+ '--experimental-loader' ,
326+ 'data:text/javascript,throw Symbol("symbol descriptor")' ,
327+ fixtures . path ( 'empty.js' ) ,
328+ ] ) ;
329+
330+ // Throwing a symbol doesn't produce any output
331+ assert . strictEqual ( stdout , '' ) ;
332+ assert . strictEqual ( code , 1 ) ;
333+ assert . strictEqual ( signal , null ) ;
334+ } ) ;
335+
336+ it ( 'should handle function' , async ( ) => {
337+ const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
338+ '--no-warnings' ,
339+ '--experimental-loader' ,
340+ 'data:text/javascript,throw function fnName(){}' ,
341+ fixtures . path ( 'empty.js' ) ,
342+ ] ) ;
343+
344+ assert . match ( stderr , / \n \[ F u n c t i o n : f n N a m e \] \r ? \n / ) ;
345+ assert . strictEqual ( stdout , '' ) ;
346+ assert . strictEqual ( code , 1 ) ;
347+ assert . strictEqual ( signal , null ) ;
348+ } ) ;
349+ } ) ;
350+
195351 it ( 'should be fine to call `process.removeAllListeners("beforeExit")` from the main thread' , async ( ) => {
196352 const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
197353 '--no-warnings' ,
0 commit comments