Skip to content

Commit

Permalink
refactor(config): replace jest/rootDir getters with private fields (
Browse files Browse the repository at this point in the history
#2033)

- Replace `jest` getter with private field `_jestCfg`
- Replace `rootDir` getter with private field `_rootDir`
- Expose `cwd`, `resolveFilePath` as public apis
  • Loading branch information
ahnpnl committed Oct 17, 2020
1 parent 351b812 commit 2bb4430
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 42 deletions.
20 changes: 10 additions & 10 deletions src/config/config-set.spec.ts
Expand Up @@ -122,7 +122,7 @@ describe('tsJest', () => {
resolveJsonModule: true,
},
})
expect(logger.target.lines[1]).toMatchSnapshot()
expect(logger.target.lines[0]).toMatchSnapshot()
})

it('should not show warning message with tsconfig option', () => {
Expand All @@ -146,7 +146,7 @@ describe('tsJest', () => {
resolveJsonModule: true,
},
})
expect(logger.target.lines[1]).not.toContain(Deprecations.TsConfig)
expect(logger.target.lines[0]).not.toContain(Deprecations.TsConfig)
})
})

Expand Down Expand Up @@ -208,7 +208,7 @@ describe('tsJest', () => {
logger.target.clear()

expect(Object.keys(cs.tsJest.transformers)).toHaveLength(1)
expect(logger.target.lines[1]).toMatchSnapshot()
expect(logger.target.lines[0]).toMatchSnapshot()
})

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

expect(Object.keys(cs.tsJest.transformers)).toHaveLength(Object.keys(data).length)
expect(logger.target.lines[1]).not.toContain(Deprecations.AstTransformerArrayConfig)
expect(logger.target.lines[0]).not.toContain(Deprecations.AstTransformerArrayConfig)
})
}) // custom AST transformers

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

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

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

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

expect(cs.tsJest.babelConfig?.kind).toEqual('file')
expect(cs.tsJest.babelConfig?.value).toEqual(join(process.cwd(), FILE))
expect(logger.target.lines[3]).toMatchInlineSnapshot(`
expect(logger.target.lines[2]).toMatchInlineSnapshot(`
"[level:20] normalized babel config via ts-jest option
"
`)
Expand Down Expand Up @@ -989,7 +989,7 @@ describe('babelJestTransformer', () => {
],
}
`)
expect(logger.target.lines[2]).toMatchInlineSnapshot(`
expect(logger.target.lines[1]).toMatchInlineSnapshot(`
"[level:20] normalized babel config via ts-jest option
"
`)
Expand Down Expand Up @@ -1017,7 +1017,7 @@ describe('babelJestTransformer', () => {

expect(cs.tsJest.babelConfig?.kind).toEqual('inline')
expect(cs.tsJest.babelConfig?.value).toEqual(CONFIG)
expect(logger.target.lines[2]).toMatchInlineSnapshot(`
expect(logger.target.lines[1]).toMatchInlineSnapshot(`
"[level:20] normalized babel config via ts-jest option
"
`)
Expand Down
62 changes: 30 additions & 32 deletions src/config/config-set.ts
Expand Up @@ -125,24 +125,44 @@ const toDiagnosticCodeList = (items: (string | number)[], into: number[] = []):
}

