Skip to content

Commit 5ef980f

Browse files
committed
fix(logger): removes cyclic imports
1 parent 4c69406 commit 5ef980f

5 files changed

Lines changed: 33 additions & 28 deletions

File tree

src/config/config-set.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jest.mock('../util/backports')
1111

1212
const backports = mocked(_backports)
1313

14-
backports.backportJestConfig.mockImplementation(config => ({
14+
backports.backportJestConfig.mockImplementation((_, config) => ({
1515
...config,
1616
__backported: true,
1717
}))

src/config/config-set.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export class ConfigSet {
138138

139139
@Memoize()
140140
get jest(): jest.ProjectConfig {
141-
const config = backportJestConfig(this._jestConfig)
141+
const config = backportJestConfig(this.logger, this._jestConfig)
142142
if (this.parentOptions) {
143143
const globals: any = config.globals || (config.globals = {})
144144
// TODO: implement correct deep merging instead

src/util/backports.spec.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { backportJestConfig } from './backports'
22
import set from 'lodash.set'
33
import { inspect } from 'util'
4-
import { logTargetMock } from '../__helpers__/mocks'
4+
import { testing } from 'bs-logger'
55

6-
const logTarget = logTargetMock()
6+
const logger = testing.createLoggerMock()
7+
const logTarget = logger.target
78

89
beforeEach(() => {
910
logTarget.clear()
@@ -19,12 +20,14 @@ describe('backportJestConfig', () => {
1920
})
2021
describe(`with "${oldPath}" set to ${inspect(val)}`, () => {
2122
it(`should wran the user`, () => {
22-
backportJestConfig(original)
23+
backportJestConfig(logger, original)
2324
expect(logTarget.lines.warn.last).toMatchSnapshot()
2425
}) // should warn the user
2526
it(`should have changed the config correctly`, () => {
2627
expect(original).toMatchSnapshot('before')
27-
expect(backportJestConfig(original)).toMatchSnapshot('migrated')
28+
expect(backportJestConfig(logger, original)).toMatchSnapshot(
29+
'migrated',
30+
)
2831
}) // should have changed the config
2932
}) // with xxx set to yyy
3033
}) // for

src/util/backports.ts

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,22 @@
11
import { interpolate, Deprecateds } from './messages'
2-
import { Logger } from 'bs-logger'
2+
import { Logger, LogContexts } from 'bs-logger'
33

4-
// we must use a getter here since the root logger is using ourself
5-
let _logger: Logger
6-
const logger = {
7-
get get() {
8-
return (
9-
_logger ||
10-
(_logger = require('./logger').rootLogger.child({
11-
namespace: 'backports',
12-
}))
13-
)
14-
},
15-
}
4+
const context = { [LogContexts.namespace]: 'backports' }
165

176
export const backportJestConfig = <
187
T extends jest.InitialOptions | jest.ProjectConfig
198
>(
20-
config: T = {} as any,
9+
logger: Logger,
10+
config: T,
2111
): T => {
22-
logger.get.debug({ config }, 'backporting config')
12+
logger.debug({ ...context, config }, 'backporting config')
2313

24-
const { globals = {} } = config as any
14+
const { globals = {} } = (config || {}) as any
2515
const { 'ts-jest': tsJest = {} } = globals as any
2616
const mergeTsJest: any = {}
2717
const warnConfig = (oldPath: string, newPath: string, note?: string) => {
28-
logger.get.warn(
18+
logger.warn(
19+
context,
2920
interpolate(
3021
note ? Deprecateds.ConfigOptionWithNote : Deprecateds.ConfigOption,
3122
{
@@ -120,7 +111,7 @@ export const backportJestConfig = <
120111
}
121112
}
122113

123-
export const backportTsJestDebugEnvVar = () => {
114+
export const backportTsJestDebugEnvVar = (logger: Logger) => {
124115
if ('TS_JEST_DEBUG' in process.env) {
125116
const shouldLog = !/^\s*(?:0|f(?:alse)?|no?|disabled?|off|)\s*$/i.test(
126117
process.env.TS_JEST_DEBUG || '',
@@ -129,8 +120,8 @@ export const backportTsJestDebugEnvVar = () => {
129120
if (shouldLog) {
130121
process.env.TS_JEST_LOG = `ts-jest.log,stderr:warn`
131122
}
132-
// must be called after because this function is used when the root logger is created
133-
logger.get.warn(
123+
logger.warn(
124+
context,
134125
interpolate(Deprecateds.EnvVar, {
135126
old: 'TS_JEST_DEBUG',
136127
new: 'TS_JEST_LOG',

src/util/logger.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
import { createLogger, LogContexts, LogLevels } from 'bs-logger'
22
import { backportTsJestDebugEnvVar } from './backports'
33

4-
backportTsJestDebugEnvVar()
4+
const original = process.env.TS_JEST_LOG
55

6-
export const rootLogger = createLogger({
6+
const buildOptions = () => ({
77
context: {
88
[LogContexts.package]: 'ts-jest',
99
[LogContexts.logLevel]: LogLevels.trace,
1010
},
1111
targets: process.env.TS_JEST_LOG || undefined,
1212
})
13+
14+
let rootLogger = createLogger(buildOptions())
15+
16+
backportTsJestDebugEnvVar(rootLogger)
17+
18+
// re-create the logger if the env var has been backported
19+
if (original !== process.env.TS_JEST_LOG) {
20+
rootLogger = createLogger(buildOptions())
21+
}
22+
23+
export { rootLogger }

0 commit comments

Comments
 (0)