From ac668c7ae7f56c0ffb8eb69d61a11a9e70c6391a Mon Sep 17 00:00:00 2001 From: Gustavo Henke Date: Wed, 18 Oct 2023 12:38:12 +1100 Subject: [PATCH] logger: use subscribeSpyTo in tests --- src/logger.spec.ts | 111 +++++++++++++++++++++++---------------------- 1 file changed, 56 insertions(+), 55 deletions(-) diff --git a/src/logger.spec.ts b/src/logger.spec.ts index 6d75cc4f..caee89dc 100644 --- a/src/logger.spec.ts +++ b/src/logger.spec.ts @@ -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; @@ -13,58 +12,60 @@ beforeEach(() => { const createLogger = (...options: ConstructorParameters) => { 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( @@ -76,7 +77,7 @@ 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); @@ -84,7 +85,7 @@ describe('#logCommandText()', () => { }); 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); @@ -92,7 +93,7 @@ describe('#logCommandText()', () => { }); 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); @@ -101,7 +102,7 @@ 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); @@ -109,7 +110,7 @@ describe('#logCommandText()', () => { }); 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); @@ -117,7 +118,7 @@ describe('#logCommandText()', () => { }); 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); @@ -126,7 +127,7 @@ 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); @@ -134,7 +135,7 @@ describe('#logCommandText()', () => { }); 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); @@ -142,7 +143,7 @@ describe('#logCommandText()', () => { }); it('logs with no prefix', () => { - const logger = createLogger({ prefixFormat: 'none' }); + const { logger } = createLogger({ prefixFormat: 'none' }); const cmd = new FakeCommand(); logger.logCommandText('foo', cmd); @@ -150,7 +151,7 @@ describe('#logCommandText()', () => { }); 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); @@ -158,7 +159,7 @@ describe('#logCommandText()', () => { }); 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); @@ -166,7 +167,7 @@ describe('#logCommandText()', () => { }); it('logs prefix using prefixColor from command', () => { - const logger = createLogger({}); + const { logger } = createLogger({}); const cmd = new FakeCommand('', undefined, 1, { prefixColor: 'blue', }); @@ -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', }); @@ -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, @@ -197,7 +198,7 @@ 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); @@ -205,7 +206,7 @@ describe('#logCommandText()', () => { }); 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); @@ -215,14 +216,14 @@ 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); @@ -230,7 +231,7 @@ describe('#logCommandEvent()', () => { }); 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); @@ -238,7 +239,7 @@ describe('#logCommandEvent()', () => { }); it('logs text in gray dim', () => { - const logger = createLogger({}); + const { logger } = createLogger({}); const cmd = new FakeCommand('', undefined, 1); logger.logCommandEvent('foo', cmd); @@ -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); @@ -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( @@ -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( @@ -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( @@ -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( @@ -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( @@ -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(