1
1
import { readFileSync } from 'fs'
2
2
import { basename , join , normalize } from 'path'
3
3
4
- import { DiagnosticCategory , EmitOutput , ModuleKind , ScriptTarget , TranspileOutput } from 'typescript'
4
+ import { CompilerOptions , DiagnosticCategory , EmitOutput , TranspileOutput } from 'typescript'
5
5
6
6
import { createConfigSet , makeCompiler } from '../__helpers__/fakers'
7
7
import { mockFolder } from '../__helpers__/path'
@@ -76,44 +76,57 @@ describe('TsCompiler', () => {
76
76
const fileName = join ( mockFolder , 'thing.ts' )
77
77
const fileContent = 'const bar = 1'
78
78
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 } ) => {
80
101
const compiler = makeCompiler ( {
81
- tsJestConfig : { ...baseTsJestConfig , isolatedModules : true , useESM } ,
102
+ tsJestConfig : { ...baseTsJestConfig , isolatedModules : true , useESM, babelConfig } ,
82
103
} )
83
104
const transformersStub = {
84
105
before : [ ] ,
85
106
after : [ ] ,
86
107
afterDeclarations : [ ] ,
87
108
}
88
109
// @ts -expect-error testing purpose
89
- compiler . _ts . transpileModule = jest . fn ( ) . mockReturnValueOnce ( {
110
+ const transpileMock = ( compiler . _ts . transpileModule = jest . fn ( ) . mockReturnValueOnce ( {
90
111
sourceMapText : '{}' ,
91
112
outputText : 'var bar = 1' ,
92
113
diagnostics : [ ] ,
93
- } as TranspileOutput )
114
+ } as TranspileOutput ) )
94
115
// @ts -expect-error testing purpose
95
116
compiler . _makeTransformers = jest . fn ( ) . mockReturnValueOnce ( transformersStub )
96
117
compiler . getCompiledOutput ( fileContent , fileName , {
97
118
depGraphs : new Map ( ) ,
98
- supportsStaticESM : true ,
119
+ supportsStaticESM,
99
120
watchMode : false ,
100
121
} )
101
- // @ts -expect-error testing purpose
102
- const compilerOptions = compiler . _compilerOptions
103
122
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 ( )
117
130
} )
118
131
119
132
test . each ( [ true , false ] ) ( 'should report diagnostics if shouldReportDiagnostics is %p' , ( shouldReport ) => {
@@ -163,9 +176,30 @@ describe('TsCompiler', () => {
163
176
const jsOutput = 'var bar = 1'
164
177
const sourceMap = '{}'
165
178
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 } ) => {
167
201
const configSet = createConfigSet ( {
168
- tsJestConfig : { ...baseTsJestConfig , useESM } ,
202
+ tsJestConfig : { ...baseTsJestConfig , useESM, babelConfig } ,
169
203
} )
170
204
const emptyFile = join ( mockFolder , 'empty.ts' )
171
205
configSet . parsedTsConfig . fileNames . push ( emptyFile )
@@ -180,22 +214,17 @@ describe('TsCompiler', () => {
180
214
181
215
const output = compiler . getCompiledOutput ( fileContent , fileName , {
182
216
depGraphs : new Map ( ) ,
183
- supportsStaticESM : true ,
217
+ supportsStaticESM,
184
218
watchMode : false ,
185
219
} )
186
220
187
221
// @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 ( )
199
228
expect ( output ) . toEqual ( updateOutput ( jsOutput , fileName , sourceMap ) )
200
229
201
230
// @ts -expect-error testing purpose
0 commit comments