export class ConfigSet {
readonly logger: Logger
/**
* @internal
*/
@Memoize()
private get jest(): Config.ProjectConfig {
private readonly _cwd: string
/**
* @internal
*/
private readonly _rootDir: string
/**
* @internal
*/
private _jestCfg!: Config.ProjectConfig

constructor(private readonly jestConfig: Config.ProjectConfig) {
this.logger = rootLogger.child({ [LogContexts.namespace]: 'config' })
this._cwd = normalize(this.jestConfig.cwd ?? process.cwd())
this._rootDir = normalize(this.jestConfig.rootDir ?? this._cwd)
this._backportJestCfg()
}

/**
* @internal
*/
private _backportJestCfg(): void {
const config = backportJestConfig(this.logger, this.jestConfig)

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

return config
this._jestCfg = config
}

/**
* @internal
*/
@Memoize()
get isTestFile(): (fileName: string) => boolean {
const matchablePatterns = [...this.jest.testMatch, ...this.jest.testRegex].filter(
const matchablePatterns = [...this._jestCfg.testMatch, ...this._jestCfg.testRegex].filter(
(pattern) =>
/**
* jest config testRegex doesn't always deliver the correct RegExp object
Expand All @@ -165,7 +185,7 @@ export class ConfigSet {
*/
@Memoize()
get tsJest(): TsJestConfig {
const parsedConfig = this.jest
const parsedConfig = this._jestCfg
const { globals = {} } = parsedConfig as any
const options: TsJestGlobalOptions = { ...globals['ts-jest'] }

Expand Down Expand Up @@ -601,7 +621,7 @@ export class ConfigSet {
*/
@Memoize()
get tsCacheDir(): string | undefined {
if (!this.jest.cache) {
if (!this._jestCfg.cache) {
this.logger.debug('file caching disabled')

return undefined
Expand All @@ -616,7 +636,7 @@ export class ConfigSet {
diagnostics: this.tsJest.diagnostics,
}),
)
const res = join(this.jest.cacheDirectory, 'ts-jest', cacheSuffix.substr(0, 2), cacheSuffix.substr(2))
const res = join(this._jestCfg.cacheDirectory, 'ts-jest', cacheSuffix.substr(0, 2), cacheSuffix.substr(2))

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

Expand Down Expand Up @@ -657,17 +677,6 @@ export class ConfigSet {
return options
}

/**
* @internal
*/
@Memoize()
get rootDir(): string {
return normalize(this.jest.rootDir || this.cwd)
}

/**
* @internal
*/
get cwd(): string {
return this._cwd
}
Expand All @@ -681,14 +690,6 @@ export class ConfigSet {
return MY_DIGEST
}

readonly logger: Logger
private readonly _cwd: string

constructor(private readonly jestConfig: Config.ProjectConfig) {
this.logger = rootLogger.child({ [LogContexts.namespace]: 'config' })
this._cwd = normalize(this.jestConfig.cwd ?? process.cwd())
}

/**
* @internal
*/
Expand Down Expand Up @@ -742,15 +743,15 @@ export class ConfigSet {
noProject?: boolean | null,
): ParsedCommandLine {
let config = { compilerOptions: Object.create(null) }
let basePath = normalizeSlashes(this.rootDir)
let basePath = normalizeSlashes(this._rootDir)
let configFileName: string | undefined
const ts = this.compilerModule

if (!noProject) {
// Read project configuration when available.
configFileName = resolvedConfigFile
? normalizeSlashes(resolvedConfigFile)
: ts.findConfigFile(normalizeSlashes(this.rootDir), ts.sys.fileExists)
: ts.findConfigFile(normalizeSlashes(this._rootDir), ts.sys.fileExists)

if (configFileName) {
this.logger.debug({ tsConfigFileName: configFileName }, 'readTsConfig(): reading', configFileName)
Expand Down Expand Up @@ -841,17 +842,14 @@ export class ConfigSet {
return result
}

/**
* @internal
*/
resolvePath(
inputPath: string,
{ throwIfMissing = true, nodeResolve = false }: { throwIfMissing?: boolean; nodeResolve?: boolean } = {},
): string {
let path: string = inputPath
let nodeResolved = false
if (path.startsWith('<rootDir>')) {
path = resolve(join(this.rootDir, path.substr(9)))
path = resolve(join(this._rootDir, path.substr(9)))
} else if (!isAbsolute(path)) {
if (!path.startsWith('.') && nodeResolve) {
try {
Expand Down

0 comments on commit 2bb4430

Please sign in to comment.