Skip to content

Commit 2bb4430

Browse files
authored
refactor(config): replace jest/rootDir getters with private fields (#2033)
- Replace `jest` getter with private field `_jestCfg` - Replace `rootDir` getter with private field `_rootDir` - Expose `cwd`, `resolveFilePath` as public apis
1 parent 351b812 commit 2bb4430

2 files changed

Lines changed: 40 additions & 42 deletions

File tree

src/config/config-set.spec.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ describe('tsJest', () => {
122122
resolveJsonModule: true,
123123
},
124124
})
125-
expect(logger.target.lines[1]).toMatchSnapshot()
125+
expect(logger.target.lines[0]).toMatchSnapshot()
126126
})
127127

128128
it('should not show warning message with tsconfig option', () => {
@@ -146,7 +146,7 @@ describe('tsJest', () => {
146146
resolveJsonModule: true,
147147
},
148148
})
149-
expect(logger.target.lines[1]).not.toContain(Deprecations.TsConfig)
149+
expect(logger.target.lines[0]).not.toContain(Deprecations.TsConfig)
150150
})
151151
})
152152

@@ -208,7 +208,7 @@ describe('tsJest', () => {
208208
logger.target.clear()
209209

210210
expect(Object.keys(cs.tsJest.transformers)).toHaveLength(1)
211-
expect(logger.target.lines[1]).toMatchSnapshot()
211+
expect(logger.target.lines[0]).toMatchSnapshot()
212212
})
213213

214214
it('should support transformers with options', () => {
@@ -270,7 +270,7 @@ describe('tsJest', () => {
270270
logger.target.clear()
271271

272272
expect(Object.keys(cs.tsJest.transformers)).toHaveLength(Object.keys(data).length)
273-
expect(logger.target.lines[1]).not.toContain(Deprecations.AstTransformerArrayConfig)
273+
expect(logger.target.lines[0]).not.toContain(Deprecations.AstTransformerArrayConfig)
274274
})
275275
}) // custom AST transformers
276276

