Skip to content

Commit

Permalink
refactor(config): expose only create config set and create compiler a…
Browse files Browse the repository at this point in the history
…pis (#2355)
  • Loading branch information
ahnpnl committed Feb 13, 2021
1 parent 5f91336 commit 4726b1f
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 13 deletions.
54 changes: 51 additions & 3 deletions src/ts-jest-transformer.spec.ts
@@ -1,17 +1,19 @@
import fs from 'fs'
import { join } from 'path'

import { LogLevels } from 'bs-logger'
import { Logger, LogLevels } from 'bs-logger'
import { removeSync, writeFileSync } from 'fs-extra'
import mkdirp from 'mkdirp'
import { Extension, ResolvedModuleFull } from 'typescript'

import { createConfigSet } from './__helpers__/fakers'
import { logTargetMock } from './__helpers__/mocks'
import { SOURCE_MAPPING_PREFIX } from './compiler/compiler-utils'
import { TsCompiler } from './compiler/ts-compiler'
import { TsJestCompiler } from './compiler/ts-jest-compiler'
import { ConfigSet } from './config/config-set'
import { CACHE_KEY_EL_SEPARATOR, TsJestTransformer } from './ts-jest-transformer'
import type { ResolvedModulesMap } from './types'
import type { ProjectConfigTsJest, ResolvedModulesMap, StringMap } from './types'
import { stringify } from './utils/json'
import { sha1 } from './utils/sha1'
import { VersionCheckers } from './utils/version-checkers'
Expand Down Expand Up @@ -420,7 +422,35 @@ Array [
})

describe('subclass extends TsJestTransformer', () => {
class MyTransformer extends TsJestTransformer {}
class MyTsCompiler extends TsCompiler {
constructor(readonly configSet: ConfigSet, readonly jestCacheFS: StringMap) {
super(configSet, jestCacheFS)
}
}

class MyConfigSet extends ConfigSet {
constructor(readonly jestConfig: ProjectConfigTsJest, readonly parentLogger?: Logger) {
super(jestConfig, parentLogger)
}
}

class MyTransformer extends TsJestTransformer {
// @ts-expect-error testing purpose
// eslint-disable-next-line class-methods-use-this
protected _createCompiler(configSet: ConfigSet, cacheFS: Map<string, string>): MyTsCompiler {
return new MyTsCompiler(configSet, cacheFS)
}

// eslint-disable-next-line class-methods-use-this
protected _createConfigSet(config: ProjectConfigTsJest): MyConfigSet {
return new MyConfigSet(config)
}
}
let tr: MyTransformer

beforeEach(() => {
tr = new MyTransformer()
})

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

expect(VersionCheckers.jest.warn).toHaveBeenCalled()
})

test('should create MyTsCompiler instance', () => {
// @ts-expect-error testing purpose
expect(tr._createCompiler(createConfigSet(), new Map())).toBeInstanceOf(MyTsCompiler)
})

test('should create MyConfigSet instance', () => {
expect(
// @ts-expect-error testing purpose
tr._createConfigSet({
cwd: process.cwd(),
extensionsToTreatAsEsm: [],
globals: Object.create(null),
testMatch: [],
testRegex: [],
}),
).toBeInstanceOf(MyConfigSet)
})
})
})
33 changes: 23 additions & 10 deletions src/ts-jest-transformer.ts
Expand Up @@ -31,11 +31,14 @@ interface TsJestHooksMap {
afterProcess?(args: any[], result: string | TransformedSource): string | TransformedSource | void
}

export interface DepGraphInfo {
interface DepGraphInfo {
fileContent: string
resolveModuleNames: string[]
}

/**
* @internal
*/
export const CACHE_KEY_EL_SEPARATOR = '\x00'

export class TsJestTransformer implements Transformer {
Expand All @@ -45,11 +48,11 @@ export class TsJestTransformer implements Transformer {
* @internal
*/
private static readonly _cachedConfigSets: CachedConfigSet[] = []
protected _compiler!: TsJestCompiler
protected readonly _logger: Logger
protected _tsResolvedModulesCachePath: string | undefined
protected _transformCfgStr!: string
protected _depGraphs: Map<string, DepGraphInfo> = new Map<string, DepGraphInfo>()
private readonly _logger: Logger
private _compiler!: TsJestCompiler
private _tsResolvedModulesCachePath: string | undefined
private _transformCfgStr!: string
private _depGraphs: Map<string, DepGraphInfo> = new Map<string, DepGraphInfo>()

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

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

configSet = new ConfigSet(config)
configSet = this._createConfigSet(config)
const jest = { ...config }
// we need to remove some stuff from jest config
// this which does not depend on config
jest.name = undefined as any
jest.cacheDirectory = undefined as any
this._transformCfgStr = `${new JsonableValue(jest).serialized}${configSet.cacheSuffix}`
this._compiler = new TsJestCompiler(configSet, cacheFS)
this._compiler = this._createCompiler(configSet, cacheFS)
this._getFsCachedResolvedModules(configSet)
TsJestTransformer._cachedConfigSets.push({
jestConfig: new JsonableValue(config),
Expand All @@ -113,6 +116,16 @@ export class TsJestTransformer implements Transformer {
return configSet
}

// eslint-disable-next-line class-methods-use-this
protected _createConfigSet(config: ProjectConfigTsJest): ConfigSet {
return new ConfigSet(config)
}

// eslint-disable-next-line class-methods-use-this
protected _createCompiler(configSet: ConfigSet, cacheFS: Map<string, string>): TsJestCompiler {
return new TsJestCompiler(configSet, cacheFS)
}

/**
* @public
*/
Expand Down Expand Up @@ -260,7 +273,7 @@ export class TsJestTransformer implements Transformer {
/**
* Subclasses extends `TsJestTransformer` can call this method to get resolved module disk cache
*/
protected _getFsCachedResolvedModules(configSet: ConfigSet): void {
private _getFsCachedResolvedModules(configSet: ConfigSet): void {
const cacheDir = configSet.tsCacheDir
if (!configSet.isolatedModules && cacheDir) {
// Make sure the cache directory exists before continuing.
Expand Down

0 comments on commit 4726b1f

Please sign in to comment.