|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | | -constcommon=require('../common'); |
4 | | -constfixtures=require('../common/fixtures'); |
5 | | -const{ spawn }=require('child_process'); |
6 | | -constassert=require('assert'); |
7 | | -constpath=require('path'); |
| 3 | +const{ spawnPromisified }=require('../common'); |
| 4 | +constfixtures=require('../common/fixtures.js'); |
| 5 | +constassert=require('node:assert'); |
| 6 | +constpath=require('node:path'); |
| 7 | +const{ execPath }=require('node:process'); |
| 8 | +const{ describe, it }=require('node:test'); |
| 9 | + |
8 | 10 |
|
9 | 11 | constrequiringCjsAsEsm=path.resolve(fixtures.path('/es-modules/cjs-esm.js')); |
10 | 12 | constrequiringEsm=path.resolve(fixtures.path('/es-modules/cjs-esm-esm.js')); |
11 | 13 | constpjson=path.resolve( |
12 | 14 | fixtures.path('/es-modules/package-type-module/package.json') |
13 | 15 | ); |
14 | 16 |
|
15 | | -{ |
16 | | -constrequired=path.resolve( |
17 | | -fixtures.path('/es-modules/package-type-module/cjs.js') |
18 | | -); |
19 | | -constbasename='cjs.js'; |
20 | | -constchild=spawn(process.execPath,[requiringCjsAsEsm]); |
21 | | -letstderr=''; |
22 | | -child.stderr.setEncoding('utf8'); |
23 | | -child.stderr.on('data',(data)=>{ |
24 | | -stderr+=data; |
25 | | -}); |
26 | | -child.on('close',common.mustCall((code,signal)=>{ |
| 17 | + |
| 18 | +describe('CJS ↔︎ ESM interop warnings',{concurrency: true},()=>{ |
| 19 | + |
| 20 | +it(async()=>{ |
| 21 | +constrequired=path.resolve( |
| 22 | +fixtures.path('/es-modules/package-type-module/cjs.js') |
| 23 | +); |
| 24 | +constbasename='cjs.js'; |
| 25 | +const{ code, signal, stderr }=awaitspawnPromisified(execPath,[requiringCjsAsEsm]); |
| 26 | + |
| 27 | +assert.ok( |
| 28 | +stderr.replaceAll('\r','').includes( |
| 29 | +`Error [ERR_REQUIRE_ESM]: require() of ES Module ${required} from ${requiringCjsAsEsm} not supported.\n` |
| 30 | +) |
| 31 | +); |
| 32 | +assert.ok( |
| 33 | +stderr.replaceAll('\r','').includes( |
| 34 | +`Instead rename ${basename} to end in .cjs, change the requiring `+ |
| 35 | +'code to use dynamic import() which is available in all CommonJS '+ |
| 36 | +`modules, or change "type": "module" to "type": "commonjs" in ${pjson} to `+ |
| 37 | +'treat all .js files as CommonJS (using .mjs for all ES modules '+ |
| 38 | +'instead).\n' |
| 39 | +) |
| 40 | +); |
| 41 | + |
27 | 42 | assert.strictEqual(code,1); |
28 | 43 | assert.strictEqual(signal,null); |
| 44 | +}); |
29 | 45 |
|
30 | | -assert.ok(stderr.replaceAll('\r','').includes( |
31 | | -`Error [ERR_REQUIRE_ESM]: require() of ES Module ${required} from ${ |
32 | | -requiringCjsAsEsm} not supported.\n`)); |
33 | | -assert.ok(stderr.replaceAll('\r','').includes( |
34 | | -`Instead rename ${basename} to end in .cjs, change the requiring `+ |
35 | | -'code to use dynamic import() which is available in all CommonJS '+ |
36 | | -`modules, or change "type": "module" to "type": "commonjs" in ${pjson} to `+ |
37 | | -'treat all .js files as CommonJS (using .mjs for all ES modules '+ |
38 | | -'instead).\n')); |
39 | | -})); |
40 | | -} |
| 46 | +it(async()=>{ |
| 47 | +constrequired=path.resolve( |
| 48 | +fixtures.path('/es-modules/package-type-module/esm.js') |
| 49 | +); |
| 50 | +constbasename='esm.js'; |
| 51 | +const{ code, signal, stderr }=awaitspawnPromisified(execPath,[requiringEsm]); |
| 52 | + |
| 53 | +assert.ok( |
| 54 | +stderr.replace(/\r/g,'').includes( |
| 55 | +`Error [ERR_REQUIRE_ESM]: require() of ES Module ${required} from ${requiringEsm} not supported.\n` |
| 56 | +) |
| 57 | +); |
| 58 | +assert.ok( |
| 59 | +stderr.replace(/\r/g,'').includes( |
| 60 | +`Instead change the require of ${basename} in ${requiringEsm} to`+ |
| 61 | +' a dynamic import() which is available in all CommonJS modules.\n' |
| 62 | +) |
| 63 | +); |
41 | 64 |
|
42 | | -{ |
43 | | -constrequired=path.resolve( |
44 | | -fixtures.path('/es-modules/package-type-module/esm.js') |
45 | | -); |
46 | | -constbasename='esm.js'; |
47 | | -constchild=spawn(process.execPath,[requiringEsm]); |
48 | | -letstderr=''; |
49 | | -child.stderr.setEncoding('utf8'); |
50 | | -child.stderr.on('data',(data)=>{ |
51 | | -stderr+=data; |
52 | | -}); |
53 | | -child.on('close',common.mustCall((code,signal)=>{ |
54 | 65 | assert.strictEqual(code,1); |
55 | 66 | assert.strictEqual(signal,null); |
56 | | - |
57 | | -assert.ok(stderr.replace(/\r/g,'').includes( |
58 | | -`Error [ERR_REQUIRE_ESM]: require() of ES Module ${required} from ${ |
59 | | -requiringEsm} not supported.\n`)); |
60 | | -assert.ok(stderr.replace(/\r/g,'').includes( |
61 | | -`Instead change the require of ${basename} in ${requiringEsm} to`+ |
62 | | -' a dynamic import() which is available in all CommonJS modules.\n')); |
63 | | -})); |
64 | | -} |
| 67 | +}); |
| 68 | +}); |
0 commit comments