Skip to content

Commit 107e062

Browse files
authoredMar 26, 2020
fix: always do type check for all files provided to ts-jest transformer (#1450)
1 parent 1e34075 commit 107e062

11 files changed

+144
-92
lines changed
 

‎e2e/__tests__/__snapshots__/logger.test.ts.snap

+20-20
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,28 @@ Array [
1212
"[level:20] backporting config",
1313
"[level:20] normalized jest config",
1414
"[level:20] normalized ts-jest config",
15-
"[level:20] babel is disabled",
1615
"[level:20] loaded module typescript",
1716
"[level:20] patching typescript",
1817
"[level:20] checking version of typescript: OK",
1918
"[level:20] readTsConfig(): reading <cwd>/tsconfig.json",
2019
"[level:20] normalized typescript config",
21-
"[level:20] processing <cwd>/Hello.spec.ts",
2220
"[level:20] file caching disabled",
2321
"[level:20] compileUsingLanguageService(): create typescript compiler",
2422
"[level:20] compileUsingLanguageService(): creating language service",
23+
"[level:20] compileFn(): computing diagnostics for language service",
24+
"[level:20] babel is disabled",
25+
"[level:20] processing <cwd>/Hello.spec.ts",
2526
"[level:20] readThrough(): no cache",
2627
"[level:20] compileFn(): compiling using language service",
2728
"[level:20] updateMemoryCache(): update memory cache for language service",
2829
"[level:20] visitSourceFileNode(): hoisting",
29-
"[level:20] compileFn(): computing diagnostics for language service",
3030
"[level:20] computing cache key for <cwd>/Hello.ts",
31+
"[level:20] compileFn(): computing diagnostics for language service",
3132
"[level:20] processing <cwd>/Hello.ts",
3233
"[level:20] readThrough(): no cache",
3334
"[level:20] compileFn(): compiling using language service",
3435
"[level:20] updateMemoryCache(): update memory cache for language service",
3536
"[level:20] visitSourceFileNode(): hoisting",
36-
"[level:20] compileFn(): computing diagnostics for language service",
3737
]
3838
`;
3939
@@ -49,33 +49,33 @@ Array [
4949
"[level:20] backporting config",
5050
"[level:20] normalized jest config",
5151
"[level:20] normalized ts-jest config",
52-
"[level:20] normalized babel config via ts-jest option",
5352
"[level:20] loaded module typescript",
5453
"[level:20] patching typescript",
5554
"[level:20] checking version of typescript: OK",
5655
"[level:20] readTsConfig(): reading <cwd>/tsconfig.json",
5756
"[level:20] normalized typescript config",
57+
"[level:20] file caching disabled",
58+
"[level:20] compileUsingLanguageService(): create typescript compiler",
59+
"[level:20] compileUsingLanguageService(): creating language service",
60+
"[level:20] compileFn(): computing diagnostics for language service",
61+
"[level:20] normalized babel config via ts-jest option",
5862
"[level:20] processing <cwd>/Hello.spec.ts",
5963
"[level:20] creating babel-jest transformer",
6064
"[level:20] loaded module babel-jest",
6165
"[level:20] patching babel-jest",
6266
"[level:20] checking version of babel-jest: OK",
63-
"[level:20] file caching disabled",
64-
"[level:20] compileUsingLanguageService(): create typescript compiler",
65-
"[level:20] compileUsingLanguageService(): creating language service",
6667
"[level:20] readThrough(): no cache",
6768
"[level:20] compileFn(): compiling using language service",
6869
"[level:20] updateMemoryCache(): update memory cache for language service",
6970
"[level:20] visitSourceFileNode(): hoisting",
70-
"[level:20] compileFn(): computing diagnostics for language service",
7171
"[level:20] calling babel-jest processor",
7272
"[level:20] computing cache key for <cwd>/Hello.ts",
73+
"[level:20] compileFn(): computing diagnostics for language service",
7374
"[level:20] processing <cwd>/Hello.ts",
7475
"[level:20] readThrough(): no cache",
7576
"[level:20] compileFn(): compiling using language service",
7677
"[level:20] updateMemoryCache(): update memory cache for language service",
7778
"[level:20] visitSourceFileNode(): hoisting",
78-
"[level:20] compileFn(): computing diagnostics for language service",
7979
"[level:20] calling babel-jest processor",
8080
]
8181
`;
@@ -93,33 +93,33 @@ Array [
9393
"[level:20] normalized jest config",
9494
"[level:20] resolved path from babel.config.js to <cwd>/babel.config.js",
9595
"[level:20] normalized ts-jest config",
96-
"[level:20] normalized babel config via ts-jest option",
9796
"[level:20] loaded module typescript",
9897
"[level:20] patching typescript",
9998
"[level:20] checking version of typescript: OK",
10099
"[level:20] readTsConfig(): reading <cwd>/tsconfig.json",
101100
"[level:20] normalized typescript config",
101+
"[level:20] file caching disabled",
102+
"[level:20] compileUsingLanguageService(): create typescript compiler",
103+
"[level:20] compileUsingLanguageService(): creating language service",
104+
"[level:20] compileFn(): computing diagnostics for language service",
105+
"[level:20] normalized babel config via ts-jest option",
102106
"[level:20] processing <cwd>/Hello.spec.ts",
103107
"[level:20] creating babel-jest transformer",
104108
"[level:20] loaded module babel-jest",
105109
"[level:20] patching babel-jest",
106110
"[level:20] checking version of babel-jest: OK",
107-
"[level:20] file caching disabled",
108-
"[level:20] compileUsingLanguageService(): create typescript compiler",
109-
"[level:20] compileUsingLanguageService(): creating language service",
110111
"[level:20] readThrough(): no cache",
111112
"[level:20] compileFn(): compiling using language service",
112113
"[level:20] updateMemoryCache(): update memory cache for language service",
113114
"[level:20] visitSourceFileNode(): hoisting",
114-
"[level:20] compileFn(): computing diagnostics for language service",
115115
"[level:20] calling babel-jest processor",
116116
"[level:20] computing cache key for <cwd>/Hello.ts",
117+
"[level:20] compileFn(): computing diagnostics for language service",
117118
"[level:20] processing <cwd>/Hello.ts",
118119
"[level:20] readThrough(): no cache",
119120
"[level:20] compileFn(): compiling using language service",
120121
"[level:20] updateMemoryCache(): update memory cache for language service",
121122
"[level:20] visitSourceFileNode(): hoisting",
122-
"[level:20] compileFn(): computing diagnostics for language service",
123123
"[level:20] calling babel-jest processor",
124124
]
125125
`;
@@ -136,28 +136,28 @@ Array [
136136
"[level:20] backporting config",
137137
"[level:20] normalized jest config",
138138
"[level:20] normalized ts-jest config",
139-
"[level:20] babel is disabled",
140139
"[level:20] loaded module typescript",
141140
"[level:20] patching typescript",
142141
"[level:20] checking version of typescript: OK",
143142
"[level:20] readTsConfig(): reading <cwd>/tsconfig.json",
144143
"[level:20] normalized typescript config",
145-
"[level:20] processing <cwd>/Hello.spec.ts",
146144
"[level:20] file caching disabled",
147145
"[level:20] compileUsingLanguageService(): create typescript compiler",
148146
"[level:20] compileUsingLanguageService(): creating language service",
147+
"[level:20] compileFn(): computing diagnostics for language service",
148+
"[level:20] babel is disabled",
149+
"[level:20] processing <cwd>/Hello.spec.ts",
149150
"[level:20] readThrough(): no cache",
150151
"[level:20] compileFn(): compiling using language service",
151152
"[level:20] updateMemoryCache(): update memory cache for language service",
152153
"[level:20] visitSourceFileNode(): hoisting",
153-
"[level:20] compileFn(): computing diagnostics for language service",
154154
"[level:20] computing cache key for <cwd>/Hello.ts",
155+
"[level:20] compileFn(): computing diagnostics for language service",
155156
"[level:20] processing <cwd>/Hello.ts",
156157
"[level:20] readThrough(): no cache",
157158
"[level:20] compileFn(): compiling using language service",
158159
"[level:20] updateMemoryCache(): update memory cache for language service",
159160
"[level:20] visitSourceFileNode(): hoisting",
160-
"[level:20] compileFn(): computing diagnostics for language service",
161161
]
162162
`;
163163

‎src/compiler/__snapshots__/program.spec.ts.snap

+1-5
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,6 @@ exports[`other jsx options should compile tsx file for with program 1`] = `
146146
================================================================================
147147
`;
148148

149-
exports[`typings incremental program should not report diagnostics with pathRegex config does not match file name 1`] = `"test-typings.ts: Emit skipped"`;
150-
151-
exports[`typings incremental program should report diagnostics with pathRegex config matches file name 1`] = `"test-typings.ts: Emit skipped"`;
152-
153-
exports[`typings normal program should not report diagnostics with pathRegex config matches file name 1`] = `"test-typings.ts: Emit skipped"`;
149+
exports[`typings incremental program should report diagnostics with pathRegex config matches file name 1`] = `"test-typings.ts(3,7): error TS2322: Type 'number' is not assignable to type 'string'."`;
154150

155151
exports[`typings normal program should report diagnostics with pathRegex config matches file name 1`] = `"test-typings.ts(3,7): error TS2322: Type 'number' is not assignable to type 'string'."`;

‎src/compiler/instance.ts

+14-13
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import mkdirp = require('mkdirp')
3535
import { basename, extname, join, normalize } from 'path'
3636

3737
import { ConfigSet } from '../config/config-set'
38-
import { CompileFn, CompileResult, MemoryCache, TsCompiler } from '../types'
38+
import { CompileFn, CompilerInstance, MemoryCache, TsCompiler } from '../types'
3939
import { sha1 } from '../util/sha1'
4040

4141
import { compileUsingLanguageService } from './language-service'
@@ -113,11 +113,10 @@ const readThrough = (
113113
mkdirp.sync(cachedir)
114114

115115
return (code: string, fileName: string, lineOffset?: number) => {
116-
const normalizedFileName = normalize(fileName)
117-
const cachePath = join(cachedir, getCacheName(code, normalizedFileName))
118-
const extension = getExtension(normalizedFileName)
119-
const outputPath = `${cachePath}${extension}`
120-
116+
const normalizedFileName = normalize(fileName),
117+
cachePath = join(cachedir, getCacheName(code, normalizedFileName)),
118+
extension = getExtension(normalizedFileName),
119+
outputPath = `${cachePath}${extension}`
121120
try {
122121
const output = readFileSync(outputPath, 'utf8')
123122
if (isValidCacheContent(output)) {
@@ -129,10 +128,12 @@ const readThrough = (
129128
} catch (err) {}
130129

131130
logger.debug({ fileName }, 'readThrough(): cache miss')
132-
const [value, sourceMap] = compileFn(code, normalizedFileName, lineOffset)
133-
const output = updateOutput(value, normalizedFileName, sourceMap, getExtension)
131+
132+
const [value, sourceMap] = compileFn(code, normalizedFileName, lineOffset),
133+
output = updateOutput(value, normalizedFileName, sourceMap, getExtension)
134134

135135
logger.debug({ normalizedFileName, outputPath }, 'readThrough(): writing caches')
136+
136137
memoryCache.outputs[normalizedFileName] = output
137138
writeFileSync(outputPath, output)
138139

@@ -172,16 +173,16 @@ export const createCompiler = (configs: ConfigSet): TsCompiler => {
172173
compilerOptions.jsx === ts.JsxEmit.Preserve
173174
? (path: string) => (/\.[tj]sx$/.test(path) ? '.jsx' : '.js')
174175
: (_: string) => '.js'
175-
let compileResult: CompileResult
176+
let compilerInstance: CompilerInstance
176177
if (!tsJest.isolatedModules) {
177178
// Use language services by default
178-
compileResult = !tsJest.compilerHost
179+
compilerInstance = !tsJest.compilerHost
179180
? compileUsingLanguageService(configs, logger, memoryCache)
180181
: compileUsingProgram(configs, logger, memoryCache)
181182
} else {
182-
compileResult = compileUsingTranspileModule(configs, logger)
183+
compilerInstance = compileUsingTranspileModule(configs, logger)
183184
}
184-
const compile = readThrough(cachedir, memoryCache, compileResult.compileFn, getExtension, logger)
185+
const compile = readThrough(cachedir, memoryCache, compilerInstance.compileFn, getExtension, logger)
185186

186-
return { cwd: configs.cwd, compile, program: compileResult.program }
187+
return { cwd: configs.cwd, compile, program: compilerInstance.program, diagnose: compilerInstance.diagnoseFn }
187188
}

‎src/compiler/language-service.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ describe('language service', () => {
3636
"[level:20] updateMemoryCache(): update memory cache for language service
3737
",
3838
"[level:20] visitSourceFileNode(): hoisting
39-
",
40-
"[level:20] compileFn(): computing diagnostics for language service
4139
",
4240
"[level:20] readThrough(): writing caches
4341
",
@@ -146,8 +144,9 @@ const x: string = g(5)
146144
tsJestConfig: { tsConfig: false, diagnostics: { pathRegex: fileName } },
147145
})
148146
writeFileSync(fileName, source, 'utf8')
147+
compiler.compile(source, fileName)
149148

150-
expect(() => compiler.compile(source, fileName)).toThrowErrorMatchingSnapshot()
149+
expect(() => compiler.diagnose!(fileName)).toThrowErrorMatchingSnapshot()
151150

152151
removeSync(fileName)
153152
})
@@ -162,8 +161,9 @@ const t: string = f(5)
162161
tsJestConfig: { tsConfig: false, diagnostics: { pathRegex: 'bar.ts' } },
163162
})
164163
writeFileSync(fileName, source, 'utf8')
164+
compiler.compile(source, fileName)
165165

166-
expect(() => compiler.compile(source, fileName)).not.toThrowError()
166+
expect(() => compiler.diagnose!(fileName)).not.toThrowError()
167167

168168
removeSync(fileName)
169169
})

