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' ) ;
@@ -20,18 +21,42 @@ expect('', packageTypeCommonJsMain, 'package-type-commonjs');
2021expect ( '' , packageWithoutTypeMain , 'package-without-type' ) ;
2122
2223// 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- }
24+ describe ( 'ESM type field errors' , { concurrency : true } , ( ) => {
25+ it ( '.cjs file' , ( ) => {
26+ expect ( '' , cjsFile , '.cjs file' ) ;
27+ } ) ;
28+
29+ it ( '.mjs file' , ( ) => {
30+ expect ( '' , mjsFile , '.mjs file' ) ;
31+ } ) ;
32+
33+ it ( 'package.json with "type": "module"' , ( ) => {
34+ expect ( '' , packageTypeModuleMain , 'package-type-module' ) ;
35+ } ) ;
36+
37+ it ( 'package.json with "type": "commonjs"' , ( ) => {
38+ expect ( '' , packageTypeCommonJsMain , 'package-type-commonjs' ) ;
39+ } ) ;
40+
41+ it ( 'package.json with no "type" field' , ( ) => {
42+ expect ( '' , packageWithoutTypeMain , 'package-without-type' ) ;
43+ } ) ;
44+
45+ it ( '--input-type=module disallowed for files' , ( ) => {
46+ expect (
47+ '--input-type=module' ,
48+ packageTypeModuleMain ,
49+ 'ERR_INPUT_TYPE_NOT_ALLOWED' ,
50+ true ,
51+ ) ;
52+ } ) ;
53+
54+ it ( '--input-type=module disallowed for directories' , ( ) => {
55+ assert . throws ( ( ) => require ( '../fixtures/es-modules/package-type-module/index.js' ) , {
56+ code : 'ERR_REQUIRE_ESM'
57+ } ) ;
58+ } ) ;
59+ } ) ;
3560
3661function expect ( opt = '' , inputFile , want , wantsError = false ) {
3762 const argv = [ inputFile ] ;
0 commit comments