Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
test: unit test processing of the --loglevel option
Browse files Browse the repository at this point in the history
  • Loading branch information
honzajavorek committed Jan 24, 2019
1 parent 9f87588 commit 5da4c8c
Show file tree
Hide file tree
Showing 2 changed files with 222 additions and 22 deletions.
10 changes: 7 additions & 3 deletions lib/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,21 @@ function coerceToArray(value) {


function applyLoggingOptions(options) {
// Color can be either specified as "stringified bool" or bool (nothing else
// is expected valid value). Here we're coercing the value to boolean.
// The CLI layer should handle 'color' and 'timestamp' and they should be
// implemented as --color/--no-color and --timestamp/--no-timestamp boolean
// options with no values
if (options.color === 'false') {
options.color = false;
} else if (options.color === 'true') {
options.color = true;
}

logger.transports.console.colorize = options.color;
reporterOutputLogger.transports.console.colorize = options.color;

logger.transports.console.timestamp = options.timestamp;
reporterOutputLogger.transports.console.timestamp = options.timestamp;

// Handling the 'loglevel' value
if (options.loglevel) {
const loglevel = options.loglevel.toLowerCase();
if (loglevel === 'silent') {
Expand All @@ -47,6 +48,9 @@ function applyLoggingOptions(options) {
logger.transports.console.level = 'warn';
}

// Once the reporters' output is handled in a different way than with
// a logger, this part will not be necessary anymore
reporterOutputLogger.transports.console.silent = false;
reporterOutputLogger.transports.console.level = 'info';

return options;
Expand Down
234 changes: 215 additions & 19 deletions test/unit/configuration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,232 @@ const { assert } = require('chai');

const configuration = require('../../lib/configuration');
const logger = require('../../lib/logger');
const reporterOutputLogger = require('../../lib/reporters/reporterOutputLogger');


describe('configuration.applyLoggingOptions()', () => {
let loggerSettings;
let config;
let reporterOutputLoggerSettings;

beforeEach(() => { loggerSettings = clone(logger.transports.console); });
afterEach(() => { logger.transports.console = loggerSettings; });
beforeEach(() => {
loggerSettings = clone(logger.transports.console);
reporterOutputLoggerSettings = clone(reporterOutputLogger.transports.console);
});
afterEach(() => {
logger.transports.console = loggerSettings;
reporterOutputLogger.transports.console = reporterOutputLoggerSettings;
});

it('applies logging options', () => {
config = configuration.applyLoggingOptions({
color: 'true',
loglevel: 'debug',
describe('with color set to true', () => {
let options;

beforeEach(() => {
options = configuration.applyLoggingOptions({ color: true });
});

assert.propertyVal(config, 'color', true);
assert.equal(logger.transports.console.colorize, true);
it('resulting configuration should contain \'color\' set to boolean true', () => {
assert.isTrue(options.color);
});

assert.propertyVal(config, 'loglevel', 'debug');
assert.equal(logger.transports.console.level, 'debug');
it('the application logger should be set to colorize', () => {
assert.isTrue(logger.transports.console.colorize);
});

it('the application output should be set to colorize', () => {
assert.isTrue(reporterOutputLogger.transports.console.colorize);
});
});

describe('with color set to legacy \'true\' string value', () => it('resulting configuration should contain \'color\' set to boolean true', () => {
const options = configuration.applyLoggingOptions({ color: 'true' });
assert.propertyVal(options, 'color', true);
}));
describe('with color set to \'true\' string value', () => {
let options;

describe('with color option set to legacy \'false\' string value', () => it('resulting configuration should contain \'color\' set to boolean false', () => {
const options = configuration.applyLoggingOptions({ color: 'false' });
assert.propertyVal(options, 'color', false);
}));
beforeEach(() => {
options = configuration.applyLoggingOptions({ color: 'true' });
});

it('resulting configuration should contain \'color\' set to boolean true', () => {
assert.isTrue(options.color);
});

it('the application logger should be set to colorize', () => {
assert.isTrue(logger.transports.console.colorize);
});

it('the application output should be set to colorize', () => {
assert.isTrue(reporterOutputLogger.transports.console.colorize);
});
});

describe('with color set to false', () => {
let options;

beforeEach(() => {
options = configuration.applyLoggingOptions({ color: false });
});

it('resulting configuration should contain \'color\' set to boolean false', () => {
assert.isFalse(options.color);
});

it('the application logger should be set not to colorize', () => {
assert.isFalse(logger.transports.console.colorize);
});

it('the application output should be set not to colorize', () => {
assert.isFalse(reporterOutputLogger.transports.console.colorize);
});
});

describe('with color set to \'false\' string value', () => {
let options;

beforeEach(() => {
options = configuration.applyLoggingOptions({ color: 'false' });
});

it('resulting configuration should contain \'color\' set to boolean false', () => {
assert.isFalse(options.color);
});

it('the application logger should be set not to colorize', () => {
assert.isFalse(logger.transports.console.colorize);
});

it('the application output should be set not to colorize', () => {
assert.isFalse(reporterOutputLogger.transports.console.colorize);
});
});

describe('with timestamp set to true', () => {
beforeEach(() => {
configuration.applyLoggingOptions({ timestamp: true });
});

it('the application logger should be set to add timestamps', () => {
assert.isTrue(logger.transports.console.timestamp);
});

it('the application output should be set to add timestamps', () => {
assert.isTrue(reporterOutputLogger.transports.console.timestamp);
});
});

describe('with timestamp set to false', () => {
beforeEach(() => {
configuration.applyLoggingOptions({ timestamp: false });
});

it('the application logger should be set not to add timestamps', () => {
assert.isFalse(logger.transports.console.timestamp);
});

it('the application output should be set not to add timestamps', () => {
assert.isFalse(reporterOutputLogger.transports.console.timestamp);
});
});

describe('with loglevel not set', () => {
beforeEach(() => {
configuration.applyLoggingOptions({});
});

it('the application logger level is set to warn', () => {
assert.equal(logger.transports.console.level, 'warn');
});

it('the application output logger is not influenced', () => {
assert.isFalse(reporterOutputLogger.transports.console.silent);
assert.equal(reporterOutputLogger.transports.console.level, 'info');
});
});

describe('with loglevel set to a valid value', () => {
beforeEach(() => {
configuration.applyLoggingOptions({ loglevel: 'error' });
});

it('the application logger level is set', () => {
assert.equal(logger.transports.console.level, 'error');
});

it('the application output logger is not influenced', () => {
assert.isFalse(reporterOutputLogger.transports.console.silent);
assert.equal(reporterOutputLogger.transports.console.level, 'info');
});
});

describe('with loglevel set to a valid value using uppercase', () => {
beforeEach(() => {
configuration.applyLoggingOptions({ loglevel: 'ERROR' });
});

it('the application logger level is set', () => {
assert.equal(logger.transports.console.level, 'error');
});
});

describe('with loglevel set to an invalid value', () => {
it('throws an exception', () => {
assert.throws(() => {
configuration.applyLoggingOptions({ loglevel: 'verbose' });
}, /unsupported.+verbose/i);
});
});

describe('with loglevel set to silent', () => {
beforeEach(() => {
configuration.applyLoggingOptions({ loglevel: 'silent' });
});

it('the application logger gets silenced', () => {
assert.isTrue(logger.transports.console.silent);
});

it('the application output logger is not influenced', () => {
assert.isFalse(reporterOutputLogger.transports.console.silent);
assert.equal(reporterOutputLogger.transports.console.level, 'info');
});
});

describe('with loglevel set to warn', () => {
beforeEach(() => {
configuration.applyLoggingOptions({ loglevel: 'warn' });
});

it('the application logger level is set to warn', () => {
assert.equal(logger.transports.console.level, 'warn');
});
});

describe('with loglevel set to warning', () => {
beforeEach(() => {
configuration.applyLoggingOptions({ loglevel: 'warning' });
});

it('the application logger level is set to warn', () => {
assert.equal(logger.transports.console.level, 'warn');
});
});

describe('with loglevel set to error', () => {
beforeEach(() => {
configuration.applyLoggingOptions({ loglevel: 'error' });
});

it('the application logger level is set to error', () => {
assert.equal(logger.transports.console.level, 'error');
});
});

describe('with loglevel set to debug', () => {
beforeEach(() => {
configuration.applyLoggingOptions({ loglevel: 'debug' });
});

it('the application logger level is set to debug', () => {
assert.equal(logger.transports.console.level, 'debug');
});
});
});

describe('configuration.applyConfiguration()', () => {
Expand Down

0 comments on commit 5da4c8c

Please sign in to comment.