Skip to content

Commit 7468e51

Browse files
authored
refactor(config): refactor ConfigSet class (#2050)
- Convert most of getters to private fields - Optimize logic of mapping user config to internal config - Expose a few getters as public apis - Rename interface `TsJestConfig$diagnostics` to `TsJestDiagnosticsCfg` - Change `CachedConfigSet` to cache transformerConfig string instead of object type
1 parent dee15ff commit 7468e51

12 files changed

Lines changed: 1313 additions & 1799 deletions

File tree

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

Lines changed: 101 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,21 @@ Array [
88
"[level:20] checking version of jest: OK",
99
"[level:20] created new transformer",
1010
"[level:30] no matching config-set found, creating a new one",
11-
"[level:20] backporting config",
12-
"[level:20] normalized jest config",
13-
"[level:20] normalized ts-jest config",
14-
"[level:20] babel is disabled",
1511
"[level:20] loaded module typescript",
1612
"[level:20] patching typescript",
1713
"[level:20] checking version of typescript: OK",
14+
"[level:20] normalized compiler module config via ts-jest option",
15+
"[level:20] backporting config",
16+
"[level:20] normalized jest config",
17+
"[level:20] babel is disabled",
18+
"[level:20] normalized diagnostics config via ts-jest option",
1819
"[level:20] readTsConfig(): reading <cwd>/tsconfig.json",
19-
"[level:20] normalized typescript config",
20+
"[level:20] normalized typescript config via ts-jest option",
21+
"[level:20] normalized custom AST transformers via ts-jest option",
22+
"[level:20] file caching disabled",
2023
"[level:20] computing cache key for <cwd>/Hello.spec.ts",
2124
"[level:20] processing <cwd>/Hello.spec.ts",
2225
"[level:20] initializeLanguageServiceInstance(): create typescript compiler",
23-
"[level:20] file caching disabled",
2426
"[level:20] initializeLanguageServiceInstance(): creating language service",
2527
"[level:20] compileAndUpdateOutput(): get compile output",
2628
"[level:20] compileFn(): compiling using language service",
@@ -45,23 +47,25 @@ Array [
4547
"[level:20] checking version of jest: OK",
4648
"[level:20] created new transformer",
4749
"[level:30] no matching config-set found, creating a new one",
48-
"[level:20] backporting config",
49-
"[level:20] normalized jest config",
50-
"[level:20] normalized ts-jest config",
51-
"[level:20] normalized babel config via ts-jest option",
5250
"[level:20] loaded module typescript",
5351
"[level:20] patching typescript",
5452
"[level:20] checking version of typescript: OK",
55-
"[level:20] readTsConfig(): reading <cwd>/tsconfig.json",
56-
"[level:20] normalized typescript config",
57-
"[level:20] computing cache key for <cwd>/Hello.spec.ts",
58-
"[level:20] processing <cwd>/Hello.spec.ts",
59-
"[level:20] creating babel-jest transformer",
53+
"[level:20] normalized compiler module config via ts-jest option",
54+
"[level:20] backporting config",
55+
"[level:20] normalized jest config",
56+
"[level:20] normalized babel config via ts-jest option",
6057
"[level:20] loaded module babel-jest",
6158
"[level:20] patching babel-jest",
6259
"[level:20] checking version of babel-jest: OK",
63-
"[level:20] initializeLanguageServiceInstance(): create typescript compiler",
60+
"[level:20] created babel-jest transformer",
61+
"[level:20] normalized diagnostics config via ts-jest option",
62+
"[level:20] readTsConfig(): reading <cwd>/tsconfig.json",
63+
"[level:20] normalized typescript config via ts-jest option",
64+
"[level:20] normalized custom AST transformers via ts-jest option",
6465
"[level:20] file caching disabled",
66+
"[level:20] computing cache key for <cwd>/Hello.spec.ts",
67+
"[level:20] processing <cwd>/Hello.spec.ts",
68+
"[level:20] initializeLanguageServiceInstance(): create typescript compiler",
6569
"[level:20] initializeLanguageServiceInstance(): creating language service",
6670
"[level:20] compileAndUpdateOutput(): get compile output",
6771
"[level:20] compileFn(): compiling using language service",
@@ -88,24 +92,26 @@ Array [
8892
"[level:20] checking version of jest: OK",
8993
"[level:20] created new transformer",
9094
"[level:30] no matching config-set found, creating a new one",
95+
"[level:20] loaded module typescript",
96+
"[level:20] patching typescript",
97+
"[level:20] checking version of typescript: OK",
98+
"[level:20] normalized compiler module config via ts-jest option",
9199
"[level:20] backporting config",
92100
"[level:20] normalized jest config",
93101
"[level:20] resolved path from babel.config.js to <cwd>/babel.config.js",
94-
"[level:20] normalized ts-jest config",
95102
"[level:20] normalized babel config via ts-jest option",
96-
"[level:20] loaded module typescript",
97-
"[level:20] patching typescript",
98-
"[level:20] checking version of typescript: OK",
99-
"[level:20] readTsConfig(): reading <cwd>/tsconfig.json",
100-
"[level:20] normalized typescript config",
101-
"[level:20] computing cache key for <cwd>/Hello.spec.ts",
102-
"[level:20] processing <cwd>/Hello.spec.ts",
103-
"[level:20] creating babel-jest transformer",
104103
"[level:20] loaded module babel-jest",
105104
"[level:20] patching babel-jest",
106105
"[level:20] checking version of babel-jest: OK",
107-
"[level:20] initializeLanguageServiceInstance(): create typescript compiler",
106+
"[level:20] created babel-jest transformer",
107+
"[level:20] normalized diagnostics config via ts-jest option",
108+
"[level:20] readTsConfig(): reading <cwd>/tsconfig.json",
109+
"[level:20] normalized typescript config via ts-jest option",
110+
"[level:20] normalized custom AST transformers via ts-jest option",
108111
"[level:20] file caching disabled",
112+
"[level:20] computing cache key for <cwd>/Hello.spec.ts",
113+
"[level:20] processing <cwd>/Hello.spec.ts",
114+
"[level:20] initializeLanguageServiceInstance(): create typescript compiler",
109115
"[level:20] initializeLanguageServiceInstance(): creating language service",
110116
"[level:20] compileAndUpdateOutput(): get compile output",
111117
"[level:20] compileFn(): compiling using language service",
@@ -124,12 +130,13 @@ Array [
124130
]
125131
`;
126132
127-
exports[`ts-jest logging deprecation warning with astTransformers config as an object should pass using template "default" 1`] = `
133+
exports[`ts-jest logging deprecation warning with astTransformers config as string array should pass using template "default" 1`] = `
128134
√ jest
129135
↳ exit code: 0
130136
===[ STDOUT ]===================================================================
131137
132138
===[ STDERR ]===================================================================
139+
ts-jest[config] (WARN) The configuration for astTransformers as string[] is deprecated and will be removed in ts-jest 27. Please define your custom AST transformers in a form of an object. More information you can check online documentation https://kulshekhar.github.io/ts-jest/user/config/astTransformers
133140
PASS ./Hello.spec.ts
134141
Hello Class
135142
√ should create a new Hello
@@ -142,12 +149,13 @@ exports[`ts-jest logging deprecation warning with astTransformers config as an o
142149
================================================================================
143150
`;
144151
145-
exports[`ts-jest logging deprecation warning with astTransformers config as an object should pass using template "with-babel-7" 1`] = `
152+
exports[`ts-jest logging deprecation warning with astTransformers config as string array should pass using template "with-babel-7" 1`] = `
146153
√ jest
147154
↳ exit code: 0
148155
===[ STDOUT ]===================================================================
149156
150157
===[ STDERR ]===================================================================
158+
ts-jest[config] (WARN) The configuration for astTransformers as string[] is deprecated and will be removed in ts-jest 27. Please define your custom AST transformers in a form of an object. More information you can check online documentation https://kulshekhar.github.io/ts-jest/user/config/astTransformers
151159
PASS ./Hello.spec.ts
152160
Hello Class
153161
√ should create a new Hello
@@ -160,12 +168,13 @@ exports[`ts-jest logging deprecation warning with astTransformers config as an o
160168
================================================================================
161169
`;
162170
163-
exports[`ts-jest logging deprecation warning with astTransformers config as an object should pass using template "with-babel-7-string-config" 1`] = `
171+
exports[`ts-jest logging deprecation warning with astTransformers config as string array should pass using template "with-babel-7-string-config" 1`] = `
164172
√ jest
165173
↳ exit code: 0
166174
===[ STDOUT ]===================================================================
167175
168176
===[ STDERR ]===================================================================
177+
ts-jest[config] (WARN) The configuration for astTransformers as string[] is deprecated and will be removed in ts-jest 27. Please define your custom AST transformers in a form of an object. More information you can check online documentation https://kulshekhar.github.io/ts-jest/user/config/astTransformers
169178
PASS ./Hello.spec.ts
170179
Hello Class
171180
√ should create a new Hello
@@ -178,13 +187,13 @@ exports[`ts-jest logging deprecation warning with astTransformers config as an o
178187
================================================================================
179188
`;
180189
181-
exports[`ts-jest logging deprecation warning with astTransformers config as string array should pass using template "default" 1`] = `
190+
exports[`ts-jest logging deprecation warning with packageJson config should pass using template "default" 1`] = `
182191
√ jest
183192
↳ exit code: 0
184193
===[ STDOUT ]===================================================================
185194
186195
===[ STDERR ]===================================================================
187-
ts-jest[config] (WARN) The configuration for astTransformers as string[] is deprecated and will be removed in ts-jest 27. Please define your custom AST transformers in a form of an object. More information you can check online documentation https://kulshekhar.github.io/ts-jest/user/config/astTransformers
196+
ts-jest[config] (WARN) The option \`packageJson\` is deprecated and will be removed in ts-jest 27. This option is not used by internal \`ts-jest\`
188197
PASS ./Hello.spec.ts
189198
Hello Class
190199
√ should create a new Hello
@@ -197,13 +206,13 @@ exports[`ts-jest logging deprecation warning with astTransformers config as stri
197206
================================================================================
198207
`;
199208
200-
exports[`ts-jest logging deprecation warning with astTransformers config as string array should pass using template "with-babel-7" 1`] = `
209+
exports[`ts-jest logging deprecation warning with packageJson config should pass using template "with-babel-7" 1`] = `
201210
√ jest
202211
↳ exit code: 0
203212
===[ STDOUT ]===================================================================
204213
205214
===[ STDERR ]===================================================================
206-
ts-jest[config] (WARN) The configuration for astTransformers as string[] is deprecated and will be removed in ts-jest 27. Please define your custom AST transformers in a form of an object. More information you can check online documentation https://kulshekhar.github.io/ts-jest/user/config/astTransformers
215+
ts-jest[config] (WARN) The option \`packageJson\` is deprecated and will be removed in ts-jest 27. This option is not used by internal \`ts-jest\`
207216
PASS ./Hello.spec.ts
208217
Hello Class
209218
√ should create a new Hello
@@ -216,13 +225,70 @@ exports[`ts-jest logging deprecation warning with astTransformers config as stri
216225
================================================================================
217226
`;
218227
219-
exports[`ts-jest logging deprecation warning with astTransformers config as string array should pass using template "with-babel-7-string-config" 1`] = `
228+
exports[`ts-jest logging deprecation warning with packageJson config should pass using template "with-babel-7-string-config" 1`] = `
220229
√ jest
221230
↳ exit code: 0
222231
===[ STDOUT ]===================================================================
223232
224233
===[ STDERR ]===================================================================
225-
ts-jest[config] (WARN) The configuration for astTransformers as string[] is deprecated and will be removed in ts-jest 27. Please define your custom AST transformers in a form of an object. More information you can check online documentation https://kulshekhar.github.io/ts-jest/user/config/astTransformers
234+
ts-jest[config] (WARN) The option \`packageJson\` is deprecated and will be removed in ts-jest 27. This option is not used by internal \`ts-jest\`
235+
PASS ./Hello.spec.ts
236+
Hello Class
237+
√ should create a new Hello
238+
239+
Test Suites: 1 passed, 1 total
240+
Tests: 1 passed, 1 total
241+
Snapshots: 0 total
242+
Time: XXs
243+
Ran all test suites.
244+
================================================================================
245+
`;
246+
247+
exports[`ts-jest logging deprecation warning with tsConfig config should pass using template "default" 1`] = `
248+
√ jest
249+
↳ exit code: 0
250+
===[ STDOUT ]===================================================================
251+
252+
===[ STDERR ]===================================================================
253+
ts-jest[config] (WARN) The option \`tsConfig\` is deprecated and will be removed in ts-jest 27, use \`tsconfig\` instead
254+
PASS ./Hello.spec.ts
255+
Hello Class
256+
√ should create a new Hello
257+
258+
Test Suites: 1 passed, 1 total
259+
Tests: 1 passed, 1 total
260+
Snapshots: 0 total
261+
Time: XXs
262+
Ran all test suites.
263+
================================================================================
264+
`;
265+
266+
exports[`ts-jest logging deprecation warning with tsConfig config should pass using template "with-babel-7" 1`] = `
267+
√ jest
268+
↳ exit code: 0
269+
===[ STDOUT ]===================================================================
270+
271+
===[ STDERR ]===================================================================
272+
ts-jest[config] (WARN) The option \`tsConfig\` is deprecated and will be removed in ts-jest 27, use \`tsconfig\` instead
273+
PASS ./Hello.spec.ts
274+
Hello Class
275+
√ should create a new Hello
276+
277+
Test Suites: 1 passed, 1 total
278+
Tests: 1 passed, 1 total
279+
Snapshots: 0 total
280+
Time: XXs
281+
Ran all test suites.
282+
================================================================================
283+
`;
284+
285+
exports[`ts-jest logging deprecation warning with tsConfig config should pass using template "with-babel-7-string-config" 1`] = `
286+
√ jest
287+
↳ exit code: 0
288+
===[ STDOUT ]===================================================================
289+
290+
===[ STDERR ]===================================================================
291+
ts-jest[config] (WARN) The option \`tsConfig\` is deprecated and will be removed in ts-jest 27, use \`tsconfig\` instead
226292
PASS ./Hello.spec.ts
227293
Hello Class
228294
√ should create a new Hello

e2e/__tests__/logger.test.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { LogContexts, LogLevels } from 'bs-logger'
22
import { existsSync } from 'fs'
33

4-
import { PackageSets, allValidPackageSets } from '../__helpers__/templates'
4+
import { PackageSets, allValidPackageSets, allPackageSetsWithPreset } from '../__helpers__/templates'
55
import { configureTestCase } from '../__helpers__/test-case'
66

77
describe('ts-jest logging', () => {
@@ -108,14 +108,30 @@ describe('ts-jest logging', () => {
108108
})
109109
})
110110

111-
describe('with astTransformers config as an object', () => {
111+
describe('with packageJson config', () => {
112112
const testCase = configureTestCase('simple', {
113113
tsJestConfig: {
114-
astTransformers: {}
114+
packageJson: true,
115115
}
116116
})
117117

118-
testCase.runWithTemplates(allValidPackageSets, 0, (runTest, { testLabel }) => {
118+
testCase.runWithTemplates(allPackageSetsWithPreset, 0, (runTest, { testLabel }) => {
119+
it(testLabel, () => {
120+
const result = runTest()
121+
expect(result.status).toBe(0)
122+
expect(result).toMatchSnapshot()
123+
})
124+
})
125+
})
126+
127+
describe('with tsConfig config', () => {
128+
const testCase = configureTestCase('simple', {
129+
tsJestConfig: {
130+
tsConfig: true,
131+
}
132+
})
133+
134+
testCase.runWithTemplates(allPackageSetsWithPreset, 0, (runTest, { testLabel }) => {
119135
it(testLabel, () => {
120136
const result = runTest()
121137
expect(result.status).toBe(0)

src/compiler/instance.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,14 @@ export const createCompilerInstance = (configs: ConfigSet): TsCompiler => {
6262
const logger = configs.logger.child({ namespace: 'ts-compiler' })
6363
const {
6464
parsedTsConfig: { options: compilerOptions },
65-
tsJest,
6665
} = configs
6766
const extensions = ['.ts', '.tsx']
6867
// Enable `allowJs` when flag is set.
6968
if (compilerOptions.allowJs) {
7069
extensions.push('.js')
7170
extensions.push('.jsx')
7271
}
73-
const compilerInstance: CompilerInstance = !tsJest.isolatedModules
72+
const compilerInstance: CompilerInstance = !configs.isolatedModules
7473
? initializeLanguageServiceInstance(configs, logger) // Use language services by default
7574
: initializeTranspilerInstance(configs, logger)
7675
const compile = compileAndUpdateOutput(compilerInstance.compileFn, logger)

src/compiler/language-service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function doTypeChecking(
3434
service: _ts.LanguageService,
3535
logger: Logger,
3636
): void {
37-
if (configs.shouldReportDiagnostic(fileName)) {
37+
if (configs.shouldReportDiagnostics(fileName)) {
3838
// Get the relevant diagnostics - this is 3x faster than `getPreEmitDiagnostics`.
3939
const diagnostics = service.getSemanticDiagnostics(fileName).concat(service.getSyntacticDiagnostics(fileName))
4040
diagnosedFiles.push(fileName)
@@ -200,7 +200,7 @@ export const initializeLanguageServiceInstance = (configs: ConfigSet, logger: Lo
200200
getCurrentDirectory: () => cwd,
201201
getCompilationSettings: () => options,
202202
getDefaultLibFileName: () => ts.getDefaultLibFilePath(options),
203-
getCustomTransformers: () => configs.tsCustomTransformers,
203+
getCustomTransformers: () => configs.customTransformers,
204204
resolveModuleNames,
205205
}
206206

src/compiler/transpiler.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ export const initializeTranspilerInstance = (configs: ConfigSet, logger: Logger)
1919

2020
const result: _ts.TranspileOutput = ts.transpileModule(code, {
2121
fileName,
22-
transformers: configs.tsCustomTransformers,
22+
transformers: configs.customTransformers,
2323
compilerOptions: options,
24-
reportDiagnostics: configs.shouldReportDiagnostic(fileName),
24+
reportDiagnostics: configs.shouldReportDiagnostics(fileName),
2525
})
26-
if (result.diagnostics && configs.shouldReportDiagnostic(fileName)) {
26+
if (result.diagnostics && configs.shouldReportDiagnostics(fileName)) {
2727
configs.raiseDiagnostics(result.diagnostics, fileName, logger)
2828
}
2929

0 commit comments

Comments
 (0)