2020-01-15 13:20:12 +00:00
|
|
|
import assert from 'assert';
|
|
|
|
import sinon from 'sinon';
|
|
|
|
import { fakeEmbark } from 'embark-testing';
|
|
|
|
|
|
|
|
describe('core/logger', () => {
|
|
|
|
|
2020-02-20 23:47:01 +00:00
|
|
|
const { embark } = fakeEmbark();
|
2020-01-15 13:20:12 +00:00
|
|
|
|
2020-01-29 01:00:23 +00:00
|
|
|
let logger, testLogFn, resolve;
|
|
|
|
const logFile = 'embark.log';
|
2020-01-15 13:20:12 +00:00
|
|
|
|
2020-01-17 12:10:40 +00:00
|
|
|
const fsMock = {
|
|
|
|
logs: {},
|
|
|
|
appendFile: (file, content, callback) => {
|
2020-01-29 01:00:23 +00:00
|
|
|
fsMock.logs[file].push(content);
|
|
|
|
if (resolve) resolve();
|
|
|
|
callback();
|
|
|
|
},
|
|
|
|
ensureFileSync: (file) => {
|
2020-01-17 12:10:40 +00:00
|
|
|
if (!fsMock.logs[file]) {
|
|
|
|
fsMock.logs[file] = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-29 01:00:23 +00:00
|
|
|
let Logger;
|
|
|
|
beforeAll(() => {
|
|
|
|
process.env.FORCE_COLOR = '1';
|
|
|
|
// load the module only after the environment variable is set, otherwise
|
|
|
|
// the colors package will already have been loaded and in CI the result
|
|
|
|
// will be that colors are disabled and tests below will fail
|
|
|
|
({Logger} = require('../src'));
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
delete process.env.FORCE_COLOR;
|
|
|
|
});
|
2020-01-15 13:20:12 +00:00
|
|
|
|
2020-01-29 01:00:23 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
fsMock.logs[logFile] = [];
|
|
|
|
resolve = null;
|
2020-01-15 13:20:12 +00:00
|
|
|
testLogFn = sinon.fake();
|
|
|
|
logger = new Logger({
|
|
|
|
events: embark.events,
|
2020-01-29 01:00:23 +00:00
|
|
|
logFile,
|
2020-01-15 13:20:12 +00:00
|
|
|
logFunction: testLogFn,
|
2020-01-17 12:10:40 +00:00
|
|
|
fs: fsMock
|
2020-01-15 13:20:12 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
embark.teardown();
|
|
|
|
sinon.restore();
|
|
|
|
});
|
|
|
|
|
2020-01-29 01:00:23 +00:00
|
|
|
test('it should use custom log function for logging', async () => {
|
|
|
|
const promise = new Promise(res => { resolve = res; });
|
2020-01-15 13:20:12 +00:00
|
|
|
logger.info('Hello world');
|
|
|
|
assert(testLogFn.calledOnce);
|
2020-01-29 01:00:23 +00:00
|
|
|
await promise;
|
2020-01-15 13:20:12 +00:00
|
|
|
});
|
|
|
|
|
2020-01-29 01:00:23 +00:00
|
|
|
test('it should inject color encoding based on log method', async () => {
|
2020-01-15 13:20:12 +00:00
|
|
|
logger.info('Hello world');
|
|
|
|
assert(testLogFn.calledWith('\u001b[32mHello world\u001b[39m'));
|
|
|
|
logger.warn('Hello world');
|
|
|
|
assert(testLogFn.calledWith('\u001b[33mHello world\u001b[39m'));
|
2020-01-29 01:00:23 +00:00
|
|
|
const promise = new Promise(res => { resolve = res; });
|
2020-01-15 13:20:12 +00:00
|
|
|
logger.error('Hello world');
|
|
|
|
assert(testLogFn.calledWith('\u001b[31mHello world\u001b[39m'));
|
2020-01-29 01:00:23 +00:00
|
|
|
await promise;
|
2020-01-15 13:20:12 +00:00
|
|
|
});
|
|
|
|
|
2020-01-29 01:00:23 +00:00
|
|
|
test('it should write logs to log file', async () => {
|
|
|
|
const promise = new Promise(res => { resolve = res; });
|
|
|
|
logger.info('Some test log');
|
|
|
|
await promise;
|
|
|
|
assert(fsMock.logs[logFile].some(
|
|
|
|
entry => entry.includes('[info] Some test log')
|
|
|
|
));
|
2020-01-15 13:20:12 +00:00
|
|
|
});
|
|
|
|
|
2020-01-29 01:00:23 +00:00
|
|
|
test('it should not log if log method level is higher than configured log level', async () => {
|
2020-01-15 13:20:12 +00:00
|
|
|
logger.trace('Hello world');
|
|
|
|
// default log level is `info` which is lower than `trace`
|
|
|
|
assert.ok(!testLogFn.calledOnce);
|
2020-01-29 01:00:23 +00:00
|
|
|
const promise = new Promise(res => { resolve = res; });
|
2020-01-15 13:20:12 +00:00
|
|
|
logger.warn('Test');
|
|
|
|
assert.ok(testLogFn.calledOnce);
|
2020-01-29 01:00:23 +00:00
|
|
|
await promise;
|
2020-01-15 13:20:12 +00:00
|
|
|
});
|
|
|
|
});
|