embark/packages/core/logger/test/logger.spec.js

93 lines
2.5 KiB
JavaScript
Raw Normal View History

import assert from 'assert';
import sinon from 'sinon';
import { fakeEmbark } from 'embark-testing';
describe('core/logger', () => {
fix: ensure that packages properly specify their dependencies Many packages in the monorepo did not specify all of their dependencies; they were effectively relying on resolution in the monorepo's root `node_modules`. In a production release of `embark` and `embark[js]-*` packages this can lead to broken packages. To fix the problem currently and to help prevent it from happening again, make use of the `eslint-plugin-import` package's `import/no-extraneous-dependencies` and `import/no-unresolved` rules. In the root `tslint.json` set `"no-implicit-dependencies": true`, wich is the tslint equivalent of `import/no-extraneous-dependencies`; there is no tslint equivalent for `import/no-unresolved`, but we will eventually replace tslint with an eslint configuration that checks both `.js` and `.ts` files. For `import/no-unresolved` to work in our monorepo setup, in most packages add an `index.js` that has: ```js module.exports = require('./dist'); // or './dist/lib' in some cases ``` And point `"main"` in `package.json` to `"./index.js"`. Despite what's indicated in npm's documentation for `package.json`, it's also necessary to add `"index.js"` to the `"files"` array. Make sure that all `.js` files that can and should be linted are in fact linted. For example, files in `packages/embark/src/cmd/` weren't being linted and many test suites weren't being linted. Bump all relevant packages to `eslint@6.8.0`. Fix all linter errors that arose after these changes. Implement a `check-yarn-lock` script that's run as part of `"ci:full"` and `"qa:full"`, and can manually be invoked via `yarn cylock` in the root of the monorepo. The script exits with error if any specifiers are found in `yarn.lock` for `embark[js][-*]` and/or `@embarklabs/*` (with a few exceptions, cf. `scripts/check-yarn-lock.js`).
2020-02-20 23:47:01 +00:00
const { embark } = fakeEmbark();
let logger, testLogFn, resolve;
const logFile = 'embark.log';
const fsMock = {
logs: {},
appendFile: (file, content, callback) => {
fsMock.logs[file].push(content);
if (resolve) resolve();
callback();
},
ensureFileSync: (file) => {
if (!fsMock.logs[file]) {
fsMock.logs[file] = [];
}
}
};
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;
});
beforeEach(() => {
fsMock.logs[logFile] = [];
resolve = null;
testLogFn = sinon.fake();
logger = new Logger({
events: embark.events,
logFile,
logFunction: testLogFn,
fs: fsMock
});
});
afterEach(() => {
embark.teardown();
sinon.restore();
});
test('it should use custom log function for logging', async () => {
const promise = new Promise(res => { resolve = res; });
logger.info('Hello world');
assert(testLogFn.calledOnce);
await promise;
});
test('it should inject color encoding based on log method', async () => {
logger.info('Hello world');
assert(testLogFn.calledWith('\u001b[32mHello world\u001b[39m'));
logger.warn('Hello world');
assert(testLogFn.calledWith('\u001b[33mHello world\u001b[39m'));
const promise = new Promise(res => { resolve = res; });
logger.error('Hello world');
assert(testLogFn.calledWith('\u001b[31mHello world\u001b[39m'));
await promise;
});
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')
));
});
test('it should not log if log method level is higher than configured log level', async () => {
logger.trace('Hello world');
// default log level is `info` which is lower than `trace`
assert.ok(!testLogFn.calledOnce);
const promise = new Promise(res => { resolve = res; });
logger.warn('Test');
assert.ok(testLogFn.calledOnce);
await promise;
});
});