|
| 1 | +import { expect } from '@assertive-ts/core'; |
| 2 | +import { FunctionsLogger } from '../../src/admin/logger/FunctionsLogger'; |
| 3 | +import * as logger from 'firebase-functions/logger'; |
| 4 | +import { capture, spy } from 'ts-mockito'; |
| 5 | + |
| 6 | +describe('FunctionsLogger', () => { |
| 7 | + let functionsLogger: FunctionsLogger; |
| 8 | + let spiedLoggger: typeof logger; |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + functionsLogger = new FunctionsLogger(); |
| 12 | + spiedLoggger = spy(logger); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should log a debug message', () => { |
| 16 | + functionsLogger.log('debug', true, 'testFunction', 'test description', { key: 'value' }); |
| 17 | + expect(capture(spiedLoggger.write).first()[0]).toBeEqual({ |
| 18 | + severity: 'DEBUG', |
| 19 | + message: '[DEBUG: testFunction] test description | {\n\tkey: \'value\'\n}' |
| 20 | + }); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should log an info message', () => { |
| 24 | + functionsLogger.log('info', true, 'testFunction', 'test description', { key: 'value' }); |
| 25 | + expect(capture(spiedLoggger.write).first()[0]).toBeEqual({ |
| 26 | + severity: 'INFO', |
| 27 | + message: '[INFO: testFunction] test description | {\n\tkey: \'value\'\n}' |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should log a warnin message', () => { |
| 32 | + functionsLogger.log('notice', true, 'testFunction', 'test description', { key: 'value' }); |
| 33 | + expect(capture(spiedLoggger.write).first()[0]).toBeEqual({ |
| 34 | + severity: 'NOTICE', |
| 35 | + message: '[NOTICE: testFunction] test description | {\n\tkey: \'value\'\n}' |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should log a warning message', () => { |
| 40 | + functionsLogger.log('warning', true, 'testFunction', 'test description', { key: 'value' }); |
| 41 | + expect(capture(spiedLoggger.write).first()[0]).toBeEqual({ |
| 42 | + severity: 'WARNING', |
| 43 | + message: '[WARNING: testFunction] test description | {\n\tkey: \'value\'\n}' |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should log an error message', () => { |
| 48 | + functionsLogger.log('error', true, 'testFunction', 'test description', { key: 'value' }); |
| 49 | + expect(capture(spiedLoggger.write).first()[0]).toBeEqual({ |
| 50 | + severity: 'ERROR', |
| 51 | + message: '[ERROR: testFunction] test description | {\n\tkey: \'value\'\n}' |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should log a critical message', () => { |
| 56 | + functionsLogger.log('critical', true, 'testFunction', 'test description', { key: 'value' }); |
| 57 | + expect(capture(spiedLoggger.write).first()[0]).toBeEqual({ |
| 58 | + severity: 'CRITICAL', |
| 59 | + message: '[CRITICAL: testFunction] test description | {\n\tkey: \'value\'\n}' |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should log an alert message', () => { |
| 64 | + functionsLogger.log('alert', true, 'testFunction', 'test description', { key: 'value' }); |
| 65 | + expect(capture(spiedLoggger.write).first()[0]).toBeEqual({ |
| 66 | + severity: 'ALERT', |
| 67 | + message: '[ALERT: testFunction] test description | {\n\tkey: \'value\'\n}' |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should log an emergency message', () => { |
| 72 | + functionsLogger.log('emergency', true, 'testFunction', 'test description', { key: 'value' }); |
| 73 | + expect(capture(spiedLoggger.write).first()[0]).toBeEqual({ |
| 74 | + severity: 'EMERGENCY', |
| 75 | + message: '[EMERGENCY: testFunction] test description | {\n\tkey: \'value\'\n}' |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should log a message with null description and details', () => { |
| 80 | + functionsLogger.log('info', true, 'testFunction', null, null); |
| 81 | + expect(capture(spiedLoggger.write).first()[0]).toBeEqual({ |
| 82 | + severity: 'INFO', |
| 83 | + message: '[INFO: testFunction]' |
| 84 | + }); |
| 85 | + }); |
| 86 | +}); |
0 commit comments