11import { basename , normalize } from 'path'
22
3- import type { TransformedSource } from '@jest/transform'
43import { LogContexts , Logger , LogLevels } from 'bs-logger'
54import memoize from 'lodash.memoize'
65import type {
@@ -25,13 +24,13 @@ import type {
2524
2625import { LINE_FEED , TS_TSX_REGEX } from '../../constants'
2726import type {
28- DepGraphInfo ,
2927 StringMap ,
3028 TsCompilerInstance ,
3129 TsJestAstTransformer ,
3230 TsJestCompileOptions ,
3331 TTypeScript ,
3432} from '../../types'
33+ import { CompiledOutput } from '../../types'
3534import { rootLogger } from '../../utils'
3635import { Errors , interpolate } from '../../utils/messages'
3736import type { ConfigSet } from '../config/config-set'
@@ -146,11 +145,12 @@ export class TsCompiler implements TsCompilerInstance {
146145 return importedModulePaths
147146 }
148147
149- getCompiledOutput ( fileContent : string , fileName : string , options : TsJestCompileOptions ) : TransformedSource {
148+ getCompiledOutput ( fileContent : string , fileName : string , options : TsJestCompileOptions ) : CompiledOutput {
150149 let moduleKind = this . _initialCompilerOptions . module
151150 let esModuleInterop = this . _initialCompilerOptions . esModuleInterop
152151 let allowSyntheticDefaultImports = this . _initialCompilerOptions . allowSyntheticDefaultImports
153152 const currentModuleKind = this . _compilerOptions . module
153+ const isEsmMode = this . configSet . useESM && options . supportsStaticESM
154154 if (
155155 ( this . configSet . babelJestTransformer || ( ! this . configSet . babelJestTransformer && options . supportsStaticESM ) ) &&
156156 this . configSet . useESM
@@ -179,7 +179,34 @@ export class TsCompiler implements TsCompilerInstance {
179179 // Must set memory cache before attempting to compile
180180 this . _updateMemoryCache ( fileContent , fileName , currentModuleKind === moduleKind )
181181 const output : EmitOutput = this . _languageService . getEmitOutput ( fileName )
182- this . _doTypeChecking ( fileName , options . depGraphs , options . watchMode )
182+ const diagnostics = this . getDiagnostics ( fileName )
183+ if ( ! isEsmMode && diagnostics . length ) {
184+ this . configSet . raiseDiagnostics ( diagnostics , fileName , this . _logger )
185+ if ( options . watchMode ) {
186+ this . _logger . debug ( { fileName } , '_doTypeChecking(): starting watch mode computing diagnostics' )
187+
188+ for ( const entry of options . depGraphs . entries ( ) ) {
189+ const normalizedModuleNames = entry [ 1 ] . resolvedModuleNames . map ( ( moduleName ) => normalize ( moduleName ) )
190+ const fileToReTypeCheck = entry [ 0 ]
191+ if ( normalizedModuleNames . includes ( fileName ) && this . configSet . shouldReportDiagnostics ( fileToReTypeCheck ) ) {
192+ this . _logger . debug (
193+ { fileToReTypeCheck } ,
194+ '_doTypeChecking(): computing diagnostics using language service' ,
195+ )
196+
197+ this . _updateMemoryCache ( this . _getFileContentFromCache ( fileToReTypeCheck ) , fileToReTypeCheck )
198+ const importedModulesDiagnostics = [
199+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
200+ ...this . _languageService ! . getSemanticDiagnostics ( fileToReTypeCheck ) ,
201+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
202+ ...this . _languageService ! . getSyntacticDiagnostics ( fileToReTypeCheck ) ,
203+ ]
204+ // will raise or just warn diagnostics depending on config
205+ this . configSet . raiseDiagnostics ( importedModulesDiagnostics , fileName , this . _logger )
206+ }
207+ }
208+ }
209+ }
183210 if ( output . emitSkipped ) {
184211 if ( TS_TSX_REGEX . test ( fileName ) ) {
185212 throw new Error ( interpolate ( Errors . CannotProcessFile , { file : fileName } ) )
@@ -204,9 +231,11 @@ export class TsCompiler implements TsCompilerInstance {
204231 return this . _compilerOptions . sourceMap
205232 ? {
206233 code : updateOutput ( outputFiles [ 1 ] . text , fileName , outputFiles [ 0 ] . text ) ,
234+ diagnostics,
207235 }
208236 : {
209237 code : updateOutput ( outputFiles [ 0 ] . text , fileName ) ,
238+ diagnostics,
210239 }
211240 } else {
212241 this . _logger . debug ( { fileName } , 'getCompiledOutput(): compiling as isolated module' )
@@ -425,40 +454,20 @@ export class TsCompiler implements TsCompilerInstance {
425454 /**
426455 * @internal
427456 */
428- private _doTypeChecking ( fileName : string , depGraphs : Map < string , DepGraphInfo > , watchMode : boolean ) : void {
457+ private getDiagnostics ( fileName : string ) : Diagnostic [ ] {
458+ const diagnostics : Diagnostic [ ] = [ ]
429459 if ( this . configSet . shouldReportDiagnostics ( fileName ) ) {
430460 this . _logger . debug ( { fileName } , '_doTypeChecking(): computing diagnostics using language service' )
431461
432462 // Get the relevant diagnostics - this is 3x faster than `getPreEmitDiagnostics`.
433- const diagnostics : Diagnostic [ ] = [
463+ diagnostics . push (
434464 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
435465 ...this . _languageService ! . getSemanticDiagnostics ( fileName ) ,
436466 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
437467 ...this . _languageService ! . getSyntacticDiagnostics ( fileName ) ,
438- ]
439- // will raise or just warn diagnostics depending on config
440- this . configSet . raiseDiagnostics ( diagnostics , fileName , this . _logger )
468+ )
441469 }
442- if ( watchMode ) {
443- this . _logger . debug ( { fileName } , '_doTypeChecking(): starting watch mode computing diagnostics' )
444-
445- for ( const entry of depGraphs . entries ( ) ) {
446- const normalizedModuleNames = entry [ 1 ] . resolvedModuleNames . map ( ( moduleName ) => normalize ( moduleName ) )
447- const fileToReTypeCheck = entry [ 0 ]
448- if ( normalizedModuleNames . includes ( fileName ) && this . configSet . shouldReportDiagnostics ( fileToReTypeCheck ) ) {
449- this . _logger . debug ( { fileToReTypeCheck } , '_doTypeChecking(): computing diagnostics using language service' )
450470
451- this . _updateMemoryCache ( this . _getFileContentFromCache ( fileToReTypeCheck ) , fileToReTypeCheck )
452- const importedModulesDiagnostics = [
453- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
454- ...this . _languageService ! . getSemanticDiagnostics ( fileToReTypeCheck ) ,
455- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
456- ...this . _languageService ! . getSyntacticDiagnostics ( fileToReTypeCheck ) ,
457- ]
458- // will raise or just warn diagnostics depending on config
459- this . configSet . raiseDiagnostics ( importedModulesDiagnostics , fileName , this . _logger )
460- }
461- }
462- }
471+ return diagnostics
463472 }
464473}
0 commit comments