22const common = require ( '../common' ) ;
33const assert = require ( 'assert' ) ;
44const exec = require ( 'child_process' ) . execFile ;
5+ const { describe, it } = require ( 'node:test' ) ;
56
67const mjsFile = require . resolve ( '../fixtures/es-modules/mjs-file.mjs' ) ;
78const cjsFile = require . resolve ( '../fixtures/es-modules/cjs-file.cjs' ) ;
@@ -13,25 +14,48 @@ const packageTypeModuleMain =
1314 require . resolve ( '../fixtures/es-modules/package-type-module/index.js' ) ;
1415
1516// Check that running `node` without options works
16- expect ( '' , mjsFile , '.mjs file' ) ;
17- expect ( '' , cjsFile , '.cjs file' ) ;
18- expect ( '' , packageTypeModuleMain , 'package-type-module' ) ;
19- expect ( '' , packageTypeCommonJsMain , 'package-type-commonjs' ) ;
20- expect ( '' , packageWithoutTypeMain , 'package-without-type' ) ;
21-
22- // Check that --input-type isn't allowed for files
23- expect ( '--input-type=module' , packageTypeModuleMain ,
24- 'ERR_INPUT_TYPE_NOT_ALLOWED' , true ) ;
25-
26- try {
27- require ( '../fixtures/es-modules/package-type-module/index.js' ) ;
28- assert . fail ( 'Expected CJS to fail loading from type: module package.' ) ;
29- } catch ( e ) {
30- assert . strictEqual ( e . name , 'Error' ) ;
31- assert . strictEqual ( e . code , 'ERR_REQUIRE_ESM' ) ;
32- assert ( e . toString ( ) . match ( / r e q u i r e \( \) o f E S M o d u l e / g) ) ;
33- assert ( e . message . match ( / r e q u i r e \( \) o f E S M o d u l e / g) ) ;
34- }
17+ describe ( 'ESM type field errors' , { concurrency : true } , ( ) => {
18+ it ( '.cjs file' , ( ) => {
19+ expect ( '' , cjsFile , '.cjs file' ) ;
20+ } ) ;
21+
22+ it ( '.mjs file' , ( ) => {
23+ expect ( '' , mjsFile , '.mjs file' ) ;
24+ } ) ;
25+
26+ it ( 'package.json with "type": "module"' , ( ) => {
27+ expect ( '' , packageTypeModuleMain , 'package-type-module' ) ;
28+ } ) ;
29+
30+ it ( 'package.json with "type": "commonjs"' , ( ) => {
31+ expect ( '' , packageTypeCommonJsMain , 'package-type-commonjs' ) ;
32+ } ) ;
33+
34+ it ( 'package.json with no "type" field' , ( ) => {
35+ expect ( '' , packageWithoutTypeMain , 'package-without-type' ) ;
36+ } ) ;
37+
38+ it ( '--input-type=module disallowed for files' , ( ) => {
39+ expect (
40+ '--input-type=module' ,
41+ packageTypeModuleMain ,
42+ 'ERR_INPUT_TYPE_NOT_ALLOWED' ,
43+ true ,
44+ ) ;
45+ } ) ;
46+
47+ it ( '--input-type=module disallowed for directories' , ( ) => {
48+ try {
49+ require ( '../fixtures/es-modules/package-type-module/index.js' ) ;
50+ assert . fail ( 'Expected CJS to fail loading from type: module package.' ) ;
51+ } catch ( e ) {
52+ assert . match ( e . toString ( ) , / r e q u i r e \( \) o f E S M o d u l e / g) ;
53+ assert . match ( e . message , / r e q u i r e \( \) o f E S M o d u l e / g) ;
54+ assert . strictEqual ( e . code , 'ERR_REQUIRE_ESM' ) ;
55+ assert . strictEqual ( e . name , 'Error' ) ;
56+ }
57+ } ) ;
58+ } ) ;
3559
3660function expect ( opt = '' , inputFile , want , wantsError = false ) {
3761 const argv = [ inputFile ] ;
0 commit comments