|
| 1 | +import '../common/index.mjs'; |
| 2 | +import { test } from 'node:test'; |
| 3 | +import * as fixtures from '../common/fixtures.mjs'; |
| 4 | +import { spawnSync } from 'node:child_process'; |
| 5 | +import assert from 'node:assert'; |
| 6 | +import { EOL } from 'node:os'; |
| 7 | + |
| 8 | +test('correctly reports errors when an ESM module is required with --no-experimental-require-module', () => { |
| 9 | + // The following regex matches the error message that is expected to be thrown |
| 10 | + // |
| 11 | + // package-type-module/require-esm-error-annotation/index.cjs:1 |
| 12 | + // const app = require('./app'); |
| 13 | + // ^ |
| 14 | + |
| 15 | + const fixture = fixtures.path('es-modules/package-type-module/require-esm-error-annotation/index.cjs'); |
| 16 | + const args = ['--no-experimental-require-module', fixture]; |
| 17 | + |
| 18 | + const lineNumber = 1; |
| 19 | + const lineContent = `const app = require('./app');`; |
| 20 | + const fullMessage = `${fixture}:${lineNumber}${EOL}${lineContent}${EOL} ^${EOL}`; |
| 21 | + |
| 22 | + const result = spawnSync(process.execPath, args); |
| 23 | + |
| 24 | + console.log(result.stderr.toString()); |
| 25 | + |
| 26 | + assert.strictEqual(result.status, 1); |
| 27 | + assert(result.stderr.toString().indexOf(fullMessage) > -1); |
| 28 | + assert.strictEqual(result.stdout.toString(), ''); |
| 29 | +}); |
| 30 | + |
| 31 | +test('correctly reports error for a longer stack trace', () => { |
| 32 | + // The following regex matches the error message that is expected to be thrown |
| 33 | + // |
| 34 | + // package-type-module/require-esm-error-annotation/longer-stack.cjs:6 |
| 35 | + // require('./app.js') |
| 36 | + // ^ |
| 37 | + |
| 38 | + const fixture = fixtures.path('es-modules/package-type-module/require-esm-error-annotation/longer-stack.cjs'); |
| 39 | + const args = ['--no-experimental-require-module', fixture]; |
| 40 | + |
| 41 | + const lineNumber = 6; |
| 42 | + const lineContent = "require('./app.js')"; |
| 43 | + const fullMessage = `${fixture}:${lineNumber}${EOL} ${lineContent}${EOL} ^${EOL}`; |
| 44 | + |
| 45 | + const result = spawnSync(process.execPath, args); |
| 46 | + |
| 47 | + console.log(result.stderr.toString()); |
| 48 | + |
| 49 | + assert.strictEqual(result.status, 1); |
| 50 | + assert.strictEqual(result.stdout.toString(), ''); |
| 51 | + assert(result.stderr.toString().indexOf(fullMessage) > -1); |
| 52 | +}); |
0 commit comments