• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const { spawnPromisified } = require('../common');
4const fixtures = require('../common/fixtures.js');
5const assert = require('node:assert');
6const path = require('node:path');
7const { execPath } = require('node:process');
8const { describe, it } = require('node:test');
9
10
11const requiringCjsAsEsm = path.resolve(fixtures.path('/es-modules/cjs-esm.js'));
12const requiringEsm = path.resolve(fixtures.path('/es-modules/cjs-esm-esm.js'));
13const pjson = path.resolve(
14  fixtures.path('/es-modules/package-type-module/package.json')
15);
16
17
18describe('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
42    assert.strictEqual(code, 1);
43    assert.strictEqual(signal, null);
44  });
45
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    );
64
65    assert.strictEqual(code, 1);
66    assert.strictEqual(signal, null);
67  });
68});
69