Skip to content

Commit 4726b1f

Browse files
authored
refactor(config): expose only create config set and create compiler apis (#2355)
1 parent 5f91336 commit 4726b1f

File tree

2 files changed

+74
-13
lines changed

2 files changed

+74
-13
lines changed

src/ts-jest-transformer.spec.ts

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import fs from 'fs'
22
import { join } from 'path'
33

4-
import { LogLevels } from 'bs-logger'
4+
import { Logger, LogLevels } from 'bs-logger'
55
import { removeSync, writeFileSync } from 'fs-extra'
66
import mkdirp from 'mkdirp'
77
import { Extension, ResolvedModuleFull } from 'typescript'
88

99
import { createConfigSet } from './__helpers__/fakers'
1010
import { logTargetMock } from './__helpers__/mocks'
1111
import { SOURCE_MAPPING_PREFIX } from './compiler/compiler-utils'
12+
import { TsCompiler } from './compiler/ts-compiler'
1213
import { TsJestCompiler } from './compiler/ts-jest-compiler'
14+
import { ConfigSet } from './config/config-set'
1315
import { CACHE_KEY_EL_SEPARATOR, TsJestTransformer } from './ts-jest-transformer'
14-
import type { ResolvedModulesMap } from './types'
16+
import type { ProjectConfigTsJest, ResolvedModulesMap, StringMap } from './types'
1517
import { stringify } from './utils/json'
1618
import { sha1 } from './utils/sha1'
1719
import { VersionCheckers } from './utils/version-checkers'
@@ -420,7 +422,35 @@ Array [
420422
})
421423

422424
describe('subclass extends TsJestTransformer', () => {
423-
class MyTransformer extends TsJestTransformer {}
425+
class MyTsCompiler extends TsCompiler {
426+
constructor(readonly configSet: ConfigSet, readonly jestCacheFS: StringMap) {
427+
super(configSet, jestCacheFS)
428+
}
429+
}
430+
431+
class MyConfigSet extends ConfigSet {
432+
constructor(readonly jestConfig: ProjectConfigTsJest, readonly parentLogger?: Logger) {
433+
super(jestConfig, parentLogger)
434+
}
435+
}
436+
437+
class MyTransformer extends TsJestTransformer {
438+
// @ts-expect-error testing purpose
439+
// eslint-disable-next-line class-methods-use-this
440+
protected _createCompiler(configSet: ConfigSet, cacheFS: Map<string, string>): MyTsCompiler {
441+
return new MyTsCompiler(configSet, cacheFS)
442+
}
443+
444+
// eslint-disable-next-line class-methods-use-this
445+
protected _createConfigSet(config: ProjectConfigTsJest): MyConfigSet {
446+
return new MyConfigSet(config)
447+
}
448+
}
449+
let tr: MyTransformer
450+
451+
beforeEach(() => {
452+
tr = new MyTransformer()
453+
})
424454

425455
test('should have jest version checking', () => {
426456
VersionCheckers.jest.warn = jest.fn()
@@ -429,5 +459,23 @@ Array [
429459

430460
expect(VersionCheckers.jest.warn).toHaveBeenCalled()
431461
})
462+
463+
test('should create MyTsCompiler instance', () => {
464+
// @ts-expect-error testing purpose
465+
expect(tr._createCompiler(createConfigSet(), new Map())).toBeInstanceOf(MyTsCompiler)
466+
})
467+
468+
test('should create MyConfigSet instance', () => {
469+
expect(
470+
// @ts-expect-error testing purpose
471+
tr._createConfigSet({
472+
cwd: process.cwd(),
473+
extensionsToTreatAsEsm: [],
474+
globals: Object.create(null),
475+
testMatch: [],
476+
testRegex: [],
477+
}),
478+
).toBeInstanceOf(MyConfigSet)
479+
})
432480
})
433481
})

src/ts-jest-transformer.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@ interface TsJestHooksMap {
3131
afterProcess?(args: any[], result: string | TransformedSource): string | TransformedSource | void
3232
}
3333

