|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 |
| -const common = require('../common'); |
4 |
| -const fixtures = require('../common/fixtures'); |
5 |
| -const { spawn } = require('child_process'); |
6 |
| -const assert = require('assert'); |
7 |
| -const path = require('path'); |
| 3 | +const { spawnPromisified } = require('../common'); |
| 4 | +const fixtures = require('../common/fixtures.js'); |
| 5 | +const assert = require('node:assert'); |
| 6 | +const path = require('node:path'); |
| 7 | +const { execPath } = require('node:process'); |
| 8 | +const { describe, it } = require('node:test'); |
| 9 | + |
8 | 10 |
|
9 | 11 | const requiringCjsAsEsm = path.resolve(fixtures.path('/es-modules/cjs-esm.js'));
|
10 | 12 | const requiringEsm = path.resolve(fixtures.path('/es-modules/cjs-esm-esm.js'));
|
11 | 13 | const pjson = path.resolve(
|
12 | 14 | fixtures.path('/es-modules/package-type-module/package.json')
|
13 | 15 | );
|
14 | 16 |
|
15 |
| -{ |
16 |
| - const required = path.resolve( |
17 |
| - fixtures.path('/es-modules/package-type-module/cjs.js') |
18 |
| - ); |
19 |
| - const basename = 'cjs.js'; |
20 |
| - const child = spawn(process.execPath, [requiringCjsAsEsm]); |
21 |
| - let stderr = ''; |
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 | + const required = path.resolve( |
| 22 | + fixtures.path('/es-modules/package-type-module/cjs.js') |
| 23 | + ); |
| 24 | + const basename = 'cjs.js'; |
| 25 | + const { code, signal, stderr } = await spawnPromisified(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 | + const required = path.resolve( |
| 48 | + fixtures.path('/es-modules/package-type-module/esm.js') |
| 49 | + ); |
| 50 | + const basename = 'esm.js'; |
| 51 | + const { code, signal, stderr } = await spawnPromisified(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 |
| - const required = path.resolve( |
44 |
| - fixtures.path('/es-modules/package-type-module/esm.js') |
45 |
| - ); |
46 |
| - const basename = 'esm.js'; |
47 |
| - const child = spawn(process.execPath, [requiringEsm]); |
48 |
| - let stderr = ''; |
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