Skip to content

Commit

Permalink
refactor(config): bring back possibility to mock logging in test (#2053)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnpnl authored Oct 22, 2020
1 parent 6ac0f5c commit a0e5639
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
5 changes: 4 additions & 1 deletion src/__helpers__/fakers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Config } from '@jest/types'
import type { Logger } from 'bs-logger'
import { resolve } from 'path'

import { createCompilerInstance } from '../compiler/instance'
Expand Down Expand Up @@ -57,15 +58,17 @@ export const defaultResolve = (path: string): string => `resolved:${path}`
export function createConfigSet({
jestConfig,
tsJestConfig,
logger, // don't change this key name, otherwise mock logging won't work
resolve = defaultResolve,
...others
}: {
jestConfig?: Partial<Config.ProjectConfig>
tsJestConfig?: TsJestGlobalOptions
logger?: Logger
resolve?: ((path: string) => string) | null
[key: string]: any
} = {}): ConfigSet {
const cs = new ConfigSet(getJestConfig(jestConfig, tsJestConfig))
const cs = new ConfigSet(getJestConfig(jestConfig, tsJestConfig), logger)
if (resolve) {
cs.resolvePath = resolve
}
Expand Down
33 changes: 18 additions & 15 deletions src/config/config-set.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable jest/no-mocks-import */
import type { Transformer } from '@jest/transform'
import { testing } from 'bs-logger'
import { LogLevels, testing } from 'bs-logger'
import { join, resolve } from 'path'
import ts from 'typescript'

Expand Down Expand Up @@ -31,20 +31,23 @@ beforeEach(() => {

describe('packageJson', () => {
it('should not contain packageJson in final tsJest config', () => {
expect(
Object.keys(
createConfigSet({
jestConfig: {
globals: {
'ts-jest': {
packageJson: true,
},
},
} as any,
resolve: null,
}),
),
).not.toContain('packageJson')
const logger = testing.createLoggerMock()
createConfigSet({
jestConfig: {
globals: {
'ts-jest': {
packageJson: true,
},
},
} as any,
resolve: null,
logger,
})

expect(logger.target.filteredLines(LogLevels.warn)[0]).toMatchInlineSnapshot(`
"[level:40] The option \`packageJson\` is deprecated and will be removed in ts-jest 27. This option is not used by internal \`ts-jest\`
"
`)
})
}) // packageJson

Expand Down
10 changes: 8 additions & 2 deletions src/config/config-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,14 @@ export class ConfigSet {
tsBuildInfoFile: undefined,
}

constructor(private readonly jestConfig: Config.ProjectConfig) {
this.logger = rootLogger.child({ [LogContexts.namespace]: 'config' })
constructor(
private readonly jestConfig: Config.ProjectConfig,
// mainly for testing logging
private readonly parentLogger?: Logger,
) {
this.logger = this.parentLogger
? this.parentLogger.child({ [LogContexts.namespace]: 'config' })
: rootLogger.child({ namespace: 'config' })
this._cwd = normalize(this.jestConfig.cwd ?? process.cwd())
this._rootDir = normalize(this.jestConfig.rootDir ?? this._cwd)
const tsJestCfg = this.jestConfig.globals && this.jestConfig.globals['ts-jest']
Expand Down

0 comments on commit a0e5639

Please sign in to comment.