-
Notifications
You must be signed in to change notification settings - Fork 30.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
module: fix error reporting when commonjs requires an ES module
- Loading branch information
1 parent
03ec900
commit 96f5831
Showing
5 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
test/fixtures/es-modules/package-type-module/require-esm-error-annotation/app.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import nothing from 'somewhere'; |
11 changes: 11 additions & 0 deletions
11
test/fixtures/es-modules/package-type-module/require-esm-error-annotation/index.cjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
function main() { | ||
func1(func2(func3())) | ||
} | ||
|
||
function func1() { | ||
require('./app.js') | ||
} | ||
function func2() {} | ||
function func3() {} | ||
|
||
main() |
3 changes: 3 additions & 0 deletions
3
test/fixtures/es-modules/package-type-module/require-esm-error-annotation/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"type": "module" | ||
} |
52 changes: 52 additions & 0 deletions
52
test/parallel/test-require-esm-with-no-experimental-require-module.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import '../common/index.mjs'; | ||
import { test } from 'node:test'; | ||
import * as fixtures from '../common/fixtures.mjs'; | ||
import { spawnSync } from 'node:child_process'; | ||
import assert from 'node:assert'; | ||
import { EOL } from 'node:os'; | ||
|
||
test('correctly reports errors when an ESM module is required with --no-experimental-require-module', () => { | ||
// The following regex matches the error message that is expected to be thrown | ||
// | ||
// package-type-module/require-esm-error-annotation/index.cjs:1 | ||
// const app = require('./app'); | ||
// ^ | ||
|
||
const fixture = fixtures.path('es-modules/package-type-module/require-esm-error-annotation/index.cjs'); | ||
const args = ['--no-experimental-require-module', fixture]; | ||
|
||
const lineNumber = 1; | ||
const lineContent = `const app = require('./app');`; | ||
const fullMessage = `${fixture}:${lineNumber}${EOL}${lineContent}${EOL} ^${EOL}`; | ||
|
||
const result = spawnSync(process.execPath, args); | ||
|
||
console.log(result.stderr.toString()); | ||
|
||
assert.strictEqual(result.status, 1); | ||
assert(result.stderr.toString().indexOf(fullMessage) > -1); | ||
assert.strictEqual(result.stdout.toString(), ''); | ||
}); | ||
|
||
test('correctly reports error for a longer stack trace', () => { | ||
// The following regex matches the error message that is expected to be thrown | ||
// | ||
// package-type-module/require-esm-error-annotation/longer-stack.cjs:6 | ||
// require('./app.js') | ||
// ^ | ||
|
||
const fixture = fixtures.path('es-modules/package-type-module/require-esm-error-annotation/longer-stack.cjs'); | ||
const args = ['--no-experimental-require-module', fixture]; | ||
|
||
const lineNumber = 6; | ||
const lineContent = "require('./app.js')"; | ||
const fullMessage = `${fixture}:${lineNumber}${EOL} ${lineContent}${EOL} ^${EOL}`; | ||
|
||
const result = spawnSync(process.execPath, args); | ||
|
||
console.log(result.stderr.toString()); | ||
|
||
assert.strictEqual(result.status, 1); | ||
assert.strictEqual(result.stdout.toString(), ''); | ||
assert(result.stderr.toString().indexOf(fullMessage) > -1); | ||
}); |