|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const test = require('node:test'); |
| 4 | +const assert = require('node:assert/strict'); |
| 5 | +const fs = require('node:fs'); |
| 6 | +const path = require('node:path'); |
| 7 | +const { spawnSync } = require('node:child_process'); |
| 8 | + |
| 9 | +const tmpdir = require('../common/tmpdir'); |
| 10 | + |
| 11 | +test('extensionless entry point with ESM syntax under type=commonjs should not silently exit 0', () => { |
| 12 | + tmpdir.refresh(); |
| 13 | + |
| 14 | + const dir = tmpdir.resolve('extensionless-esm-commonjs'); |
| 15 | + fs.mkdirSync(dir, { recursive: true }); |
| 16 | + |
| 17 | + // package.json with type: commonjs |
| 18 | + fs.writeFileSync( |
| 19 | + path.join(dir, 'package.json'), |
| 20 | + '{\n "type": "commonjs"\n}\n', |
| 21 | + 'utf8' |
| 22 | + ); |
| 23 | + |
| 24 | + // Extensionless executable with shebang + ESM syntax. |
| 25 | + // NOTE: Execute via process.execPath to avoid PATH/env differences. |
| 26 | + const scriptPath = path.join(dir, 'script'); // no extension |
| 27 | + fs.writeFileSync( |
| 28 | + scriptPath, |
| 29 | + '#!/usr/bin/env node\n' + |
| 30 | + "console.log('script STARTED')\n" + |
| 31 | + "import { version } from 'node:process'\n" + |
| 32 | + 'console.log(version)\n', |
| 33 | + 'utf8' |
| 34 | + ); |
| 35 | + fs.chmodSync(scriptPath, 0o755); |
| 36 | + |
| 37 | + const r = spawnSync(process.execPath, ['./script'], { |
| 38 | + cwd: dir, |
| 39 | + encoding: 'utf8' |
| 40 | + }); |
| 41 | + |
| 42 | + assert.ifError(r.error); |
| 43 | + |
| 44 | + assert.notEqual( |
| 45 | + r.status, |
| 46 | + 0, |
| 47 | + `unexpected exit code 0; stdout=${JSON.stringify(r.stdout)} stderr=${JSON.stringify(r.stderr)}` |
| 48 | + ); |
| 49 | + assert.ok(r.stderr && r.stderr.length > 0, 'expected stderr to contain an error message'); |
| 50 | +}); |
0 commit comments