|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { after, beforeEach, describe, it, mock } from 'node:test'; |
| 3 | +import { ConsoleLogger, LogLevel } from './index.js'; |
| 4 | + |
| 5 | +describe('Logger', () => { |
| 6 | + describe('ConsoleLogger', () => { |
| 7 | + let logger: ConsoleLogger; |
| 8 | + |
| 9 | + /** |
| 10 | + * A collection of included console loggers to restore later. |
| 11 | + */ |
| 12 | + const output = { |
| 13 | + debug: console.debug, |
| 14 | + info: console.info, |
| 15 | + warn: console.warn, |
| 16 | + error: console.error, |
| 17 | + }; |
| 18 | + |
| 19 | + /** |
| 20 | + * The set of loggers to mock for checking outputs. |
| 21 | + */ |
| 22 | + const mocks = { |
| 23 | + debug: mock.fn(), |
| 24 | + info: mock.fn(), |
| 25 | + warn: mock.fn(), |
| 26 | + error: mock.fn(), |
| 27 | + }; |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + console.debug = mocks.debug = mock.fn(); |
| 31 | + console.info = mocks.info = mock.fn(); |
| 32 | + console.warn = mocks.warn = mock.fn(); |
| 33 | + console.error = mocks.error = mock.fn(); |
| 34 | + logger = new ConsoleLogger(); |
| 35 | + }); |
| 36 | + |
| 37 | + after(() => { |
| 38 | + console.debug = output.debug; |
| 39 | + console.info = output.info; |
| 40 | + console.warn = output.warn; |
| 41 | + console.error = mocks.error; |
| 42 | + }); |
| 43 | + |
| 44 | + describe('getLevel', () => { |
| 45 | + it('should have the default LogLevel', () => { |
| 46 | + assert.strictEqual(logger.getLevel(), LogLevel.INFO); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should get LogLevel corrrectly', () => { |
| 50 | + for (const level of [LogLevel.DEBUG, LogLevel.ERROR, LogLevel.WARN, LogLevel.INFO]) { |
| 51 | + logger.setLevel(level); |
| 52 | + assert.strictEqual(logger.getLevel(), level); |
| 53 | + } |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('setLevel', () => { |
| 58 | + it('should set the log level', () => { |
| 59 | + logger.setLevel(LogLevel.DEBUG); |
| 60 | + |
| 61 | + logger.debug('i am debug'); |
| 62 | + logger.info('i am info'); |
| 63 | + |
| 64 | + assert.strictEqual(mocks.debug.mock.callCount(), 1); |
| 65 | + assert.strictEqual(mocks.info.mock.callCount(), 1); |
| 66 | + |
| 67 | + logger.setLevel(LogLevel.INFO); |
| 68 | + |
| 69 | + logger.debug('i am debug'); |
| 70 | + logger.info('i am info'); |
| 71 | + |
| 72 | + assert.strictEqual(mocks.debug.mock.callCount(), 1); |
| 73 | + assert.strictEqual(mocks.info.mock.callCount(), 2); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('setName', () => { |
| 78 | + it('should set the name', () => { |
| 79 | + logger.setName('foobles'); |
| 80 | + |
| 81 | + logger.info('test'); |
| 82 | + |
| 83 | + assert.strictEqual(mocks.info.mock.callCount(), 1); |
| 84 | + assert.deepEqual(mocks.info.mock.calls[0].arguments, ['[INFO] ', 'foobles', 'test']); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('log levels', () => { |
| 89 | + describe('debug', () => { |
| 90 | + it('should write debug level messages', () => { |
| 91 | + logger.setLevel(LogLevel.DEBUG); |
| 92 | + |
| 93 | + logger.debug('debug'); |
| 94 | + logger.info('info'); |
| 95 | + logger.warn('warn'); |
| 96 | + logger.error('error'); |
| 97 | + |
| 98 | + assert.strictEqual(mocks.debug.mock.callCount(), 1); |
| 99 | + assert.strictEqual(mocks.info.mock.callCount(), 1); |
| 100 | + assert.strictEqual(mocks.warn.mock.callCount(), 1); |
| 101 | + assert.strictEqual(mocks.error.mock.callCount(), 1); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + describe('info', () => { |
| 106 | + it('should write info level messages', () => { |
| 107 | + logger.setLevel(LogLevel.INFO); |
| 108 | + |
| 109 | + logger.debug('debug'); |
| 110 | + logger.info('info'); |
| 111 | + logger.warn('warn'); |
| 112 | + logger.error('error'); |
| 113 | + |
| 114 | + assert.strictEqual(mocks.debug.mock.callCount(), 0); |
| 115 | + assert.strictEqual(mocks.info.mock.callCount(), 1); |
| 116 | + assert.strictEqual(mocks.warn.mock.callCount(), 1); |
| 117 | + assert.strictEqual(mocks.error.mock.callCount(), 1); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + describe('warn', () => { |
| 122 | + it('should write warn level messages', () => { |
| 123 | + logger.setLevel(LogLevel.WARN); |
| 124 | + |
| 125 | + logger.debug('debug'); |
| 126 | + logger.info('info'); |
| 127 | + logger.warn('warn'); |
| 128 | + logger.error('error'); |
| 129 | + |
| 130 | + assert.strictEqual(mocks.debug.mock.callCount(), 0); |
| 131 | + assert.strictEqual(mocks.info.mock.callCount(), 0); |
| 132 | + assert.strictEqual(mocks.warn.mock.callCount(), 1); |
| 133 | + assert.strictEqual(mocks.error.mock.callCount(), 1); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + describe('error', () => { |
| 138 | + it('should write error level messages', () => { |
| 139 | + logger.setLevel(LogLevel.ERROR); |
| 140 | + |
| 141 | + logger.debug('debug'); |
| 142 | + logger.info('info'); |
| 143 | + logger.warn('warn'); |
| 144 | + logger.error('error'); |
| 145 | + |
| 146 | + assert.strictEqual(mocks.debug.mock.callCount(), 0); |
| 147 | + assert.strictEqual(mocks.info.mock.callCount(), 0); |
| 148 | + assert.strictEqual(mocks.warn.mock.callCount(), 0); |
| 149 | + assert.strictEqual(mocks.error.mock.callCount(), 1); |
| 150 | + }); |
| 151 | + }); |
| 152 | + }); |
| 153 | + }); |
| 154 | +}); |
0 commit comments