@@ -871,7 +871,7 @@ describe('babelJestTransformer', () => {
871871
const babelJest = cs.babelJestTransformer as Transformer
872872

873873
expect(cs.tsJest.babelConfig).toBeUndefined()
874-
expect(logger.target.lines[2]).toMatchInlineSnapshot(`
874+
expect(logger.target.lines[1]).toMatchInlineSnapshot(`
875875
"[level:20] babel is disabled
876876
"
877877
`)
@@ -897,7 +897,7 @@ describe('babelJestTransformer', () => {
897897

898898
expect(cs.tsJest.babelConfig?.kind).toEqual('file')
899899
expect(cs.tsJest.babelConfig?.value).toBeUndefined()
900-
expect(logger.target.lines[2]).toMatchInlineSnapshot(`
900+
expect(logger.target.lines[1]).toMatchInlineSnapshot(`
901901
"[level:20] normalized babel config via ts-jest option
902902
"
903903
`)
@@ -925,7 +925,7 @@ describe('babelJestTransformer', () => {
925925

926926
expect(cs.tsJest.babelConfig?.kind).toEqual('file')
927927
expect(cs.tsJest.babelConfig?.value).toEqual(join(process.cwd(), FILE))
928-
expect(logger.target.lines[3]).toMatchInlineSnapshot(`
928+
expect(logger.target.lines[2]).toMatchInlineSnapshot(`
929929
"[level:20] normalized babel config via ts-jest option
930930
"
931931
`)
@@ -953,7 +953,7 @@ describe('babelJestTransformer', () => {
953953

954954
expect(cs.tsJest.babelConfig?.kind).toEqual('file')
955955
expect(cs.tsJest.babelConfig?.value).toEqual(join(process.cwd(), FILE))
956-
expect(logger.target.lines[3]).toMatchInlineSnapshot(`
956+
expect(logger.target.lines[2]).toMatchInlineSnapshot(`
957957
"[level:20] normalized babel config via ts-jest option
958958
"
959959
`)
@@ -989,7 +989,7 @@ describe('babelJestTransformer', () => {
989989
],
990990
}
991991
`)
992-
expect(logger.target.lines[2]).toMatchInlineSnapshot(`
992+
expect(logger.target.lines[1]).toMatchInlineSnapshot(`
993993
"[level:20] normalized babel config via ts-jest option
994994
"
995995
`)
@@ -1017,7 +1017,7 @@ describe('babelJestTransformer', () => {
10171017

10181018
expect(cs.tsJest.babelConfig?.kind).toEqual('inline')
10191019
expect(cs.tsJest.babelConfig?.value).toEqual(CONFIG)
1020-
expect(logger.target.lines[2]).toMatchInlineSnapshot(`
1020+
expect(logger.target.lines[1]).toMatchInlineSnapshot(`
10211021
"[level:20] normalized babel config via ts-jest option
10221022
"
10231023
`)

src/config/config-set.ts

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -125,24 +125,44 @@ const toDiagnosticCodeList = (items: (string | number)[], into: number[] = []):
125125
}
126126

127127
export class ConfigSet {
128+
readonly logger: Logger
128129
/**
129130
* @internal
130131
*/
131-
@Memoize()
132-
private get jest(): Config.ProjectConfig {
132+
private readonly _cwd: string
133+
/**
134+
* @internal
135+
*/
136+
private readonly _rootDir: string
137+
/**
138+
* @internal
139+
*/
140+
private _jestCfg!: Config.ProjectConfig
141+
142+
constructor(private readonly jestConfig: Config.ProjectConfig) {
143+
this.logger = rootLogger.child({ [LogContexts.namespace]: 'config' })
144+
this._cwd = normalize(this.jestConfig.cwd ?? process.cwd())
145+
this._rootDir = normalize(this.jestConfig.rootDir ?? this._cwd)
146+
this._backportJestCfg()
147+
}
148+
149+
/**
150+
* @internal
151+
*/
152+
private _backportJestCfg(): void {
133153
const config = backportJestConfig(this.logger, this.jestConfig)
134154

135155
this.logger.debug({ jestConfig: config }, 'normalized jest config')
136156

137-
return config
157+
this._jestCfg = config
138158
}
139159

140160
/**
141161
* @internal
142162
*/
143163
@Memoize()
144164
get isTestFile(): (fileName: string) => boolean {
145-
const matchablePatterns = [...this.jest.testMatch, ...this.jest.testRegex].filter(
165+
const matchablePatterns = [...this._jestCfg.testMatch, ...this._jestCfg.testRegex].filter(
146166
(pattern) =>
147167
/**
148168
* jest config testRegex doesn't always deliver the correct RegExp object
@@ -165,7 +185,7 @@ export class ConfigSet {
165185
*/
166186
@Memoize()
167187
get tsJest(): TsJestConfig {
168-
const parsedConfig = this.jest
188+
const parsedConfig = this._jestCfg
169189
const { globals = {} } = parsedConfig as any
170190
const options: TsJestGlobalOptions = { ...globals['ts-jest'] }
171191

@@ -601,7 +621,7 @@ export class ConfigSet {
601621
*/
602622
@Memoize()
603623
get tsCacheDir(): string | undefined {
604-
if (!this.jest.cache) {
624+
if (!this._jestCfg.cache) {
605625
this.logger.debug('file caching disabled')
606626

607627
return undefined
@@ -616,7 +636,7 @@ export class ConfigSet {
616636
diagnostics: this.tsJest.diagnostics,
617637
}),
618638
)
619-
const res = join(this.jest.cacheDirectory, 'ts-jest', cacheSuffix.substr(0, 2), cacheSuffix.substr(2))
639+
const res = join(this._jestCfg.cacheDirectory, 'ts-jest', cacheSuffix.substr(0, 2), cacheSuffix.substr(2))
620640

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

@@ -657,17 +677,6 @@ export class ConfigSet {
657677
return options
658678
}
659679

660-
/**
661-
* @internal
662-
*/
663-
@Memoize()
664-
get rootDir(): string {
665-
return normalize(this.jest.rootDir || this.cwd)
666-
}
667-
668-
/**
669-
* @internal
670-
*/
671680
get cwd(): string {
672681
return this._cwd
673682
}
@@ -681,14 +690,6 @@ export class ConfigSet {
681690
return MY_DIGEST
682691
}
683692

684-
readonly logger: Logger
685-
private readonly _cwd: string
686-
687-
constructor(private readonly jestConfig: Config.ProjectConfig) {
688-
this.logger = rootLogger.child({ [LogContexts.namespace]: 'config' })
689-
this._cwd = normalize(this.jestConfig.cwd ?? process.cwd())
690-
}
691-
692693
/**
693694
* @internal
694695
*/
@@ -742,15 +743,15 @@ export class ConfigSet {
742743
noProject?: boolean | null,
743744
): ParsedCommandLine {
744745
let config = { compilerOptions: Object.create(null) }
745-
let basePath = normalizeSlashes(this.rootDir)
746+
let basePath = normalizeSlashes(this._rootDir)
746747
let configFileName: string | undefined
747748
const ts = this.compilerModule
748749

749750
if (!noProject) {
750751
// Read project configuration when available.
751752
configFileName = resolvedConfigFile
752753
? normalizeSlashes(resolvedConfigFile)
753-
: ts.findConfigFile(normalizeSlashes(this.rootDir), ts.sys.fileExists)
754+
: ts.findConfigFile(normalizeSlashes(this._rootDir), ts.sys.fileExists)
754755

755756
if (configFileName) {
756757
this.logger.debug({ tsConfigFileName: configFileName }, 'readTsConfig(): reading', configFileName)
@@ -841,17 +842,14 @@ export class ConfigSet {
841842
return result
842843
}
843844

844-
/**
845-
* @internal
846-
*/
847845
resolvePath(
848846
inputPath: string,
849847
{ throwIfMissing = true, nodeResolve = false }: { throwIfMissing?: boolean; nodeResolve?: boolean } = {},
850848
): string {
851849
let path: string = inputPath
852850
let nodeResolved = false
853851
if (path.startsWith('<rootDir>')) {
854-
path = resolve(join(this.rootDir, path.substr(9)))
852+
path = resolve(join(this._rootDir, path.substr(9)))
855853
} else if (!isAbsolute(path)) {
856854
if (!path.startsWith('.') && nodeResolve) {
857855
try {

0 commit comments

Comments
 (0)