34-
export interface DepGraphInfo {
34+
interface DepGraphInfo {
3535
fileContent: string
3636
resolveModuleNames: string[]
3737
}
3838

39+
/**
40+
* @internal
41+
*/
3942
export const CACHE_KEY_EL_SEPARATOR = '\x00'
4043

4144
export class TsJestTransformer implements Transformer {
@@ -45,11 +48,11 @@ export class TsJestTransformer implements Transformer {
4548
* @internal
4649
*/
4750
private static readonly _cachedConfigSets: CachedConfigSet[] = []
48-
protected _compiler!: TsJestCompiler
49-
protected readonly _logger: Logger
50-
protected _tsResolvedModulesCachePath: string | undefined
51-
protected _transformCfgStr!: string
52-
protected _depGraphs: Map<string, DepGraphInfo> = new Map<string, DepGraphInfo>()
51+
private readonly _logger: Logger
52+
private _compiler!: TsJestCompiler
53+
private _tsResolvedModulesCachePath: string | undefined
54+
private _transformCfgStr!: string
55+
private _depGraphs: Map<string, DepGraphInfo> = new Map<string, DepGraphInfo>()
5356

5457
constructor() {
5558
this._logger = rootLogger.child({ namespace: 'ts-jest-transformer' })
@@ -58,7 +61,7 @@ export class TsJestTransformer implements Transformer {
5861
this._logger.debug('created new transformer')
5962
}
6063

61-
protected _configsFor(transformOptions: TransformOptionsTsJest): ConfigSet {
64+
private _configsFor(transformOptions: TransformOptionsTsJest): ConfigSet {
6265
const { config, cacheFS } = transformOptions
6366
const ccs: CachedConfigSet | undefined = TsJestTransformer._cachedConfigSets.find(
6467
(cs) => cs.jestConfig.value === config,
@@ -90,14 +93,14 @@ export class TsJestTransformer implements Transformer {
9093
// create the new record in the index
9194
this._logger.info('no matching config-set found, creating a new one')
9295

93-
configSet = new ConfigSet(config)
96+
configSet = this._createConfigSet(config)
9497
const jest = { ...config }
9598
// we need to remove some stuff from jest config
9699
// this which does not depend on config
97100
jest.name = undefined as any
98101
jest.cacheDirectory = undefined as any
99102
this._transformCfgStr = `${new JsonableValue(jest).serialized}${configSet.cacheSuffix}`
100-
this._compiler = new TsJestCompiler(configSet, cacheFS)
103+
this._compiler = this._createCompiler(configSet, cacheFS)
101104
this._getFsCachedResolvedModules(configSet)
102105
TsJestTransformer._cachedConfigSets.push({
103106
jestConfig: new JsonableValue(config),
@@ -113,6 +116,16 @@ export class TsJestTransformer implements Transformer {
113116
return configSet
114117
}
115118

119+
// eslint-disable-next-line class-methods-use-this
120+
protected _createConfigSet(config: ProjectConfigTsJest): ConfigSet {
121+
return new ConfigSet(config)
122+
}
123+
124+
// eslint-disable-next-line class-methods-use-this
125+
protected _createCompiler(configSet: ConfigSet, cacheFS: Map<string, string>): TsJestCompiler {
126+
return new TsJestCompiler(configSet, cacheFS)
127+
}
128+
116129
/**
117130
* @public
118131
*/
@@ -260,7 +273,7 @@ export class TsJestTransformer implements Transformer {
260273
/**
261274
* Subclasses extends `TsJestTransformer` can call this method to get resolved module disk cache
262275
*/
263-
protected _getFsCachedResolvedModules(configSet: ConfigSet): void {
276+
private _getFsCachedResolvedModules(configSet: ConfigSet): void {
264277
const cacheDir = configSet.tsCacheDir
265278
if (!configSet.isolatedModules && cacheDir) {
266279
// Make sure the cache directory exists before continuing.

0 commit comments

Comments
 (0)