Skip to content

Commit

Permalink
fix(logger): use global instance of console/null logger for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Mar 30, 2020
1 parent 852045f commit c55e154
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
9 changes: 4 additions & 5 deletions 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;
}
}

Expand Down
4 changes: 3 additions & 1 deletion 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);
}
25 changes: 25 additions & 0 deletions 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);
});
});
});

0 comments on commit c55e154

Please sign in to comment.