-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Expand file tree
/
Copy pathtest-cjs-esm-error-annotation.js
More file actions
50 lines (42 loc) · 1.89 KB
/
test-cjs-esm-error-annotation.js
File metadata and controls
50 lines (42 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
'use strict';
// This test verifies that when a CommonJS module requires an ES module,
// the error annotation (arrow message) points to the user's require()
// call in their source file, not to an internal frame such as
// TracingChannel.traceSync in node:diagnostics_channel.
// Regression test for https://github.com/nodejs/node/issues/55350.
const { spawnPromisified } = require('../common');
const fixtures = require('../common/fixtures.js');
const assert = require('node:assert');
const path = require('node:path');
const { execPath } = require('node:process');
const { describe, it } = require('node:test');
const requiringEsm = path.resolve(
fixtures.path('/es-modules/cjs-esm-esm.js')
);
describe('ERR_REQUIRE_ESM error annotation', { concurrency: !process.env.TEST_PARALLEL }, () => {
it('should point to the user require() call, not internal frames', async () => {
const { code, stderr } = await spawnPromisified(execPath, [
'--no-experimental-require-module', requiringEsm,
]);
assert.strictEqual(code, 1);
const stderrStr = stderr.replaceAll('\r', '');
// The error annotation should reference the user's file, not
// node:diagnostics_channel or any other internal module.
assert.ok(
stderrStr.includes(requiringEsm),
`Expected error output to reference ${requiringEsm}, got:\n${stderrStr}`
);
// The first line of the error output (before "Error [ERR_REQUIRE_ESM]")
// should contain the path to the requiring file, not "undefined".
const lines = stderrStr.split('\n');
const fileAnnotationLine = lines[0];
assert.ok(
fileAnnotationLine.includes(requiringEsm),
`First line should reference the requiring file, got: "${fileAnnotationLine}"`
);
assert.ok(
!fileAnnotationLine.includes('diagnostics_channel'),
`First line should not reference diagnostics_channel, got: "${fileAnnotationLine}"`
);
});
});