‎src/compiler/language-service.ts

+19-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { basename, normalize, relative } from 'path'
44
import * as _ts from 'typescript'
55

66
import { ConfigSet } from '../config/config-set'
7-
import { CompileResult, MemoryCache, SourceOutput } from '../types'
7+
import { CompilerInstance, MemoryCache, SourceOutput } from '../types'
88
import { Errors, interpolate } from '../util/messages'
99

1010
const hasOwn = Object.prototype.hasOwnProperty
@@ -16,7 +16,7 @@ export const compileUsingLanguageService = (
1616
configs: ConfigSet,
1717
logger: Logger,
1818
memoryCache: MemoryCache,
19-
): CompileResult => {
19+
): CompilerInstance => {
2020
logger.debug('compileUsingLanguageService(): create typescript compiler')
2121

2222
const ts = configs.compilerModule,
@@ -95,17 +95,6 @@ export const compileUsingLanguageService = (
9595
// Must set memory cache before attempting to read file.
9696
updateMemoryCache(code, normalizedFileName)
9797
const output: _ts.EmitOutput = service.getEmitOutput(normalizedFileName)
98-
if (configs.shouldReportDiagnostic(normalizedFileName)) {
99-
logger.debug({ normalizedFileName }, 'compileFn(): computing diagnostics for language service')
100-
101-
// Get the relevant diagnostics - this is 3x faster than `getPreEmitDiagnostics`.
102-
const diagnostics = service
103-
.getCompilerOptionsDiagnostics()
104-
.concat(service.getSyntacticDiagnostics(normalizedFileName))
105-
.concat(service.getSemanticDiagnostics(normalizedFileName))
106-
// will raise or just warn diagnostics depending on config
107-
configs.raiseDiagnostics(diagnostics, normalizedFileName, logger)
108-
}
10998

11099
/* istanbul ignore next (this should never happen but is kept for security) */
111100
if (output.emitSkipped) {
@@ -123,6 +112,23 @@ export const compileUsingLanguageService = (
123112

124113
return [output.outputFiles[1].text, output.outputFiles[0].text]
125114
},
115+
diagnoseFn: (filePath: string) => {
116+
const normalizedFileName = normalize(filePath)
117+
if (configs.shouldReportDiagnostic(normalizedFileName)) {
118+
logger.debug({ normalizedFileName }, 'compileFn(): computing diagnostics for language service')
119+
120+
// Not sure why e2e tests not working without this if. In reality, this if is not needed
121+
if (service.getProgram()!.getSourceFile(filePath)) {
122+
// Get the relevant diagnostics - this is 3x faster than `getPreEmitDiagnostics`.
123+
const diagnostics = service
124+
.getCompilerOptionsDiagnostics()
125+
.concat(service.getSyntacticDiagnostics(normalizedFileName))
126+
.concat(service.getSemanticDiagnostics(normalizedFileName))
127+
// will raise or just warn diagnostics depending on config
128+
configs.raiseDiagnostics(diagnostics, normalizedFileName, logger)
129+
}
130+
}
131+
},
126132
program: service.getProgram(),
127133
}
128134
}

‎src/compiler/program.spec.ts

+21-9
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ const t: string = f(5)
3838
},
3939
})
4040

