11import { readFileSync } from 'fs'
22import { basename , join , normalize } from 'path'
33
4- import { DiagnosticCategory , EmitOutput , ModuleKind , ScriptTarget , TranspileOutput } from 'typescript'
4+ import { CompilerOptions , DiagnosticCategory , EmitOutput , TranspileOutput } from 'typescript'
55
66import { createConfigSet , makeCompiler } from '../__helpers__/fakers'
77import { mockFolder } from '../__helpers__/path'
@@ -76,44 +76,57 @@ describe('TsCompiler', () => {
7676 const fileName = join ( mockFolder , 'thing.ts' )
7777 const fileContent = 'const bar = 1'
7878
79- test . each ( [ true , false ] ) ( 'should transpile code with useESM %p' , ( useESM ) => {
79+ test . each ( [
80+ {
81+ useESM : true ,
82+ babelConfig : true ,
83+ supportsStaticESM : false ,
84+ } ,
85+ {
86+ useESM : true ,
87+ babelConfig : false ,
88+ supportsStaticESM : true ,
89+ } ,
90+ {
91+ useESM : true ,
92+ babelConfig : false ,
93+ supportsStaticESM : false ,
94+ } ,
95+ {
96+ useESM : false ,
97+ babelConfig : false ,
98+ supportsStaticESM : true ,
99+ } ,
100+ ] ) ( 'should transpile code with config %p' , ( { useESM, babelConfig, supportsStaticESM } ) => {
80101 const compiler = makeCompiler ( {
81- tsJestConfig : { ...baseTsJestConfig , isolatedModules : true , useESM } ,
102+ tsJestConfig : { ...baseTsJestConfig , isolatedModules : true , useESM, babelConfig } ,
82103 } )
83104 const transformersStub = {
84105 before : [ ] ,
85106 after : [ ] ,
86107 afterDeclarations : [ ] ,
87108 }
88109 // @ts -expect-error testing purpose
89- compiler . _ts . transpileModule = jest . fn ( ) . mockReturnValueOnce ( {
110+ const transpileMock = ( compiler . _ts . transpileModule = jest . fn ( ) . mockReturnValueOnce ( {
90111 sourceMapText : '{}' ,
91112 outputText : 'var bar = 1' ,
92113 diagnostics : [ ] ,
93- } as TranspileOutput )
114+ } as TranspileOutput ) )
94115 // @ts -expect-error testing purpose
95116 compiler . _makeTransformers = jest . fn ( ) . mockReturnValueOnce ( transformersStub )
96117 compiler . getCompiledOutput ( fileContent , fileName , {
97118 depGraphs : new Map ( ) ,
98- supportsStaticESM : true ,
119+ supportsStaticESM,
99120 watchMode : false ,
100121 } )
101- // @ts -expect-error testing purpose
102- const compilerOptions = compiler . _compilerOptions
103122
104- // @ts -expect-error testing purpose
105- expect ( compiler . _ts . transpileModule ) . toHaveBeenCalledWith ( fileContent , {
106- fileName,
107- compilerOptions : {
108- ...compilerOptions ,
109- module : useESM ? ModuleKind . ESNext : ModuleKind . CommonJS ,
110- target : useESM ? ScriptTarget . ES2015 : compilerOptions . target ,
111- esModuleInterop : useESM ? true : compilerOptions . esModuleInterop ,
112- allowSyntheticDefaultImports : useESM ? true : compilerOptions . allowSyntheticDefaultImports ,
113- } ,
114- transformers : transformersStub ,
115- reportDiagnostics : compiler . configSet . shouldReportDiagnostics ( fileName ) ,
116- } )
123+ const usedCompilerOptions = transpileMock . mock . calls [ 0 ] [ 1 ] . compilerOptions as CompilerOptions
124+ expect ( transpileMock ) . toHaveBeenCalled ( )
125+ expect ( {
126+ module : usedCompilerOptions . module ,
127+ esModuleInterop : usedCompilerOptions . esModuleInterop ,
128+ allowSyntheticDefaultImports : usedCompilerOptions . allowSyntheticDefaultImports ,
129+ } ) . toMatchSnapshot ( )
117130 } )
118131
119132 test . each ( [ true , false ] ) ( 'should report diagnostics if shouldReportDiagnostics is %p' , ( shouldReport ) => {
@@ -163,9 +176,30 @@ describe('TsCompiler', () => {
163176 const jsOutput = 'var bar = 1'
164177 const sourceMap = '{}'
165178
166- test . each ( [ true , false ] ) ( 'should compile codes with useESM %p' , ( useESM ) => {
179+ test . each ( [
180+ {
181+ useESM : true ,
182+ babelConfig : true ,
183+ supportsStaticESM : false ,
184+ } ,
185+ {
186+ useESM : true ,
187+ babelConfig : false ,
188+ supportsStaticESM : true ,
189+ } ,
190+ {
191+ useESM : true ,
192+ babelConfig : false ,
193+ supportsStaticESM : false ,
194+ } ,
195+ {
196+ useESM : false ,
197+ babelConfig : false ,
198+ supportsStaticESM : true ,
199+ } ,
200+ ] ) ( 'should compile codes with useESM %p' , ( { useESM, babelConfig, supportsStaticESM } ) => {
167201 const configSet = createConfigSet ( {
168- tsJestConfig : { ...baseTsJestConfig , useESM } ,
202+ tsJestConfig : { ...baseTsJestConfig , useESM, babelConfig } ,
169203 } )
170204 const emptyFile = join ( mockFolder , 'empty.ts' )
171205 configSet . parsedTsConfig . fileNames . push ( emptyFile )
@@ -180,22 +214,17 @@ describe('TsCompiler', () => {
180214
181215 const output = compiler . getCompiledOutput ( fileContent , fileName , {
182216 depGraphs : new Map ( ) ,
183- supportsStaticESM : true ,
217+ supportsStaticESM,
184218 watchMode : false ,
185219 } )
186220
187221 // @ts -expect-error testing purpose
188- const compileTarget = compiler . _compilerOptions . target
189- // @ts -expect-error testing purpose
190- const moduleKind = compiler . _compilerOptions . module
191- // @ts -expect-error testing purpose
192- const esModuleInterop = compiler . _compilerOptions . esModuleInterop
193- // @ts -expect-error testing purpose
194- const allowSyntheticDefaultImports = compiler . _compilerOptions . allowSyntheticDefaultImports
195- expect ( compileTarget ) . toEqual ( useESM ? ScriptTarget . ES2015 : compileTarget )
196- expect ( moduleKind ) . toEqual ( useESM ? ModuleKind . ESNext : moduleKind )
197- expect ( esModuleInterop ) . toEqual ( useESM ? true : esModuleInterop )
198- expect ( allowSyntheticDefaultImports ) . toEqual ( useESM ? true : allowSyntheticDefaultImports )
222+ const usedCompilerOptions = compiler . _compilerOptions
223+ expect ( {
224+ module : usedCompilerOptions . module ,
225+ esModuleInterop : usedCompilerOptions . esModuleInterop ,
226+ allowSyntheticDefaultImports : usedCompilerOptions . allowSyntheticDefaultImports ,
227+ } ) . toMatchSnapshot ( )
199228 expect ( output ) . toEqual ( updateOutput ( jsOutput , fileName , sourceMap ) )
200229
201230 // @ts -expect-error testing purpose
0 commit comments