Skip to content

Commit a0e5639

Browse files
authored
refactor(config): bring back possibility to mock logging in test (#2053)
1 parent 6ac0f5c commit a0e5639

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed

src/__helpers__/fakers.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Config } from '@jest/types'
2+
import type { Logger } from 'bs-logger'
23
import { resolve } from 'path'
34

45
import { createCompilerInstance } from '../compiler/instance'
@@ -57,15 +58,17 @@ export const defaultResolve = (path: string): string => `resolved:${path}`
5758
export function createConfigSet({
5859
jestConfig,
5960
tsJestConfig,
61+
logger, // don't change this key name, otherwise mock logging won't work
6062
resolve = defaultResolve,
6163
...others
6264
}: {
6365
jestConfig?: Partial<Config.ProjectConfig>
6466
tsJestConfig?: TsJestGlobalOptions
67+
logger?: Logger
6568
resolve?: ((path: string) => string) | null
6669
[key: string]: any
6770
} = {}): ConfigSet {
68-
const cs = new ConfigSet(getJestConfig(jestConfig, tsJestConfig))
71+
const cs = new ConfigSet(getJestConfig(jestConfig, tsJestConfig), logger)
6972
if (resolve) {
7073
cs.resolvePath = resolve
7174
}

src/config/config-set.spec.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable jest/no-mocks-import */
22
import type { Transformer } from '@jest/transform'
3-
import { testing } from 'bs-logger'
3+
import { LogLevels, testing } from 'bs-logger'
44
import { join, resolve } from 'path'
55
import ts from 'typescript'
66

@@ -31,20 +31,23 @@ beforeEach(() => {
3131

3232
describe('packageJson', () => {
3333
it('should not contain packageJson in final tsJest config', () => {
34-
expect(
35-
Object.keys(
36-
createConfigSet({
37-
jestConfig: {
38-
globals: {
39-
'ts-jest': {
40-
packageJson: true,
41-
},
42-
},
43-
} as any,
44-
resolve: null,
45-
}),
46-
),
47-
).not.toContain('packageJson')
34+
const logger = testing.createLoggerMock()
35+
createConfigSet({
36+
jestConfig: {
37+
globals: {
38+
'ts-jest': {
39+
packageJson: true,
40+
},
41+
},
42+
} as any,
43+
resolve: null,
44+
logger,
45+
})
46+
47+
expect(logger.target.filteredLines(LogLevels.warn)[0]).toMatchInlineSnapshot(`
48+
"[level:40] The option \`packageJson\` is deprecated and will be removed in ts-jest 27. This option is not used by internal \`ts-jest\`
49+
"
50+
`)
4851
})
4952
}) // packageJson
5053

src/config/config-set.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,14 @@ export class ConfigSet {
145145
tsBuildInfoFile: undefined,
146146
}
147147

148-
constructor(private readonly jestConfig: Config.ProjectConfig) {
149-
this.logger = rootLogger.child({ [LogContexts.namespace]: 'config' })
148+
constructor(
149+
private readonly jestConfig: Config.ProjectConfig,
150+
// mainly for testing logging
151+
private readonly parentLogger?: Logger,
152+
) {
153+
this.logger = this.parentLogger
154+
? this.parentLogger.child({ [LogContexts.namespace]: 'config' })
155+
: rootLogger.child({ namespace: 'config' })
150156
this._cwd = normalize(this.jestConfig.cwd ?? process.cwd())
151157
this._rootDir = normalize(this.jestConfig.rootDir ?? this._cwd)
152158
const tsJestCfg = this.jestConfig.globals && this.jestConfig.globals['ts-jest']

0 commit comments

Comments
 (0)