Skip to content

Commit

Permalink
logger: use subscribeSpyTo in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavohenke committed Oct 18, 2023
1 parent a61ef39 commit ac668c7
Showing 1 changed file with 56 additions and 55 deletions.
111 changes: 56 additions & 55 deletions src/logger.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { subscribeSpyTo } from '@hirez_io/observer-spy';
import chalk from 'chalk';

import { FakeCommand } from './fixtures/fake-command';
import { Logger } from './logger';

let emitSpy: jest.SpyInstance;

beforeEach(() => {
// Force chalk to use colors, otherwise tests may pass when they were supposed to be failing.
chalk.level = 3;
Expand All @@ -13,58 +12,60 @@ beforeEach(() => {
const createLogger = (...options: ConstructorParameters<typeof Logger>) => {
const logger = new Logger(...options);
jest.spyOn(logger, 'log');
emitSpy = jest.spyOn(logger, 'emit');
return logger;
const spy = subscribeSpyTo(logger.output);
return { logger, spy };
};

describe('#log()', () => {
it('writes prefix + text to the output stream', () => {
const logger = createLogger({});
it('emits prefix + text in the output stream', () => {
const { logger, spy } = createLogger({});
logger.log('foo', 'bar');

expect(logger.emit).toHaveBeenCalledTimes(2);
expect(logger.emit).toHaveBeenCalledWith(undefined, 'foo');
expect(logger.emit).toHaveBeenCalledWith(undefined, 'bar');
const values = spy.getValues();
expect(values).toHaveLength(2);
expect(values[0]).toEqual({ command: undefined, text: 'foo' });
expect(values[1]).toEqual({ command: undefined, text: 'bar' });
});

it('writes multiple lines of text with prefix on each', () => {
const logger = createLogger({});
it('emits multiple lines of text with prefix on each', () => {
const { logger, spy } = createLogger({});
logger.log('foo', 'bar\nbaz\n');

expect(logger.emit).toHaveBeenCalledTimes(2);
expect(logger.emit).toHaveBeenCalledWith(undefined, 'foo');
expect(logger.emit).toHaveBeenCalledWith(undefined, 'bar\nfoobaz\n');
const values = spy.getValues();
expect(values).toHaveLength(2);
expect(values[0]).toEqual({ command: undefined, text: 'foo' });
expect(values[1]).toEqual({ command: undefined, text: 'bar\nfoobaz\n' });
});

it('does not prepend prefix if last call did not finish with a LF', () => {
const logger = createLogger({});
it('does not emit prefix if last call did not finish with a LF', () => {
const { logger, spy } = createLogger({});
logger.log('foo', 'bar');
emitSpy.mockClear();
logger.log('foo', 'baz');

expect(logger.emit).toHaveBeenCalledTimes(1);
expect(logger.emit).toHaveBeenCalledWith(undefined, 'baz');
expect(spy.getValuesLength()).toBe(3);
expect(spy.getLastValue()).toEqual({ command: undefined, text: 'baz' });
});

it('does not prepend prefix or handle text if logger is in raw mode', () => {
const logger = createLogger({ raw: true });
it('does not emit prefix nor handle text if logger is in raw mode', () => {
const { logger, spy } = createLogger({ raw: true });
logger.log('foo', 'bar\nbaz\n');

expect(logger.emit).toHaveBeenCalledTimes(1);
expect(logger.emit).toHaveBeenCalledWith(undefined, 'bar\nbaz\n');
const values = spy.getValues();
expect(values).toHaveLength(1);
expect(values[0]).toEqual({ command: undefined, text: 'bar\nbaz\n' });
});
});

describe('#logGlobalEvent()', () => {
it('does nothing if in raw mode', () => {
const logger = createLogger({ raw: true });
const { logger } = createLogger({ raw: true });
logger.logGlobalEvent('foo');

expect(logger.log).not.toHaveBeenCalled();
});

it('logs in gray dim style with arrow prefix', () => {
const logger = createLogger({});
const { logger } = createLogger({});
logger.logGlobalEvent('foo');

expect(logger.log).toHaveBeenCalledWith(
Expand All @@ -76,23 +77,23 @@ describe('#logGlobalEvent()', () => {

describe('#logCommandText()', () => {
it('logs with name if no prefixFormat is set', () => {
const logger = createLogger({});
const { logger } = createLogger({});
const cmd = new FakeCommand('bla');
logger.logCommandText('foo', cmd);

expect(logger.log).toHaveBeenCalledWith(chalk.reset('[bla]') + ' ', 'foo', cmd);
});

it('logs with index if no prefixFormat is set, and command has no name', () => {
const logger = createLogger({});
const { logger } = createLogger({});
const cmd = new FakeCommand('', undefined, 2);
logger.logCommandText('foo', cmd);

expect(logger.log).toHaveBeenCalledWith(chalk.reset('[2]') + ' ', 'foo', cmd);
});

it('logs with prefixFormat set to pid', () => {
const logger = createLogger({ prefixFormat: 'pid' });
const { logger } = createLogger({ prefixFormat: 'pid' });
const cmd = new FakeCommand();
cmd.pid = 123;
logger.logCommandText('foo', cmd);
Expand All @@ -101,23 +102,23 @@ describe('#logCommandText()', () => {
});

it('logs with prefixFormat set to name', () => {
const logger = createLogger({ prefixFormat: 'name' });
const { logger } = createLogger({ prefixFormat: 'name' });
const cmd = new FakeCommand('bar');
logger.logCommandText('foo', cmd);

expect(logger.log).toHaveBeenCalledWith(chalk.reset('[bar]') + ' ', 'foo', cmd);
});

it('logs with prefixFormat set to index', () => {
const logger = createLogger({ prefixFormat: 'index' });
const { logger } = createLogger({ prefixFormat: 'index' });
const cmd = new FakeCommand(undefined, undefined, 3);
logger.logCommandText('foo', cmd);

expect(logger.log).toHaveBeenCalledWith(chalk.reset('[3]') + ' ', 'foo', cmd);
});

it('logs with prefixFormat set to time (with timestampFormat)', () => {
const logger = createLogger({ prefixFormat: 'time', timestampFormat: 'yyyy' });
const { logger } = createLogger({ prefixFormat: 'time', timestampFormat: 'yyyy' });
const cmd = new FakeCommand();
logger.logCommandText('foo', cmd);

Expand All @@ -126,47 +127,47 @@ describe('#logCommandText()', () => {
});

it('logs with templated prefixFormat', () => {
const logger = createLogger({ prefixFormat: '{index}-{name}' });
const { logger } = createLogger({ prefixFormat: '{index}-{name}' });
const cmd = new FakeCommand('bar');
logger.logCommandText('foo', cmd);

expect(logger.log).toHaveBeenCalledWith(chalk.reset('0-bar') + ' ', 'foo', cmd);
});

it('does not strip spaces from beginning or end of prefixFormat', () => {
const logger = createLogger({ prefixFormat: ' {index}-{name} ' });
const { logger } = createLogger({ prefixFormat: ' {index}-{name} ' });
const cmd = new FakeCommand('bar');
logger.logCommandText('foo', cmd);

expect(logger.log).toHaveBeenCalledWith(chalk.reset(' 0-bar ') + ' ', 'foo', cmd);
});

it('logs with no prefix', () => {
const logger = createLogger({ prefixFormat: 'none' });
const { logger } = createLogger({ prefixFormat: 'none' });
const cmd = new FakeCommand();
logger.logCommandText('foo', cmd);

expect(logger.log).toHaveBeenCalledWith(chalk.reset(''), 'foo', cmd);
});

it('logs prefix using command line itself', () => {
const logger = createLogger({ prefixFormat: 'command' });
const { logger } = createLogger({ prefixFormat: 'command' });
const cmd = new FakeCommand();
logger.logCommandText('foo', cmd);

expect(logger.log).toHaveBeenCalledWith(chalk.reset('[echo foo]') + ' ', 'foo', cmd);
});

it('logs prefix using command line itself, capped at prefixLength bytes', () => {
const logger = createLogger({ prefixFormat: 'command', prefixLength: 6 });
const { logger } = createLogger({ prefixFormat: 'command', prefixLength: 6 });
const cmd = new FakeCommand();
logger.logCommandText('foo', cmd);

expect(logger.log).toHaveBeenCalledWith(chalk.reset('[ec..oo]') + ' ', 'foo', cmd);
});

it('logs prefix using prefixColor from command', () => {
const logger = createLogger({});
const { logger } = createLogger({});
const cmd = new FakeCommand('', undefined, 1, {
prefixColor: 'blue',
});
Expand All @@ -176,7 +177,7 @@ describe('#logCommandText()', () => {
});

it('logs prefix in gray dim if prefixColor from command does not exist', () => {
const logger = createLogger({});
const { logger } = createLogger({});
const cmd = new FakeCommand('', undefined, 1, {
prefixColor: 'blue.fake',
});
Expand All @@ -186,7 +187,7 @@ describe('#logCommandText()', () => {
});

it('logs prefix using prefixColor from command if prefixColor is a hex value', () => {
const logger = createLogger({});
const { logger } = createLogger({});
const prefixColor = '#32bd8a';
const cmd = new FakeCommand('', undefined, 1, {
prefixColor,
Expand All @@ -197,15 +198,15 @@ describe('#logCommandText()', () => {
});

it('does nothing if command is hidden by name', () => {
const logger = createLogger({ hide: ['abc'] });
const { logger } = createLogger({ hide: ['abc'] });
const cmd = new FakeCommand('abc');
logger.logCommandText('foo', cmd);

expect(logger.log).not.toHaveBeenCalled();
});

it('does nothing if command is hidden by index', () => {
const logger = createLogger({ hide: [3] });
const { logger } = createLogger({ hide: [3] });
const cmd = new FakeCommand('', undefined, 3);
logger.logCommandText('foo', cmd);

Expand All @@ -215,30 +216,30 @@ describe('#logCommandText()', () => {

describe('#logCommandEvent()', () => {
it('does nothing if in raw mode', () => {
const logger = createLogger({ raw: true });
const { logger } = createLogger({ raw: true });
logger.logCommandEvent('foo', new FakeCommand());

expect(logger.log).not.toHaveBeenCalled();
});

it('does nothing if command is hidden by name', () => {
const logger = createLogger({ hide: ['abc'] });
const { logger } = createLogger({ hide: ['abc'] });
const cmd = new FakeCommand('abc');
logger.logCommandEvent('foo', cmd);

expect(logger.log).not.toHaveBeenCalled();
});

it('does nothing if command is hidden by index', () => {
const logger = createLogger({ hide: [3] });
const { logger } = createLogger({ hide: [3] });
const cmd = new FakeCommand('', undefined, 3);
logger.logCommandEvent('foo', cmd);

expect(logger.log).not.toHaveBeenCalled();
});

it('logs text in gray dim', () => {
const logger = createLogger({});
const { logger } = createLogger({});
const cmd = new FakeCommand('', undefined, 1);
logger.logCommandEvent('foo', cmd);

Expand All @@ -252,14 +253,14 @@ describe('#logCommandEvent()', () => {

describe('#logTable()', () => {
it('does not log anything in raw mode', () => {
const logger = createLogger({ raw: true });
const { logger } = createLogger({ raw: true });
logger.logTable([{ foo: 1, bar: 2 }]);

expect(logger.log).not.toHaveBeenCalled();
});

it('does not log anything if value is not an array', () => {
const logger = createLogger({});
const { logger } = createLogger({});
logger.logTable({} as never);
logger.logTable(null as never);
logger.logTable(0 as never);
Expand All @@ -269,21 +270,21 @@ describe('#logTable()', () => {
});

it('does not log anything if array is empy', () => {
const logger = createLogger({});
const { logger } = createLogger({});
logger.logTable([]);

expect(logger.log).not.toHaveBeenCalled();
});

it('does not log anything if array items have no properties', () => {
const logger = createLogger({});
const { logger } = createLogger({});
logger.logTable([{}]);

expect(logger.log).not.toHaveBeenCalled();
});

it("logs a header for each item's properties", () => {
const logger = createLogger({});
const { logger } = createLogger({});
logger.logTable([{ foo: 1, bar: 2 }]);

expect(logger.log).toHaveBeenCalledWith(
Expand All @@ -293,7 +294,7 @@ describe('#logTable()', () => {
});

it("logs padded headers according to longest column's value", () => {
const logger = createLogger({});
const { logger } = createLogger({});
logger.logTable([{ a: 'foo', b: 'barbaz' }]);

expect(logger.log).toHaveBeenCalledWith(
Expand All @@ -303,7 +304,7 @@ describe('#logTable()', () => {
});

it("logs each items's values", () => {
const logger = createLogger({});
const { logger } = createLogger({});
logger.logTable([{ foo: 123 }, { foo: 456 }]);

expect(logger.log).toHaveBeenCalledWith(
Expand All @@ -317,7 +318,7 @@ describe('#logTable()', () => {
});

it("logs each items's values with empty column", () => {
const logger = createLogger({});
const { logger } = createLogger({});
logger.logTable([{ foo: 123 }, { foo: null }]);

expect(logger.log).toHaveBeenCalledWith(
Expand All @@ -331,7 +332,7 @@ describe('#logTable()', () => {
});

it("logs each items's values padded according to longest column's value", () => {
const logger = createLogger({});
const { logger } = createLogger({});
logger.logTable([{ foo: 1 }, { foo: 123 }]);

expect(logger.log).toHaveBeenCalledWith(
Expand All @@ -341,7 +342,7 @@ describe('#logTable()', () => {
});

it('logs items with different properties in each', () => {
const logger = createLogger({});
const { logger } = createLogger({});
logger.logTable([{ foo: 1 }, { bar: 2 }]);

expect(logger.log).toHaveBeenCalledWith(
Expand Down

0 comments on commit ac668c7

Please sign in to comment.