@@ -5,6 +5,7 @@ import type { CompilerOptions, EmitOutput, transpileModule, TranspileOutput } fr
5
5
import * as ts from 'typescript'
6
6
7
7
import { createConfigSet , makeCompiler } from '../../__helpers__/fakers'
8
+ import type { RawCompilerOptions } from '../../raw-compiler-options'
8
9
import type { DepGraphInfo } from '../../types'
9
10
import { Errors , interpolate } from '../../utils/messages'
10
11
@@ -190,64 +191,80 @@ describe('TsCompiler', () => {
190
191
test . each ( [
191
192
{
192
193
useESM : true ,
193
- babelConfig : true ,
194
- supportsStaticESM : false ,
194
+ supportsStaticESM : true ,
195
+ moduleValue : 'ESNext' ,
196
+ expectedModule : ts . ModuleKind . ESNext ,
197
+ expectedEsModuleInterop : false ,
195
198
} ,
196
199
{
197
200
useESM : true ,
198
- babelConfig : false ,
199
201
supportsStaticESM : true ,
202
+ moduleValue : 'NodeNext' ,
203
+ expectedModule : ts . ModuleKind . NodeNext ,
204
+ expectedEsModuleInterop : true ,
200
205
} ,
201
206
{
202
207
useESM : true ,
203
- babelConfig : false ,
204
208
supportsStaticESM : false ,
209
+ moduleValue : 'ESNext' ,
210
+ expectedModule : ts . ModuleKind . CommonJS ,
211
+ expectedEsModuleInterop : false ,
205
212
} ,
206
213
{
207
214
useESM : false ,
208
- babelConfig : false ,
209
215
supportsStaticESM : true ,
216
+ moduleValue : 'ESNext' ,
217
+ expectedModule : ts . ModuleKind . CommonJS ,
218
+ expectedEsModuleInterop : false ,
210
219
} ,
211
- ] ) ( 'should compile codes with useESM %p' , ( { useESM, babelConfig, supportsStaticESM } ) => {
212
- const configSet = createConfigSet ( {
213
- tsJestConfig : { ...baseTsJestConfig , useESM, babelConfig } ,
214
- } )
215
- const emptyFile = join ( mockFolder , 'empty.ts' )
216
- configSet . parsedTsConfig . fileNames . push ( emptyFile )
217
- const compiler = new TsCompiler ( configSet , new Map ( ) )
218
- // @ts -expect-error testing purpose
219
- compiler . _languageService . getEmitOutput = jest . fn ( ) . mockReturnValueOnce ( {
220
- outputFiles : [ { text : sourceMap } , { text : jsOutput } ] ,
221
- emitSkipped : false ,
222
- } as EmitOutput )
223
- // @ts -expect-error testing purpose
224
- compiler . getDiagnostics = jest . fn ( ) . mockReturnValue ( [ ] )
220
+ ] ) (
221
+ 'should compile codes with useESM %p' ,
222
+ ( { useESM, supportsStaticESM, moduleValue, expectedModule, expectedEsModuleInterop } ) => {
223
+ const configSet = createConfigSet ( {
224
+ tsJestConfig : {
225
+ ...baseTsJestConfig ,
226
+ useESM,
227
+ tsconfig : {
228
+ module : moduleValue as unknown as RawCompilerOptions [ 'module' ] ,
229
+ esModuleInterop : false ,
230
+ } ,
231
+ } ,
232
+ } )
233
+ const emptyFile = join ( mockFolder , 'empty.ts' )
234
+ configSet . parsedTsConfig . fileNames . push ( emptyFile )
235
+ const compiler = new TsCompiler ( configSet , new Map ( ) )
236
+ // @ts -expect-error testing purpose
237
+ compiler . _languageService . getEmitOutput = jest . fn ( ) . mockReturnValueOnce ( {
238
+ outputFiles : [ { text : sourceMap } , { text : jsOutput } ] ,
239
+ emitSkipped : false ,
240
+ } as EmitOutput )
241
+ // @ts -expect-error testing purpose
242
+ compiler . getDiagnostics = jest . fn ( ) . mockReturnValue ( [ ] )
225
243
226
- const output = compiler . getCompiledOutput ( fileContent , fileName , {
227
- depGraphs : new Map ( ) ,
228
- supportsStaticESM,
229
- watchMode : false ,
230
- } )
244
+ const output = compiler . getCompiledOutput ( fileContent , fileName , {
245
+ depGraphs : new Map ( ) ,
246
+ supportsStaticESM,
247
+ watchMode : false ,
248
+ } )
231
249
232
- // @ts -expect-error testing purpose
233
- const usedCompilerOptions = compiler . _compilerOptions
234
- expect ( {
235
- module : usedCompilerOptions . module ,
236
- esModuleInterop : usedCompilerOptions . esModuleInterop ,
237
- allowSyntheticDefaultImports : usedCompilerOptions . allowSyntheticDefaultImports ,
238
- } ) . toMatchSnapshot ( )
239
- expect ( output ) . toEqual ( {
240
- code : updateOutput ( jsOutput , fileName , sourceMap ) ,
241
- diagnostics : [ ] ,
242
- } )
250
+ // @ts -expect-error testing purpose
251
+ const usedCompilerOptions = compiler . _compilerOptions
243
252
244
- // @ts -expect-error testing purpose
245
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
246
- compiler . _languageService ! . getSemanticDiagnostics ( fileName )
253
+ expect ( usedCompilerOptions . module ) . toBe ( expectedModule )
254
+ expect ( usedCompilerOptions . esModuleInterop ) . toBe ( expectedEsModuleInterop )
255
+ expect ( output ) . toEqual ( {
256
+ code : updateOutput ( jsOutput , fileName , sourceMap ) ,
257
+ diagnostics : [ ] ,
258
+ } )
247
259
248
- // @ts -expect-error testing purpose
249
- expect ( compiler . _fileContentCache . has ( emptyFile ) ) . toBe ( true )
250
- } )
260
+ // @ts -expect-error testing purpose
261
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
262
+ compiler . _languageService ! . getSemanticDiagnostics ( fileName )
263
+
264
+ // @ts -expect-error testing purpose
265
+ expect ( compiler . _fileContentCache . has ( emptyFile ) ) . toBe ( true )
266
+ } ,
267
+ )
251
268
252
269
test ( 'should show a warning message and return original file content for non ts/tsx files if emitSkipped is true' , ( ) => {
253
270
const compiler = makeCompiler ( {
0 commit comments