@@ -53,10 +53,10 @@ function omitLeadingWhitespace(text: string): string {
53
53
54
54
describe ( 'transpileModules' , ( ) => {
55
55
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' )
60
60
vol . fromJSON (
61
61
{
62
62
'./esm-node-modern/package.json' : JSON . stringify ( {
@@ -68,7 +68,7 @@ describe('transpileModules', () => {
68
68
69
69
console.log(foo);
70
70
` ,
71
- './esm-node-modern/ foo.mts' : `
71
+ './foo.mts' : `
72
72
import { foo } from 'foo';
73
73
74
74
console.log(foo);
@@ -82,7 +82,7 @@ describe('transpileModules', () => {
82
82
83
83
console.log(foo);
84
84
` ,
85
- './esm-node-modern/ foo.cts' : `
85
+ './foo.cts' : `
86
86
import { foo } from 'foo';
87
87
88
88
console.log(foo);
@@ -94,20 +94,17 @@ describe('transpileModules', () => {
94
94
it . each ( [
95
95
{
96
96
module : ts . ModuleKind . Node16 ,
97
- moduleResolution : ts . ModuleResolutionKind . Node16 ,
98
97
} ,
99
98
{
100
99
module : ts . ModuleKind . NodeNext ,
101
- moduleResolution : ts . ModuleResolutionKind . NodeNext ,
102
100
} ,
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 ,
106
104
compilerOptions : {
107
105
module,
108
106
target : ts . ScriptTarget . ESNext ,
109
107
verbatimModuleSyntax : true ,
110
- moduleResolution,
111
108
} ,
112
109
} )
113
110
@@ -119,19 +116,16 @@ describe('transpileModules', () => {
119
116
it . each ( [
120
117
{
121
118
module : ts . ModuleKind . Node16 ,
122
- moduleResolution : ts . ModuleResolutionKind . Node16 ,
123
119
} ,
124
120
{
125
121
module : ts . ModuleKind . NodeNext ,
126
- moduleResolution : ts . ModuleResolutionKind . NodeNext ,
127
122
} ,
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 ,
131
126
compilerOptions : {
132
127
module,
133
128
target : ts . ScriptTarget . ESNext ,
134
- moduleResolution,
135
129
} ,
136
130
} )
137
131
@@ -140,30 +134,78 @@ describe('transpileModules', () => {
140
134
` )
141
135
} )
142
136
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 } ) => {
144
163
const result = tsTranspileModule ( vol . readFileSync ( mtsFilePath , 'utf-8' ) . toString ( ) , {
145
164
fileName : mtsFilePath ,
146
165
compilerOptions : {
166
+ module,
147
167
target : ts . ScriptTarget . ESNext ,
148
168
} ,
149
169
} )
150
170
151
- expect ( omitLeadingWhitespace ( result . outputText ) ) . toContain ( dedent `
152
- import { foo } from 'foo';
153
- ` )
171
+ expect ( omitLeadingWhitespace ( result . outputText ) ) . toContain ( expectedResult )
154
172
} )
155
173
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 } ) => {
157
200
const result = tsTranspileModule ( vol . readFileSync ( ctsFilePath , 'utf-8' ) . toString ( ) , {
158
201
fileName : ctsFilePath ,
159
202
compilerOptions : {
203
+ module,
160
204
target : ts . ScriptTarget . ESNext ,
161
205
} ,
162
206
} )
163
207
164
- expect ( omitLeadingWhitespace ( result . outputText ) ) . toContain ( dedent `
165
- import { foo } from 'foo';
166
- ` )
208
+ expect ( omitLeadingWhitespace ( result . outputText ) ) . toContain ( expectedResult )
167
209
} )
168
210
} )
169
211
0 commit comments