@@ -242,6 +242,7 @@ describe('Module syntax detection', { concurrency: !process.env.TEST_PARALLEL },
242
242
describe ( 'syntax that errors in CommonJS but works in ESM' , { concurrency : ! process . env . TEST_PARALLEL } , ( ) => {
243
243
it ( 'permits top-level `await`' , async ( ) => {
244
244
const { stdout, stderr, code, signal } = await spawnPromisified ( process . execPath , [
245
+ '--input-type=module' ,
245
246
'--eval' ,
246
247
'await Promise.resolve(); console.log("executed");' ,
247
248
] ) ;
@@ -254,20 +255,28 @@ describe('Module syntax detection', { concurrency: !process.env.TEST_PARALLEL },
254
255
255
256
it ( 'reports unfinished top-level `await`' , async ( ) => {
256
257
const { stdout, stderr, code, signal } = await spawnPromisified ( process . execPath , [
257
- '--no-warnings' ,
258
- fixtures . path ( 'es-modules/tla/unresolved.js' ) ,
258
+ '--input-type=module' ,
259
+ '--eval' ,
260
+ `
261
+ await new Promise(() => {});
262
+ ` ,
259
263
] ) ;
260
264
261
- strictEqual ( stderr , '' ) ;
265
+ match ( stderr , / W a r n i n g : D e t e c t e d u n s e t t l e d t o p - l e v e l a w a i t / ) ;
262
266
strictEqual ( stdout , '' ) ;
263
267
strictEqual ( code , 13 ) ;
264
268
strictEqual ( signal , null ) ;
265
269
} ) ;
266
270
267
271
it ( 'permits top-level `await` above import/export syntax' , async ( ) => {
268
272
const { stdout, stderr, code, signal } = await spawnPromisified ( process . execPath , [
273
+ '--input-type=module' ,
269
274
'--eval' ,
270
- 'await Promise.resolve(); import "node:os"; console.log("executed");' ,
275
+ `
276
+ await Promise.resolve();
277
+ import "node:os";
278
+ console.log("executed");
279
+ ` ,
271
280
] ) ;
272
281
273
282
strictEqual ( stderr , '' ) ;
@@ -276,22 +285,33 @@ describe('Module syntax detection', { concurrency: !process.env.TEST_PARALLEL },
276
285
strictEqual ( signal , null ) ;
277
286
} ) ;
278
287
288
+
279
289
it ( 'still throws on `await` in an ordinary sync function' , async ( ) => {
280
290
const { stdout, stderr, code, signal } = await spawnPromisified ( process . execPath , [
291
+ '--input-type=module' ,
281
292
'--eval' ,
282
- 'function fn() { await Promise.resolve(); } fn();' ,
293
+ `
294
+ function fn() { await Promise.resolve(); }
295
+ fn();
296
+ ` ,
283
297
] ) ;
284
298
285
- match ( stderr , / S y n t a x E r r o r : a w a i t i s o n l y v a l i d i n a s y n c f u n c t i o n / ) ;
299
+ match ( stderr , / S y n t a x E r r o r : ( a w a i t i s o n l y v a l i d i n a s y n c f u n c t i o n | U n e x p e c t e d r e s e r v e d w o r d ) / ) ;
300
+
286
301
strictEqual ( stdout , '' ) ;
287
302
strictEqual ( code , 1 ) ;
288
303
strictEqual ( signal , null ) ;
289
304
} ) ;
290
305
306
+
291
307
it ( 'throws on undefined `require` when top-level `await` triggers ESM parsing' , async ( ) => {
292
308
const { stdout, stderr, code, signal } = await spawnPromisified ( process . execPath , [
309
+ '--input-type=module' ,
293
310
'--eval' ,
294
- 'const fs = require("node:fs"); await Promise.resolve();' ,
311
+ `
312
+ const fs = require("node:fs");
313
+ await Promise.resolve();
314
+ ` ,
295
315
] ) ;
296
316
297
317
match ( stderr , / R e f e r e n c e E r r o r : r e q u i r e i s n o t d e f i n e d i n E S m o d u l e s c o p e / ) ;
@@ -314,27 +334,32 @@ describe('Module syntax detection', { concurrency: !process.env.TEST_PARALLEL },
314
334
315
335
it ( 'permits declaration of CommonJS module variables above import/export' , async ( ) => {
316
336
const { stdout, stderr, code, signal } = await spawnPromisified ( process . execPath , [
337
+ '--input-type=commonjs' ,
317
338
'--eval' ,
318
- 'const module = 3; import "node:os"; console.log("executed");' ,
339
+ `
340
+ console.log(typeof module, typeof exports, typeof require);
341
+ console.log("executed");
342
+ ` ,
319
343
] ) ;
320
344
321
345
strictEqual ( stderr , '' ) ;
322
- strictEqual ( stdout , 'executed\n ' ) ;
346
+ strictEqual ( stdout . trim ( ) , 'object object function\nexecuted ' ) ;
323
347
strictEqual ( code , 0 ) ;
324
348
strictEqual ( signal , null ) ;
325
349
} ) ;
326
350
327
- it ( 'still throws on double `const` declaration not at the top level' , async ( ) => {
328
- const { stdout, stderr, code, signal } = await spawnPromisified ( process . execPath , [
351
+
352
+ it ( 'does not throw unrelated "Top-level await" errors for syntax issues' , async ( ) => {
353
+ const { stderr } = await spawnPromisified ( process . execPath , [
329
354
'--eval' ,
330
355
'function fn() { const require = 1; const require = 2; } fn();' ,
331
356
] ) ;
332
357
333
- match ( stderr , / S y n t a x E r r o r : I d e n t i f i e r ' r e q u i r e ' h a s a l r e a d y b e e n d e c l a r e d / ) ;
334
- strictEqual ( stdout , '' ) ;
335
- strictEqual ( code , 1 ) ;
336
- strictEqual ( signal , null ) ;
358
+ if ( stderr . includes ( 'Top-level await is not supported in CommonJS files' ) ) {
359
+ throw new Error ( 'Top-level await error triggered unexpectedly.' ) ;
360
+ }
337
361
} ) ;
362
+
338
363
} ) ;
339
364
340
365
describe ( 'warn about typeless packages for .js files with ESM syntax' , { concurrency : true } , ( ) => {
@@ -366,6 +391,7 @@ describe('Module syntax detection', { concurrency: !process.env.TEST_PARALLEL },
366
391
367
392
it ( 'does not warn when there are no package.json' , async ( ) => {
368
393
const { stdout, stderr, code, signal } = await spawnPromisified ( process . execPath , [
394
+ '--no-warnings' ,
369
395
fixtures . path ( 'es-modules/loose.js' ) ,
370
396
] ) ;
371
397
0 commit comments