@@ -146,34 +146,41 @@ export class TsCompiler implements TsCompilerInstance {
146146 return importedModulePaths
147147 }
148148
149- getCompiledOutput ( fileContent : string , fileName : string , options : TsJestCompileOptions ) : CompiledOutput {
150- let moduleKind = this . _initialCompilerOptions . module
151- let esModuleInterop = this . _initialCompilerOptions . esModuleInterop
152- let allowSyntheticDefaultImports = this . _initialCompilerOptions . allowSyntheticDefaultImports
153- const currentModuleKind = this . _compilerOptions . module
154- const isEsmMode = this . configSet . useESM && options . supportsStaticESM
155- if (
156- ( this . configSet . babelJestTransformer || ( ! this . configSet . babelJestTransformer && options . supportsStaticESM ) ) &&
157- this . configSet . useESM
158- ) {
159- moduleKind =
160- ! moduleKind ||
161- ( moduleKind &&
162- ! [ this . _ts . ModuleKind . ES2015 , this . _ts . ModuleKind . ES2020 , this . _ts . ModuleKind . ESNext ] . includes ( moduleKind ) )
163- ? this . _ts . ModuleKind . ESNext
164- : moduleKind
165- // Make sure `esModuleInterop` and `allowSyntheticDefaultImports` true to support import CJS into ESM
166- esModuleInterop = true
167- allowSyntheticDefaultImports = true
168- } else {
169- moduleKind = this . _ts . ModuleKind . CommonJS
149+ private fixupCompilerOptionsForModuleKind ( compilerOptions : CompilerOptions , isEsm : boolean ) : CompilerOptions {
150+ if ( ! isEsm ) {
151+ const moduleKind = compilerOptions . module ?? this . _ts . ModuleKind . CommonJS
152+ const moduleKindValue = [
153+ this . _ts . ModuleKind . CommonJS ,
154+ this . _ts . ModuleKind . Node16 ,
155+ this . _ts . ModuleKind . NodeNext ,
156+ ] . includes ( moduleKind )
157+ ? moduleKind
158+ : this . _ts . ModuleKind . CommonJS
159+
160+ return {
161+ ...compilerOptions ,
162+ module : moduleKindValue ,
163+ }
170164 }
171- this . _compilerOptions = {
172- ...this . _compilerOptions ,
173- allowSyntheticDefaultImports,
174- esModuleInterop,
165+
166+ const moduleKind = compilerOptions . module ?? this . _ts . ModuleKind . ESNext
167+ const esModuleInterop = [ this . _ts . ModuleKind . Node16 , this . _ts . ModuleKind . NodeNext ] . includes ( moduleKind )
168+ ? true
169+ : compilerOptions . esModuleInterop
170+
171+ return {
172+ ...compilerOptions ,
175173 module : moduleKind ,
174+ esModuleInterop : esModuleInterop ?? false ,
175+ allowSyntheticDefaultImports : esModuleInterop ?? compilerOptions . allowSyntheticDefaultImports ,
176176 }
177+ }
178+
179+ getCompiledOutput ( fileContent : string , fileName : string , options : TsJestCompileOptions ) : CompiledOutput {
180+ const moduleKind = this . _initialCompilerOptions . module
181+ const currentModuleKind = this . _compilerOptions . module
182+ const isEsmMode = this . configSet . useESM && options . supportsStaticESM
183+ this . _compilerOptions = this . fixupCompilerOptionsForModuleKind ( this . _initialCompilerOptions , isEsmMode )
177184 if ( this . _languageService ) {
178185 this . _logger . debug ( { fileName } , 'getCompiledOutput(): compiling using language service' )
179186
0 commit comments