Skip to content

Commit 3b6b705

Browse files
authored
feat(config): expose several typings as public apis (#2054)
- Expose several properties in `ConfigSet` - Expose `JsonableValue` and some json utils - Expose `updateOutput` function in compiler TypeScript doesn't allow subclass to use parent class getters so we have to convert public getters to public properties, see microsoft/TypeScript#338
1 parent a0e5639 commit 3b6b705

File tree

5 files changed

+83
-78
lines changed

5 files changed

+83
-78
lines changed

src/compiler/instance.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const SOURCE_MAPPING_PREFIX = 'sourceMappingURL='
1515
/**
1616
* Update the output remapping the source map.
1717
*/
18-
function updateOutput(outputText: string, normalizedFileName: string, sourceMap: string): string {
18+
export function updateOutput(outputText: string, normalizedFileName: string, sourceMap: string): string {
1919
const base64Map = Buffer.from(updateSourceMap(sourceMap, normalizedFileName), 'utf8').toString('base64')
2020
const sourceMapContent = `data:application/json;charset=utf-8;base64,${base64Map}`
2121

src/config/config-set.ts

Lines changed: 74 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,36 @@ const toDiagnosticCodeList = (items: (string | number)[], into: number[] = []):
112112

113113
export class ConfigSet {
114114
readonly logger: Logger
115-
private readonly _cwd: string
116-
private readonly _rootDir: string
115+
readonly compilerModule: TTypeScript
116+
readonly isolatedModules: boolean
117+
readonly cwd: string
118+
tsCacheDir: string | undefined
119+
parsedTsConfig!: ParsedCommandLine
120+
customTransformers: CustomTransformers = Object.create(null)
121+
readonly rootDir: string
122+
/**
123+
* @internal
124+
*/
117125
private _jestCfg!: Config.ProjectConfig
118-
private _isolatedModules!: boolean
119-
private _parsedTsConfig!: ParsedCommandLine
120-
private _customTransformers: CustomTransformers = Object.create(null)
126+
/**
127+
* @internal
128+
*/
121129
private _babelConfig: BabelConfig | undefined
130+
/**
131+
* @internal
132+
*/
122133
private _babelJestTransformers: BabelJestTransformer | undefined
134+
/**
135+
* @internal
136+
*/
123137
private _diagnostics!: TsJestDiagnosticsCfg
138+
/**
139+
* @internal
140+
*/
124141
private _stringifyContentRegExp: RegExp | undefined
125-
private readonly _compilerModule!: TTypeScript
126-
private _tsCacheDir: string | undefined
142+
/**
143+
* @internal
144+
*/
127145
private _overriddenCompilerOptions: Partial<CompilerOptions> = {
128146
// we handle sourcemaps this way and not another
129147
sourceMap: true,
@@ -146,27 +164,39 @@ export class ConfigSet {
146164
}
147165

148166
constructor(
167+
/**
168+
* @internal
169+
*/
149170
private readonly jestConfig: Config.ProjectConfig,
150-
// mainly for testing logging
171+
/**
172+
* Mainly for testing logging
173+
*
174+
* @internal
175+
*/
151176
private readonly parentLogger?: Logger,
152177
) {
153178
this.logger = this.parentLogger
154179
? this.parentLogger.child({ [LogContexts.namespace]: 'config' })
155180
: rootLogger.child({ namespace: 'config' })
156-
this._cwd = normalize(this.jestConfig.cwd ?? process.cwd())
157-
this._rootDir = normalize(this.jestConfig.rootDir ?? this._cwd)
181+
this.cwd = normalize(this.jestConfig.cwd ?? process.cwd())
182+
this.rootDir = normalize(this.jestConfig.rootDir ?? this.cwd)
158183
const tsJestCfg = this.jestConfig.globals && this.jestConfig.globals['ts-jest']
159184
const options: TsJestGlobalOptions = tsJestCfg ?? Object.create(null)
160185
// compiler module
161-
this._compilerModule = importer.typescript(ImportReasons.TsJest, options.compiler ?? 'typescript')
186+
this.compilerModule = importer.typescript(ImportReasons.TsJest, options.compiler ?? 'typescript')
187+
// isolatedModules
188+
this.isolatedModules = options.isolatedModules ?? false
162189

163-
this.logger.debug({ compilerModule: this._compilerModule }, 'normalized compiler module config via ts-jest option')
190+
this.logger.debug({ compilerModule: this.compilerModule }, 'normalized compiler module config via ts-jest option')
164191

165192
this._backportJestCfg()
166193
this._setupTsJestCfg(options)
167194
this._resolveTsCacheDir()
168195
}
169196

197+
/**
198+
* @internal
199+
*/
170200
private _backportJestCfg(): void {
171201
const config = backportJestConfig(this.logger, this.jestConfig)
172202

@@ -175,10 +205,10 @@ export class ConfigSet {
175205
this._jestCfg = config
176206
}
177207

208+
/**
209+
* @internal
210+
*/
178211
private _setupTsJestCfg(options: TsJestGlobalOptions): void {
179-
// isolatedModules
180-
this._isolatedModules = options.isolatedModules ?? false
181-
182212
if (options.packageJson) {
183213
this.logger.warn(Deprecations.PackageJson)
184214
}
@@ -187,7 +217,7 @@ export class ConfigSet {
187217
if (!options.babelConfig) {
188218
this.logger.debug('babel is disabled')
189219
} else {
190-
const baseBabelCfg = { cwd: this._cwd }
220+
const baseBabelCfg = { cwd: this.cwd }
191221
if (typeof options.babelConfig === 'string') {
192222
if (extname(options.babelConfig) === '.js') {
193223
this._babelConfig = {
@@ -251,24 +281,24 @@ export class ConfigSet {
251281
}
252282
const tsconfigOpt = options.tsConfig ?? options.tsconfig
253283
const configFilePath = typeof tsconfigOpt === 'string' ? this.resolvePath(tsconfigOpt) : undefined
254-
this._parsedTsConfig = this._readTsConfig(typeof tsconfigOpt === 'object' ? tsconfigOpt : undefined, configFilePath)
284+
this.parsedTsConfig = this._readTsConfig(typeof tsconfigOpt === 'object' ? tsconfigOpt : undefined, configFilePath)
255285
// throw errors if any matching wanted diagnostics
256-
this.raiseDiagnostics(this._parsedTsConfig.errors, configFilePath)
286+
this.raiseDiagnostics(this.parsedTsConfig.errors, configFilePath)
257287

258-
this.logger.debug({ tsconfig: this._parsedTsConfig }, 'normalized typescript config via ts-jest option')
288+
this.logger.debug({ tsconfig: this.parsedTsConfig }, 'normalized typescript config via ts-jest option')
259289

260290
// transformers
261291
const { astTransformers } = options
262-
this._customTransformers = {
292+
this.customTransformers = {
263293
before: [hoisting(this)],
264294
}
265295
if (astTransformers) {
266296
if (Array.isArray(astTransformers)) {
267297
this.logger.warn(Deprecations.AstTransformerArrayConfig)
268298

269-
this._customTransformers = {
299+
this.customTransformers = {
270300
before: [
271-
...this._customTransformers.before,
301+
...this.customTransformers.before,
272302
...astTransformers.map((transformer) => {
273303
const transformerPath = this.resolvePath(transformer, { nodeResolve: true })
274304

@@ -291,27 +321,27 @@ export class ConfigSet {
291321
}
292322
})
293323
if (astTransformers.before) {
294-
this._customTransformers = {
295-
before: [...this._customTransformers.before, ...resolveTransformers(astTransformers.before)],
324+
this.customTransformers = {
325+
before: [...this.customTransformers.before, ...resolveTransformers(astTransformers.before)],
296326
}
297327
}
298328
if (astTransformers.after) {
299-
this._customTransformers = {
300-
...this._customTransformers,
329+
this.customTransformers = {
330+
...this.customTransformers,
301331
after: resolveTransformers(astTransformers.after),
302332
}
303333
}
304334
if (astTransformers.afterDeclarations) {
305-
this._customTransformers = {
306-
...this._customTransformers,
335+
this.customTransformers = {
336+
...this.customTransformers,
307337
afterDeclarations: resolveTransformers(astTransformers.afterDeclarations),
308338
}
309339
}
310340
}
311341
}
312342

313343
this.logger.debug(
314-
{ customTransformers: this._customTransformers },
344+
{ customTransformers: this.customTransformers },
315345
'normalized custom AST transformers via ts-jest option',
316346
)
317347

@@ -329,6 +359,9 @@ export class ConfigSet {
329359
}
330360
}
331361

362+
/**
363+
* @internal
364+
*/
332365
private _resolveTsCacheDir(): void {
333366
if (!this._jestCfg.cache) {
334367
this.logger.debug('file caching disabled')
@@ -337,19 +370,19 @@ export class ConfigSet {
337370
}
338371
const cacheSuffix = sha1(
339372
stringify({
340-
version: this._compilerModule.version,
373+
version: this.compilerModule.version,
341374
digest: this.tsJestDigest,
342-
compilerModule: this._compilerModule,
343-
compilerOptions: this._parsedTsConfig.options,
344-
isolatedModules: this._isolatedModules,
375+
compilerModule: this.compilerModule,
376+
compilerOptions: this.parsedTsConfig.options,
377+
isolatedModules: this.isolatedModules,
345378
diagnostics: this._diagnostics,
346379
}),
347380
)
348381
const res = join(this._jestCfg.cacheDirectory, 'ts-jest', cacheSuffix.substr(0, 2), cacheSuffix.substr(2))
349382

350383
this.logger.debug({ cacheDirectory: res }, 'will use file caching')
351384

352-
this._tsCacheDir = res
385+
this.tsCacheDir = res
353386
}
354387

355388
/**
@@ -360,12 +393,12 @@ export class ConfigSet {
360393
*/
361394
private _readTsConfig(compilerOptions?: CompilerOptions, resolvedConfigFile?: string): ParsedCommandLine {
362395
let config = { compilerOptions: Object.create(null) }
363-
let basePath = normalizeSlashes(this._rootDir)
364-
const ts = this._compilerModule
396+
let basePath = normalizeSlashes(this.rootDir)
397+
const ts = this.compilerModule
365398
// Read project configuration when available.
366399
const configFileName: string | undefined = resolvedConfigFile
367400
? normalizeSlashes(resolvedConfigFile)
368-
: ts.findConfigFile(normalizeSlashes(this._rootDir), ts.sys.fileExists)
401+
: ts.findConfigFile(normalizeSlashes(this.rootDir), ts.sys.fileExists)
369402
if (configFileName) {
370403
this.logger.debug({ tsConfigFileName: configFileName }, 'readTsConfig(): reading', configFileName)
371404
const result = ts.readConfigFile(configFileName, ts.sys.readFile)
@@ -456,25 +489,9 @@ export class ConfigSet {
456489
return result
457490
}
458491

459-
get parsedTsConfig(): ParsedCommandLine {
460-
return this._parsedTsConfig
461-
}
462-
463-
get isolatedModules(): boolean {
464-
return this._isolatedModules
465-
}
466-
467492
/**
468-
* This API can be used by custom transformers
493+
* @internal
469494
*/
470-
get compilerModule(): TTypeScript {
471-
return this._compilerModule
472-
}
473-
474-
get customTransformers(): CustomTransformers {
475-
return this._customTransformers
476-
}
477-
478495
@Memoize()
479496
get tsCompiler(): TsCompiler {
480497
return createCompilerInstance(this)
@@ -494,14 +511,6 @@ export class ConfigSet {
494511
return this._babelJestTransformers
495512
}
496513

497-
get cwd(): string {
498-
return this._cwd
499-
}
500-
501-
get tsCacheDir(): string | undefined {
502-
return this._tsCacheDir
503-
}
504-
505514
/**
506515
* Use by e2e, don't mark as internal
507516
*/
@@ -518,7 +527,7 @@ export class ConfigSet {
518527
get hooks(): TsJestHooksMap {
519528
let hooksFile = process.env.TS_JEST_HOOKS
520529
if (hooksFile) {
521-
hooksFile = resolve(this._cwd, hooksFile)
530+
hooksFile = resolve(this.cwd, hooksFile)
522531

523532
return importer.tryTheseOr(hooksFile, {})
524533
}
@@ -546,16 +555,13 @@ export class ConfigSet {
546555
matchablePatterns.some((pattern) => (typeof pattern === 'string' ? isMatch(fileName) : pattern.test(fileName)))
547556
}
548557

549-
/**
550-
* @internal
551-
*/
552558
shouldStringifyContent(filePath: string): boolean {
553559
return this._stringifyContentRegExp ? this._stringifyContentRegExp.test(filePath) : false
554560
}
555561

556562
raiseDiagnostics(diagnostics: Diagnostic[], filePath?: string, logger?: Logger): void {
557563
const { ignoreCodes } = this._diagnostics
558-
const { DiagnosticCategory } = this._compilerModule
564+
const { DiagnosticCategory } = this.compilerModule
559565
const filteredDiagnostics =
560566
filePath && !this.shouldReportDiagnostics(filePath)
561567
? []
@@ -614,7 +620,7 @@ export class ConfigSet {
614620
let path: string = inputPath
615621
let nodeResolved = false
616622
if (path.startsWith('<rootDir>')) {
617-
path = resolve(join(this._rootDir, path.substr(9)))
623+
path = resolve(join(this.rootDir, path.substr(9)))
618624
} else if (!isAbsolute(path)) {
619625
if (!path.startsWith('.') && nodeResolve) {
620626
try {

src/ts-jest-transformer.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,18 @@ interface CachedConfigSet {
1919
export class TsJestTransformer implements Transformer {
2020
/**
2121
* cache ConfigSet between test runs
22+
*
23+
* @internal
2224
*/
2325
private static readonly _cachedConfigSets: CachedConfigSet[] = []
2426
protected readonly logger: Logger
27+
/**
28+
* @internal
29+
*/
2530
private _transformCfgStr!: string
31+
/**
32+
* @internal
33+
*/
2634
private _tsJestCfgSet!: ConfigSet
2735

2836
constructor() {

src/utils/json.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,10 @@ import stableStringify = require('fast-json-stable-stringify')
33

44
const UNDEFINED = 'undefined'
55

6-
/**
7-
* @internal
8-
*/
96
export function stringify(input: unknown): string {
107
return input === undefined ? UNDEFINED : stableStringify(input)
118
}
129

13-
/**
14-
* @internal
15-
*/
1610
export function parse(input: string): any {
1711
return input === UNDEFINED ? undefined : JSON.parse(input)
1812
}

src/utils/jsonable-value.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { stringify } from './json'
22

3-
/**
4-
* @internal
5-
*/
63
// eslint-disable-next-line @typescript-eslint/no-explicit-any
74
export class JsonableValue<V = Record<string, any>> {
85
private _serialized!: string

0 commit comments

Comments
 (0)