Skip to content

Commit 107e062

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

File tree

11 files changed

+144
-92
lines changed

11 files changed

+144
-92
lines changed

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

Lines changed: 20 additions & 20 deletions
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

Lines changed: 1 addition & 5 deletions
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

Lines changed: 14 additions & 13 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 19 additions & 13 deletions
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

Lines changed: 21 additions & 9 deletions
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
",

0 commit comments

Comments
 (0)