41-
expect(() => compiler.compile(source, fileName)).toThrowErrorMatchingSnapshot()
41+
try {
42+
compiler.compile(source, fileName)
43+
} catch (e) {}
44+
45+
expect(() => compiler.diagnose!(fileName)).toThrowErrorMatchingSnapshot()
4246
})
4347

4448
it('should not report diagnostics with pathRegex config matches file name', () => {
@@ -50,7 +54,11 @@ const t: string = f(5)
5054
},
5155
})
5256

53-
expect(() => compiler.compile(source, fileName)).toThrowErrorMatchingSnapshot()
57+
try {
58+
compiler.compile(source, fileName)
59+
} catch (e) {}
60+
61+
expect(() => compiler.diagnose!(fileName)).not.toThrowError()
5462
})
5563
})
5664

@@ -60,11 +68,15 @@ const t: string = f(5)
6068
tsJestConfig: {
6169
...baseTsJestConfig,
6270
incremental: true,
63-
diagnostics: { pathRegex: 'typings-error.ts' },
71+
diagnostics: { pathRegex: fileName },
6472
},
6573
})
6674

67-
expect(() => compiler.compile(source, fileName)).toThrowErrorMatchingSnapshot()
75+
try {
76+
compiler.compile(source, fileName)
77+
} catch (e) {}
78+
79+
expect(() => compiler.diagnose!(fileName)).toThrowErrorMatchingSnapshot()
6880
})
6981

