Skip to content

Commit

Permalink
fix(logger): removes cyclic imports
Browse files Browse the repository at this point in the history
  • Loading branch information
huafu committed Sep 1, 2018
1 parent 4c69406 commit 5ef980f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/config/config-set.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jest.mock('../util/backports')

const backports = mocked(_backports)

backports.backportJestConfig.mockImplementation(config => ({
backports.backportJestConfig.mockImplementation((_, config) => ({
...config,
__backported: true,
}))
Expand Down
2 changes: 1 addition & 1 deletion src/config/config-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions src/util/backports.spec.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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
Expand Down
31 changes: 11 additions & 20 deletions src/util/backports.ts
Original file line number Diff line number Diff line change
@@ -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,
{
Expand Down Expand Up @@ -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 || '',
Expand All @@ -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',
Expand Down
15 changes: 13 additions & 2 deletions src/util/logger.ts
Original file line number Diff line number Diff line change
@@ -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 }

0 comments on commit 5ef980f

Please sign in to comment.