From 5ef980f9b7e51d589149995b7ca7ee693af55d50 Mon Sep 17 00:00:00 2001 From: Huafu Gandon Date: Sat, 1 Sep 2018 09:29:38 +0200 Subject: [PATCH] fix(logger): removes cyclic imports --- src/config/config-set.spec.ts | 2 +- src/config/config-set.ts | 2 +- src/util/backports.spec.ts | 11 +++++++---- src/util/backports.ts | 31 +++++++++++-------------------- src/util/logger.ts | 15 +++++++++++++-- 5 files changed, 33 insertions(+), 28 deletions(-) diff --git a/src/config/config-set.spec.ts b/src/config/config-set.spec.ts index ca85017512..52af76c2d8 100644 --- a/src/config/config-set.spec.ts +++ b/src/config/config-set.spec.ts @@ -11,7 +11,7 @@ jest.mock('../util/backports') const backports = mocked(_backports) -backports.backportJestConfig.mockImplementation(config => ({ +backports.backportJestConfig.mockImplementation((_, config) => ({ ...config, __backported: true, })) diff --git a/src/config/config-set.ts b/src/config/config-set.ts index 52151a73a8..c2b0a097fa 100644 --- a/src/config/config-set.ts +++ b/src/config/config-set.ts @@ -138,7 +138,7 @@ export class ConfigSet { @Memoize() get jest(): jest.ProjectConfig { - const config = backportJestConfig(this._jestConfig) + const config = backportJestConfig(this.logger, this._jestConfig) if (this.parentOptions) { const globals: any = config.globals || (config.globals = {}) // TODO: implement correct deep merging instead diff --git a/src/util/backports.spec.ts b/src/util/backports.spec.ts index 5d4785a71c..4e4e6c8e16 100644 --- a/src/util/backports.spec.ts +++ b/src/util/backports.spec.ts @@ -1,9 +1,10 @@ import { backportJestConfig } from './backports' import set from 'lodash.set' import { inspect } from 'util' -import { logTargetMock } from '../__helpers__/mocks' +import { testing } from 'bs-logger' -const logTarget = logTargetMock() +const logger = testing.createLoggerMock() +const logTarget = logger.target beforeEach(() => { logTarget.clear() @@ -19,12 +20,14 @@ describe('backportJestConfig', () => { }) describe(`with "${oldPath}" set to ${inspect(val)}`, () => { it(`should wran the user`, () => { - backportJestConfig(original) + backportJestConfig(logger, original) expect(logTarget.lines.warn.last).toMatchSnapshot() }) // should warn the user it(`should have changed the config correctly`, () => { expect(original).toMatchSnapshot('before') - expect(backportJestConfig(original)).toMatchSnapshot('migrated') + expect(backportJestConfig(logger, original)).toMatchSnapshot( + 'migrated', + ) }) // should have changed the config }) // with xxx set to yyy }) // for diff --git a/src/util/backports.ts b/src/util/backports.ts index d67001633c..74990983ce 100644 --- a/src/util/backports.ts +++ b/src/util/backports.ts @@ -1,31 +1,22 @@ import { interpolate, Deprecateds } from './messages' -import { Logger } from 'bs-logger' +import { Logger, LogContexts } from 'bs-logger' -// we must use a getter here since the root logger is using ourself -let _logger: Logger -const logger = { - get get() { - return ( - _logger || - (_logger = require('./logger').rootLogger.child({ - namespace: 'backports', - })) - ) - }, -} +const context = { [LogContexts.namespace]: 'backports' } export const backportJestConfig = < T extends jest.InitialOptions | jest.ProjectConfig >( - config: T = {} as any, + logger: Logger, + config: T, ): T => { - logger.get.debug({ config }, 'backporting config') + logger.debug({ ...context, config }, 'backporting config') - const { globals = {} } = config as any + const { globals = {} } = (config || {}) as any const { 'ts-jest': tsJest = {} } = globals as any const mergeTsJest: any = {} const warnConfig = (oldPath: string, newPath: string, note?: string) => { - logger.get.warn( + logger.warn( + context, interpolate( note ? Deprecateds.ConfigOptionWithNote : Deprecateds.ConfigOption, { @@ -120,7 +111,7 @@ export const backportJestConfig = < } } -export const backportTsJestDebugEnvVar = () => { +export const backportTsJestDebugEnvVar = (logger: Logger) => { if ('TS_JEST_DEBUG' in process.env) { const shouldLog = !/^\s*(?:0|f(?:alse)?|no?|disabled?|off|)\s*$/i.test( process.env.TS_JEST_DEBUG || '', @@ -129,8 +120,8 @@ export const backportTsJestDebugEnvVar = () => { if (shouldLog) { process.env.TS_JEST_LOG = `ts-jest.log,stderr:warn` } - // must be called after because this function is used when the root logger is created - logger.get.warn( + logger.warn( + context, interpolate(Deprecateds.EnvVar, { old: 'TS_JEST_DEBUG', new: 'TS_JEST_LOG', diff --git a/src/util/logger.ts b/src/util/logger.ts index e5c6969984..6741d58620 100644 --- a/src/util/logger.ts +++ b/src/util/logger.ts @@ -1,12 +1,23 @@ import { createLogger, LogContexts, LogLevels } from 'bs-logger' import { backportTsJestDebugEnvVar } from './backports' -backportTsJestDebugEnvVar() +const original = process.env.TS_JEST_LOG -export const rootLogger = createLogger({ +const buildOptions = () => ({ context: { [LogContexts.package]: 'ts-jest', [LogContexts.logLevel]: LogLevels.trace, }, targets: process.env.TS_JEST_LOG || undefined, }) + +let rootLogger = createLogger(buildOptions()) + +backportTsJestDebugEnvVar(rootLogger) + +// re-create the logger if the env var has been backported +if (original !== process.env.TS_JEST_LOG) { + rootLogger = createLogger(buildOptions()) +} + +export { rootLogger }