From c55e154570150947c401be860e3b4e09f10f7fbe Mon Sep 17 00:00:00 2001 From: ssube Date: Mon, 30 Mar 2020 18:23:53 -0500 Subject: [PATCH] fix(logger): use global instance of console/null logger for tests --- src/Logger.ts | 9 ++++----- src/utils/Env.ts | 4 +++- test/TestLogger.ts | 25 +++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 test/TestLogger.ts diff --git a/src/Logger.ts b/src/Logger.ts index 49bdd7c7..12668c32 100644 --- a/src/Logger.ts +++ b/src/Logger.ts @@ -1,12 +1,11 @@ import { ConsoleLogger, Logger, NullLogger } from 'noicejs'; - -const ENV_DEBUG = 'DEBUG'; +import { isDebug } from './utils/Env'; export function getTestLogger(verbose = false): Logger { - if (verbose || process.env[ENV_DEBUG] === 'TRUE') { - return new ConsoleLogger(); + if (verbose || isDebug()) { + return ConsoleLogger.global; } else { - return new NullLogger(); + return NullLogger.global; } } diff --git a/src/utils/Env.ts b/src/utils/Env.ts index 96bc09f6..3020c070 100644 --- a/src/utils/Env.ts +++ b/src/utils/Env.ts @@ -1,3 +1,5 @@ +const ENV_DEBUG = 'DEBUG'; + export function isDebug() { - return Reflect.has(process.env, 'DEBUG'); + return Reflect.has(process.env, ENV_DEBUG); } diff --git a/test/TestLogger.ts b/test/TestLogger.ts new file mode 100644 index 00000000..7bc744e8 --- /dev/null +++ b/test/TestLogger.ts @@ -0,0 +1,25 @@ +import { expect } from 'chai'; +import { ConsoleLogger, NullLogger } from 'noicejs'; + +import { getTestLogger, spyLogger } from '../src/Logger'; + +describe('logger utils', () => { + describe('get test logger helper', () => { + it('should return console logger in verbose mode', () => { + expect(getTestLogger(true)).to.equal(ConsoleLogger.global); + }); + + it('should return console logger in debug mode'); + + it('should return null logger otherwise', () => { + expect(getTestLogger()).to.equal(NullLogger.global); + }); + }); + + describe('spy logger helper', () => { + it('should return itself as a child', () => { + const logger = spyLogger({}); + expect(logger.child({})).to.equal(logger); + }); + }); +});