-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Expand file tree
/
Copy pathtest-esm-extensionless-commonjs-type.js
More file actions
52 lines (45 loc) · 1.94 KB
/
test-esm-extensionless-commonjs-type.js
File metadata and controls
52 lines (45 loc) · 1.94 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
51
52
'use strict';
// Test that extensionless files containing ESM syntax are not silently
// swallowed when the nearest package.json has "type": "commonjs".
// Regression test for https://github.com/nodejs/node/issues/61104
const common = require('../common');
const assert = require('assert');
const { execFileSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();
const dir = path.join(tmpdir.path, 'esm-extensionless');
fs.mkdirSync(dir, { recursive: true });
// Create package.json with "type": "commonjs"
fs.writeFileSync(path.join(dir, 'package.json'), JSON.stringify({
type: 'commonjs',
}));
// Create an extensionless script with ESM syntax (simulating a CLI tool with a shebang)
const script = path.join(dir, 'script');
fs.writeFileSync(script, `#!/usr/bin/env node
process.exitCode = 42;
export {};
`);
fs.chmodSync(script, 0o755);
// The script should either run as ESM (exit code 42) or throw an error.
// It must NOT silently exit with code 0.
try {
const result = execFileSync(process.execPath, [script], {
encoding: 'utf8',
stdio: ['pipe', 'pipe', 'pipe'],
});
// If we reach here, the script ran without error.
// The exit code should be 42 (set by process.exitCode in the ESM script).
assert.fail('Expected the script to either exit with code 42 or throw an error, but it exited with code 0');
} catch (err) {
// execFileSync throws if exit code is non-zero, which is expected.
// Either exit code 42 (ESM ran correctly) or an error was thrown (also acceptable).
if (err.status !== null) {
// The script ran but exited non-zero — ESM was properly detected and executed.
assert.strictEqual(err.status, 42,
`Expected exit code 42 from ESM script, got ${err.status}. stderr: ${err.stderr}`);
}
// If there's a stderr message about ESM/CommonJS mismatch, that's also acceptable
// as long as it's not a silent success.
}