@@ -53,10 +53,10 @@ function omitLeadingWhitespace(text: string): string {
5353
5454describe ( 'transpileModules' , ( ) => {
5555 describe ( 'with modern Node resolution' , ( ) => {
56- const esmModernNodeFilePath = path . join ( workspaceRoot , 'esm-node-modern' , 'foo.ts' )
57- const mtsFilePath = path . join ( workspaceRoot , 'esm-node-modern' , ' foo.mts')
58- const cjsModernNodeFilePath = path . join ( workspaceRoot , 'cjs-node-modern' , 'foo.ts' )
59- const ctsFilePath = path . join ( workspaceRoot , 'esm-node-modern' , ' foo.cts')
56+ const tsFilePathInEsmModernNode = path . join ( workspaceRoot , 'esm-node-modern' , 'foo.ts' )
57+ const mtsFilePath = path . join ( workspaceRoot , 'foo.mts' )
58+ const tsFilePathInCjsModernNode = path . join ( workspaceRoot , 'cjs-node-modern' , 'foo.ts' )
59+ const ctsFilePath = path . join ( workspaceRoot , 'foo.cts' )
6060 vol . fromJSON (
6161 {
6262 './esm-node-modern/package.json' : JSON . stringify ( {
@@ -68,7 +68,7 @@ describe('transpileModules', () => {
6868
6969 console.log(foo);
7070 ` ,
71- './esm-node-modern/ foo.mts' : `
71+ './foo.mts' : `
7272 import { foo } from 'foo';
7373
7474 console.log(foo);
@@ -82,7 +82,7 @@ describe('transpileModules', () => {
8282
8383 console.log(foo);
8484 ` ,
85- './esm-node-modern/ foo.cts' : `
85+ './foo.cts' : `
8686 import { foo } from 'foo';
8787
8888 console.log(foo);
@@ -94,20 +94,17 @@ describe('transpileModules', () => {
9494 it . each ( [
9595 {
9696 module : ts . ModuleKind . Node16 ,
97- moduleResolution : ts . ModuleResolutionKind . Node16 ,
9897 } ,
9998 {
10099 module : ts . ModuleKind . NodeNext ,
101- moduleResolution : ts . ModuleResolutionKind . NodeNext ,
102100 } ,
103- ] ) ( 'should emit CJS code with "type: commonjs" in package.json' , ( { module, moduleResolution } ) => {
104- const result = tsTranspileModule ( vol . readFileSync ( cjsModernNodeFilePath , 'utf-8' ) . toString ( ) , {
105- fileName : cjsModernNodeFilePath ,
101+ ] ) ( 'should emit CJS code with "type: commonjs" in package.json' , ( { module } ) => {
102+ const result = tsTranspileModule ( vol . readFileSync ( tsFilePathInCjsModernNode , 'utf-8' ) . toString ( ) , {
103+ fileName : tsFilePathInCjsModernNode ,
106104 compilerOptions : {
107105 module,
108106 target : ts . ScriptTarget . ESNext ,
109107 verbatimModuleSyntax : true ,
110- moduleResolution,
111108 } ,
112109 } )
113110
@@ -119,19 +116,16 @@ describe('transpileModules', () => {
119116 it . each ( [
120117 {
121118 module : ts . ModuleKind . Node16 ,
122- moduleResolution : ts . ModuleResolutionKind . Node16 ,
123119 } ,
124120 {
125121 module : ts . ModuleKind . NodeNext ,
126- moduleResolution : ts . ModuleResolutionKind . NodeNext ,
127122 } ,
128- ] ) ( 'should emit ESM code with "type: module" in package.json' , ( { module, moduleResolution } ) => {
129- const result = tsTranspileModule ( vol . readFileSync ( esmModernNodeFilePath , 'utf-8' ) . toString ( ) , {
130- fileName : esmModernNodeFilePath ,
123+ ] ) ( 'should emit ESM code with "type: module" in package.json' , ( { module } ) => {
124+ const result = tsTranspileModule ( vol . readFileSync ( tsFilePathInEsmModernNode , 'utf-8' ) . toString ( ) , {
125+ fileName : tsFilePathInEsmModernNode ,
131126 compilerOptions : {
132127 module,
133128 target : ts . ScriptTarget . ESNext ,
134- moduleResolution,
135129 } ,
136130 } )
137131
@@ -140,30 +134,78 @@ describe('transpileModules', () => {
140134 ` )
141135 } )
142136
143- it ( 'should emit ESM code with .mts extension' , ( ) => {
137+ it . each ( [
138+ {
139+ module : ts . ModuleKind . CommonJS ,
140+ expectedResult : dedent `
141+ const foo_1 = require("foo");
142+ ` ,
143+ } ,
144+ {
145+ module : ts . ModuleKind . Node16 ,
146+ expectedResult : dedent `
147+ import { foo } from 'foo';
148+ ` ,
149+ } ,
150+ {
151+ module : ts . ModuleKind . ES2020 ,
152+ expectedResult : dedent `
153+ import { foo } from 'foo';
154+ ` ,
155+ } ,
156+ {
157+ module : undefined ,
158+ expectedResult : dedent `
159+ import { foo } from 'foo';
160+ ` ,
161+ } ,
162+ ] ) ( 'should emit code with ".mts" extension respecting module option' , ( { module, expectedResult } ) => {
144163 const result = tsTranspileModule ( vol . readFileSync ( mtsFilePath , 'utf-8' ) . toString ( ) , {
145164 fileName : mtsFilePath ,
146165 compilerOptions : {
166+ module,
147167 target : ts . ScriptTarget . ESNext ,
148168 } ,
149169 } )
150170
151- expect ( omitLeadingWhitespace ( result . outputText ) ) . toContain ( dedent `
152- import { foo } from 'foo';
153- ` )
171+ expect ( omitLeadingWhitespace ( result . outputText ) ) . toContain ( expectedResult )
154172 } )
155173
156- it ( 'should emit CJS code with .cts extension' , ( ) => {
174+ it . each ( [
175+ {
176+ module : ts . ModuleKind . CommonJS ,
177+ expectedResult : dedent `
178+ const foo_1 = require("foo");
179+ ` ,
180+ } ,
181+ {
182+ module : ts . ModuleKind . Node16 ,
183+ expectedResult : dedent `
184+ const foo_1 = require("foo");
185+ ` ,
186+ } ,
187+ {
188+ module : ts . ModuleKind . ES2020 ,
189+ expectedResult : dedent `
190+ import { foo } from 'foo';
191+ ` ,
192+ } ,
193+ {
194+ module : undefined ,
195+ expectedResult : dedent `
196+ import { foo } from 'foo';
197+ ` ,
198+ } ,
199+ ] ) ( 'should emit code with ".cts" extension respecting module option' , ( { module, expectedResult } ) => {
157200 const result = tsTranspileModule ( vol . readFileSync ( ctsFilePath , 'utf-8' ) . toString ( ) , {
158201 fileName : ctsFilePath ,
159202 compilerOptions : {
203+ module,
160204 target : ts . ScriptTarget . ESNext ,
161205 } ,
162206 } )
163207
164- expect ( omitLeadingWhitespace ( result . outputText ) ) . toContain ( dedent `
165- import { foo } from 'foo';
166- ` )
208+ expect ( omitLeadingWhitespace ( result . outputText ) ) . toContain ( expectedResult )
167209 } )
168210 } )
169211
0 commit comments