7082
it('should not report diagnostics with pathRegex config does not match file name', () => {
@@ -76,7 +88,11 @@ const t: string = f(5)
7688
},
7789
})
7890

79-
expect(() => compiler.compile(source, fileName)).toThrowErrorMatchingSnapshot()
91+
try {
92+
compiler.compile(source, fileName)
93+
} catch (e) {}
94+
95+
expect(() => compiler.diagnose!(fileName)).not.toThrowError()
8096
})
8197
})
8298
})
@@ -161,8 +177,6 @@ describe('cache', () => {
161177
"[level:20] updateMemoryCache(): update memory cache for program
162178
",
163179
"[level:20] visitSourceFileNode(): hoisting
164-
",
165-
"[level:20] compileFn(): computing diagnostics for program
166180
",
167181
"[level:20] readThrough(): writing caches
168182
",
@@ -202,8 +216,6 @@ Array [
202216
"[level:20] updateMemoryCache(): update memory cache for incremental program
203217
",
204218
"[level:20] visitSourceFileNode(): hoisting
205-
",
206-
"[level:20] compileFn(): computing diagnostics for incremental program
207219
",
208220
"[level:20] readThrough(): writing caches
209221
",

‎src/compiler/program.ts

+16-14
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import { basename, normalize, relative } from 'path'
44
import * as _ts from 'typescript'
55

66
import { ConfigSet } from '../config/config-set'
7-
import { CompileResult, MemoryCache, SourceOutput } from '../types'
7+
import { CompilerInstance, MemoryCache, SourceOutput } from '../types'
88
import { Errors, interpolate } from '../util/messages'
99

1010
const hasOwn = Object.prototype.hasOwnProperty
1111

1212
/**
1313
* @internal
1414
*/
15-
export const compileUsingProgram = (configs: ConfigSet, logger: Logger, memoryCache: MemoryCache): CompileResult => {
15+
export const compileUsingProgram = (configs: ConfigSet, logger: Logger, memoryCache: MemoryCache): CompilerInstance => {
1616
logger.debug('compileUsingProgram(): create typescript compiler')
1717

1818
const ts = configs.compilerModule,
@@ -144,18 +144,6 @@ export const compileUsingProgram = (configs: ConfigSet, logger: Logger, memoryCa
144144
undefined,
145145
customTransformers,
146146
)
147-
if (configs.shouldReportDiagnostic(normalizedFileName)) {
148-
logger.debug(
149-
{ normalizedFileName },
150-
`compileFn(): computing diagnostics for ${incremental ? 'incremental program' : 'program'}`,
151-
)
152-
153-
const diagnostics = program
154-
.getSemanticDiagnostics(sourceFile)
155-
.concat(program.getSyntacticDiagnostics(sourceFile))
156-
// will raise or just warn diagnostics depending on config
157-
configs.raiseDiagnostics(diagnostics, normalizedFileName, logger)
158-
}
159147

160148
if (result.emitSkipped) {
161149
throw new TypeError(`${relative(cwd, fileName)}: Emit skipped`)
@@ -172,6 +160,20 @@ export const compileUsingProgram = (configs: ConfigSet, logger: Logger, memoryCa
172160

173161
return output
174162
},
163+
diagnoseFn: (filePath: string) => {
164+
const normalizedFileName = normalize(filePath)
165+
if (configs.shouldReportDiagnostic(normalizedFileName)) {
166+
logger.debug(
167+
{ normalizedFileName },
168+
`compileFn(): computing diagnostics for ${incremental ? 'incremental program' : 'program'}`,
169+
)
170+
171+
const sourceFile = program.getSourceFile(normalizedFileName),
172+
diagnostics = program.getSemanticDiagnostics(sourceFile).concat(program.getSyntacticDiagnostics(sourceFile))
173+
// will raise or just warn diagnostics depending on config
174+
configs.raiseDiagnostics(diagnostics, normalizedFileName, logger)
175+
}
176+
},
175177
program,
176178
}
177179
}

‎src/compiler/transpile-module.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@ import { Logger } from 'bs-logger'
22
import { normalize } from 'path'
33

44
import { ConfigSet } from '../config/config-set'
5-
import { CompileResult, SourceOutput } from '../types'
5+
import { CompilerInstance, SourceOutput } from '../types'
66

77
/**
88
* @internal
99
*/
10-
export const compileUsingTranspileModule = (configs: ConfigSet, logger: Logger): CompileResult => {
10+
export const compileUsingTranspileModule = (configs: ConfigSet, logger: Logger): CompilerInstance => {
1111
logger.debug('compileUsingTranspileModule(): create typescript compiler')
1212

1313
return {
1414
compileFn: (code: string, fileName: string): SourceOutput => {
1515
logger.debug({ fileName }, 'getOutput(): compiling as isolated module')
1616

17-
const normalizedFileName = normalize(fileName)
18-
const result = configs.compilerModule.transpileModule(code, {
19-
fileName: normalizedFileName,
20-
transformers: configs.tsCustomTransformers,
21-
compilerOptions: configs.typescript.options,
22-
reportDiagnostics: configs.shouldReportDiagnostic(normalizedFileName),
23-
})
17+
const normalizedFileName = normalize(fileName),
18+
result = configs.compilerModule.transpileModule(code, {
19+
fileName: normalizedFileName,
20+
transformers: configs.tsCustomTransformers,
21+
compilerOptions: configs.typescript.options,
22+
reportDiagnostics: configs.shouldReportDiagnostic(normalizedFileName),
23+
})
2424

2525
if (result.diagnostics && configs.shouldReportDiagnostic(normalizedFileName)) {
2626
configs.raiseDiagnostics(result.diagnostics, normalizedFileName, logger)

‎src/ts-jest-transformer.spec.ts

+33-4
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,20 @@ Array [
249249
})
250250

251251
describe('getCacheKey', () => {
252+
let tr: TsJestTransformer
253+
254+
beforeEach(() => {
255+
tr = new TsJestTransformer()
256+
})
257+
252258
it('should be different for each argument value', () => {
253-
const tr = new TsJestTransformer()
254-
jest
255-
.spyOn(tr, 'configsFor')
256-
.mockImplementation(jestConfigStr => (({ cacheKey: jestConfigStr } as unknown) as ConfigSet))
259+
jest.spyOn(tr, 'configsFor').mockImplementation(
260+
jestConfigStr =>
261+
(({
262+
cacheKey: jestConfigStr,
263+
tsCompiler: {},
264+
} as unknown) as ConfigSet),
265+
)
257266
const input = {
258267
fileContent: 'export default "foo"',
259268
fileName: 'foo.ts',
@@ -274,4 +283,24 @@ describe('getCacheKey', () => {
274283
// unique array should have same length
275284
expect(keys.filter((k, i, all) => all.indexOf(k) === i)).toHaveLength(keys.length)
276285
})
286+
287+
it('should call diagnosticsFn to do type checking for test file', () => {
288+
const tsCompilerStub = { diagnose: jest.fn() }
289+
jest.spyOn(tr, 'configsFor').mockImplementation(
290+
jestConfigStr =>
291+
(({
292+
cacheKey: jestConfigStr,
293+
tsCompiler: tsCompilerStub,
294+
} as unknown) as ConfigSet),
295+
)
296+
const input = {
297+
fileContent: 'export default "foo"',
298+
fileName: 'foo.ts',
299+
jestConfigStr: '{"foo": "bar"}',
300+
options: { instrument: false, rootDir: '/foo' },
301+
}
302+
tr.getCacheKey(input.fileContent, input.fileName, input.jestConfigStr, input.options)
303+
304+
expect(tsCompilerStub.diagnose).toHaveBeenCalledWith(input.fileName)
305+
})
277306
})

‎src/ts-jest-transformer.ts

+3
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ export class TsJestTransformer implements Transformer {
179179
const configs = this.configsFor(jestConfigStr)
180180
// we do not instrument, ensure it is false all the time
181181
const { instrument = false, rootDir = configs.rootDir } = transformOptions
182+
if (configs.tsCompiler.diagnose) {
183+
configs.tsCompiler.diagnose(filePath)
184+
}
182185

183186
return sha1(
184187
configs.cacheKey,

‎src/types.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ export type ModulePatcher<T = any> = (module: T) => T
162162
export interface TsCompiler {
163163
cwd: string
164164
compile(code: string, fileName: string, lineOffset?: number): string
165+
diagnose: DiagnoseFn | undefined
165166
program: _ts.Program | undefined
166167
}
167168

@@ -181,9 +182,11 @@ export interface MemoryCache {
181182
}
182183

183184
export type CompileFn = (code: string, fileName: string, lineOffset?: number) => SourceOutput
185+
export type DiagnoseFn = (filePath: string) => void
184186

185-
export interface CompileResult {
187+
export interface CompilerInstance {
186188
compileFn: CompileFn
189+
diagnoseFn?: DiagnoseFn
187190
program?: _ts.Program
188191
}
189192

0 commit comments

Comments
 (0)
Please sign in to comment.