|
| 1 | +import {mocked} from 'ts-jest/utils'; |
| 2 | +import Monitor from '../src/Monitor'; |
| 3 | +import {consoleLogger} from '../src/loggers'; |
| 4 | +import {MonitorOptions} from '../src'; |
| 5 | +import { |
| 6 | + assertIncrementWasCalled, |
| 7 | + assertIncrementWasNotCalled, |
| 8 | + assertTimingWasCalled, |
| 9 | + assertGaugeWasCalled, |
| 10 | +} from './utils'; |
| 11 | + |
| 12 | +jest.mock('../AsyncStatsD'); |
| 13 | +jest.mock('../loggers', () => ({ |
| 14 | + consoleLogger: { |
| 15 | + info: jest.fn(), |
| 16 | + debug: jest.fn(), |
| 17 | + warn: jest.fn(), |
| 18 | + error: jest.fn(), |
| 19 | + }, |
| 20 | + emptyLogger: { |
| 21 | + info: jest.fn(), |
| 22 | + debug: jest.fn(), |
| 23 | + warn: jest.fn(), |
| 24 | + error: jest.fn(), |
| 25 | + }, |
| 26 | +})); |
| 27 | +jest.mock('../parseError', () => ({ |
| 28 | + default: err => err, |
| 29 | +})); |
| 30 | + |
| 31 | +const statsdOptions: MonitorOptions['statsd'] = { |
| 32 | + apiKey: 'key', |
| 33 | + host: 'host', |
| 34 | + root: 'root', |
| 35 | +}; |
| 36 | + |
| 37 | +const defaultMonitorOptions: MonitorOptions = { |
| 38 | + serviceName: 'test-service', |
| 39 | + statsd: statsdOptions, |
| 40 | +}; |
| 41 | + |
| 42 | +describe('Monitor', () => { |
| 43 | + beforeEach(() => { |
| 44 | + jest.resetAllMocks(); |
| 45 | + }); |
| 46 | + |
| 47 | + describe('monitored', () => { |
| 48 | + describe('validate result', () => { |
| 49 | + test('sync function', async () => { |
| 50 | + const mockReturn = 10; |
| 51 | + const mockFunc = jest.fn().mockReturnValue(mockReturn); |
| 52 | + const monitor = new Monitor({...defaultMonitorOptions}); |
| 53 | + |
| 54 | + const res = monitor.monitored('test', mockFunc); |
| 55 | + |
| 56 | + expect(mockFunc).toBeCalledTimes(1); |
| 57 | + expect(res).toEqual(mockReturn); |
| 58 | + }); |
| 59 | + |
| 60 | + test('async function', async () => { |
| 61 | + const mockReturn = 10; |
| 62 | + const mockFunc = jest.fn().mockResolvedValue(mockReturn); |
| 63 | + const monitor = new Monitor({...defaultMonitorOptions}); |
| 64 | + |
| 65 | + const res = await monitor.monitored('test', mockFunc); |
| 66 | + |
| 67 | + expect(mockFunc).toBeCalledTimes(1); |
| 68 | + expect(res).toEqual(mockReturn); |
| 69 | + }); |
| 70 | + |
| 71 | + test('sync function throws', async () => { |
| 72 | + const mockError = new Error('error'); |
| 73 | + const mockFunc = jest.fn(() => { |
| 74 | + throw mockError; |
| 75 | + }); |
| 76 | + const monitor = new Monitor({...defaultMonitorOptions}); |
| 77 | + |
| 78 | + try { |
| 79 | + monitor.monitored('test', mockFunc); |
| 80 | + |
| 81 | + fail('should throw error'); |
| 82 | + } catch (err) { |
| 83 | + expect(err).toEqual(mockError); |
| 84 | + } |
| 85 | + |
| 86 | + expect(mockFunc).toBeCalledTimes(1); |
| 87 | + }); |
| 88 | + |
| 89 | + test('async function throws', async () => { |
| 90 | + const mockError = new Error('error'); |
| 91 | + const mockFunc = jest.fn().mockRejectedValue(mockError); |
| 92 | + const monitor = new Monitor({...defaultMonitorOptions}); |
| 93 | + |
| 94 | + try { |
| 95 | + await monitor.monitored('test', mockFunc); |
| 96 | + |
| 97 | + fail('should throw error'); |
| 98 | + } catch (err) { |
| 99 | + expect(err).toEqual(mockError); |
| 100 | + } |
| 101 | + |
| 102 | + expect(mockFunc).toBeCalledTimes(1); |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + describe('shouldMonitorExecutionStart', () => { |
| 107 | + test('true', async () => { |
| 108 | + const mockReturn = 10; |
| 109 | + const mockFunc = jest.fn().mockReturnValue(mockReturn); |
| 110 | + const monitor = new Monitor({...defaultMonitorOptions, shouldMonitorExecutionStart: true}); |
| 111 | + |
| 112 | + const res = monitor.monitored('test', mockFunc); |
| 113 | + |
| 114 | + expect(mockFunc).toBeCalledTimes(1); |
| 115 | + expect(res).toEqual(mockReturn); |
| 116 | + assertIncrementWasCalled(monitor, 'test.start'); |
| 117 | + expect(mocked(consoleLogger.debug)).toHaveBeenCalledWith('test.start', {extra: undefined}); |
| 118 | + }); |
| 119 | + |
| 120 | + test('false', async () => { |
| 121 | + const mockReturn = 10; |
| 122 | + const mockFunc = jest.fn().mockReturnValue(mockReturn); |
| 123 | + const monitor = new Monitor({...defaultMonitorOptions, shouldMonitorExecutionStart: false}); |
| 124 | + |
| 125 | + const res = monitor.monitored('test', mockFunc); |
| 126 | + |
| 127 | + expect(mockFunc).toBeCalledTimes(1); |
| 128 | + expect(res).toEqual(mockReturn); |
| 129 | + assertIncrementWasNotCalled(monitor, 'test.start'); |
| 130 | + expect(mocked(consoleLogger.debug)).not.toHaveBeenCalledWith('test.start', expect.anything()); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + describe('result', () => { |
| 135 | + const mockReturn = 10; |
| 136 | + |
| 137 | + [false, true].forEach(isAsync => { |
| 138 | + const mockFunc: any = isAsync |
| 139 | + ? () => |
| 140 | + new Promise<number>(resolve => { |
| 141 | + resolve(mockReturn); |
| 142 | + }) |
| 143 | + : () => mockReturn; |
| 144 | + |
| 145 | + describe(`${isAsync ? 'async' : 'sync'} function`, () => { |
| 146 | + test('default options', async () => { |
| 147 | + const monitor = new Monitor({...defaultMonitorOptions}); |
| 148 | + |
| 149 | + const res = monitor.monitored('test', mockFunc); |
| 150 | + |
| 151 | + if (isAsync) { |
| 152 | + await res; |
| 153 | + } |
| 154 | + |
| 155 | + assertIncrementWasCalled(monitor, 'test.success'); |
| 156 | + assertGaugeWasCalled(monitor, 'test.ExecutionTime'); |
| 157 | + assertTimingWasCalled(monitor, 'test.ExecutionTime'); |
| 158 | + |
| 159 | + expect(mocked(consoleLogger.debug)).toHaveBeenCalledWith('test.success', { |
| 160 | + extra: { |
| 161 | + executionTime: expect.any(Number), |
| 162 | + executionResult: 'NOT_LOGGED', |
| 163 | + }, |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + test('send context', async () => { |
| 168 | + const context = {a: 'a', b: 'bbbbb', c: true}; |
| 169 | + const monitor = new Monitor({...defaultMonitorOptions}); |
| 170 | + |
| 171 | + const res = monitor.monitored('test', mockFunc, {context}); |
| 172 | + |
| 173 | + if (isAsync) { |
| 174 | + await res; |
| 175 | + } |
| 176 | + |
| 177 | + expect(mocked(consoleLogger.debug)).toHaveBeenCalledWith('test.success', { |
| 178 | + extra: { |
| 179 | + ...context, |
| 180 | + executionTime: expect.any(Number), |
| 181 | + executionResult: 'NOT_LOGGED', |
| 182 | + }, |
| 183 | + }); |
| 184 | + }); |
| 185 | + |
| 186 | + test('logResult: true', async () => { |
| 187 | + const monitor = new Monitor({...defaultMonitorOptions, shouldMonitorExecutionStart: false}); |
| 188 | + |
| 189 | + const res = monitor.monitored('test', mockFunc, {logResult: true}); |
| 190 | + |
| 191 | + if (isAsync) { |
| 192 | + await res; |
| 193 | + } |
| 194 | + |
| 195 | + expect(mocked(consoleLogger.debug)).toHaveBeenCalledWith('test.success', { |
| 196 | + extra: { |
| 197 | + executionTime: expect.any(Number), |
| 198 | + executionResult: mockReturn, |
| 199 | + }, |
| 200 | + }); |
| 201 | + }); |
| 202 | + |
| 203 | + test('logResult: true with parseResult', async () => { |
| 204 | + const monitor = new Monitor({...defaultMonitorOptions}); |
| 205 | + const mockParsedReturn = 50; |
| 206 | + const parseResult = jest.fn().mockReturnValue(mockParsedReturn); |
| 207 | + |
| 208 | + let res = monitor.monitored('test', mockFunc, {logResult: true, parseResult}); |
| 209 | + |
| 210 | + if (isAsync) { |
| 211 | + res = await res; |
| 212 | + } |
| 213 | + |
| 214 | + expect(res).toEqual(mockReturn); |
| 215 | + |
| 216 | + expect(mocked(consoleLogger.debug)).toHaveBeenCalledWith('test.success', { |
| 217 | + extra: { |
| 218 | + executionTime: expect.any(Number), |
| 219 | + executionResult: mockParsedReturn, |
| 220 | + }, |
| 221 | + }); |
| 222 | + }); |
| 223 | + }); |
| 224 | + }); |
| 225 | + }); |
| 226 | + |
| 227 | + describe('error', () => { |
| 228 | + const mockError = new Error('error'); |
| 229 | + |
| 230 | + [false, true].forEach(isAsync => { |
| 231 | + const mockFunc: any = isAsync |
| 232 | + ? () => |
| 233 | + new Promise<number>((_, reject) => { |
| 234 | + reject(mockError); |
| 235 | + }) |
| 236 | + : () => { throw mockError }; |
| 237 | + |
| 238 | + describe(`${isAsync ? 'async' : 'sync'} function`, () => { |
| 239 | + test('default options', async () => { |
| 240 | + const monitor = new Monitor({...defaultMonitorOptions}); |
| 241 | + |
| 242 | + try { |
| 243 | + const res = monitor.monitored('test', mockFunc); |
| 244 | + |
| 245 | + if (isAsync) { |
| 246 | + await res; |
| 247 | + } |
| 248 | + |
| 249 | + fail('Should not success'); |
| 250 | + } catch (err) { |
| 251 | + expect(err).toEqual(mockError); |
| 252 | + |
| 253 | + assertIncrementWasCalled(monitor, 'test.error'); |
| 254 | + |
| 255 | + expect(mocked(consoleLogger.error)).toHaveBeenCalledWith('test.error', { |
| 256 | + err: mockError, |
| 257 | + extra: undefined |
| 258 | + }); |
| 259 | + } |
| 260 | + }); |
| 261 | + |
| 262 | + test('send context', async () => { |
| 263 | + const context = {a: 'a', b: 'bbbbb', c: true}; |
| 264 | + const monitor = new Monitor({...defaultMonitorOptions}); |
| 265 | + |
| 266 | + try { |
| 267 | + const res = monitor.monitored('test', mockFunc, {context}); |
| 268 | + |
| 269 | + if (isAsync) { |
| 270 | + await res; |
| 271 | + } |
| 272 | + |
| 273 | + fail('Should not success'); |
| 274 | + } catch (err) { |
| 275 | + expect(err).toEqual(mockError); |
| 276 | + |
| 277 | + expect(mocked(consoleLogger.error)).toHaveBeenCalledWith('test.error', { |
| 278 | + err: mockError, |
| 279 | + extra: context |
| 280 | + }); |
| 281 | + } |
| 282 | + }); |
| 283 | + |
| 284 | + test('with parseError', async () => { |
| 285 | + const monitor = new Monitor({...defaultMonitorOptions}); |
| 286 | + const mockParsedError = 'blabla'; |
| 287 | + |
| 288 | + try { |
| 289 | + const res = monitor.monitored('test', mockFunc, {parseError: () => mockParsedError}); |
| 290 | + |
| 291 | + if (isAsync) { |
| 292 | + await res; |
| 293 | + } |
| 294 | + |
| 295 | + fail('Should not success'); |
| 296 | + } catch (err) { |
| 297 | + expect(err).toEqual(mockError); |
| 298 | + |
| 299 | + expect(mocked(consoleLogger.error)).toHaveBeenCalledWith('test.error', { |
| 300 | + err: mockParsedError, |
| 301 | + extra: undefined |
| 302 | + }); |
| 303 | + } |
| 304 | + }); |
| 305 | + }); |
| 306 | + }); |
| 307 | + }); |
| 308 | + }); |
| 309 | +}); |
0 commit comments