|
1 | | -import { describe, expect, it, vi } from 'vitest'; |
2 | | -import { addConsoleInstrumentationHandler } from '../../../src/instrument/console'; |
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { |
| 3 | + _INTERNAL_resetConsoleInstrumentationOptions, |
| 4 | + addConsoleInstrumentationFilter, |
| 5 | + addConsoleInstrumentationHandler, |
| 6 | +} from '../../../src/instrument/console'; |
3 | 7 | import { GLOBAL_OBJ } from '../../../src/utils/worldwide'; |
| 8 | +import { debug, originalConsoleMethods } from '../../../src/utils/debug-logger'; |
| 9 | +import { resetInstrumentationHandlers } from '../../../src/instrument/handlers'; |
4 | 10 |
|
5 | 11 | describe('addConsoleInstrumentationHandler', () => { |
| 12 | + let _originalConsoleMethods: typeof originalConsoleMethods = {}; |
| 13 | + |
| 14 | + afterEach(() => { |
| 15 | + Object.assign(originalConsoleMethods, _originalConsoleMethods); |
| 16 | + resetInstrumentationHandlers(); |
| 17 | + vi.restoreAllMocks(); |
| 18 | + }); |
| 19 | + |
| 20 | + // This cannot be done in beforeEach, as the first invocation of `addConsoleInstrumentationHandler` will overwrite the original console methods. |
| 21 | + // Due to `fill` being called |
| 22 | + // So instead, we need to call this each time after calling `addConsoleInstrumentationHandler` |
| 23 | + function mockConsoleMethods() { |
| 24 | + // Re-store this with the current implementation |
| 25 | + Object.assign(_originalConsoleMethods, originalConsoleMethods); |
| 26 | + |
| 27 | + // Overwrite with mock console methods |
| 28 | + Object.assign(originalConsoleMethods, { |
| 29 | + log: vi.fn(), |
| 30 | + warn: vi.fn(), |
| 31 | + error: vi.fn(), |
| 32 | + debug: vi.fn(), |
| 33 | + info: vi.fn(), |
| 34 | + }); |
| 35 | + } |
| 36 | + |
6 | 37 | it.each(['log', 'warn', 'error', 'debug', 'info'] as const)( |
7 | 38 | 'calls registered handler when console.%s is called', |
8 | 39 | level => { |
9 | 40 | const handler = vi.fn(); |
10 | 41 | addConsoleInstrumentationHandler(handler); |
| 42 | + mockConsoleMethods(); |
11 | 43 |
|
12 | 44 | GLOBAL_OBJ.console[level]('test message'); |
13 | 45 |
|
14 | 46 | expect(handler).toHaveBeenCalledWith(expect.objectContaining({ args: ['test message'], level })); |
| 47 | + expect(originalConsoleMethods[level]).toHaveBeenCalledWith('test message'); |
15 | 48 | }, |
16 | 49 | ); |
17 | 50 |
|
18 | 51 | it('calls through to the underlying console method without throwing', () => { |
19 | 52 | addConsoleInstrumentationHandler(vi.fn()); |
| 53 | + mockConsoleMethods(); |
20 | 54 | expect(() => GLOBAL_OBJ.console.log('hello')).not.toThrow(); |
21 | 55 | }); |
| 56 | + |
| 57 | + describe('filter', () => { |
| 58 | + afterEach(() => { |
| 59 | + _INTERNAL_resetConsoleInstrumentationOptions(); |
| 60 | + }); |
| 61 | + |
| 62 | + describe('when debug is disabled', () => { |
| 63 | + beforeEach(() => { |
| 64 | + vi.spyOn(debug, 'isEnabled').mockImplementation(() => false); |
| 65 | + }); |
| 66 | + |
| 67 | + it('filters out messages that match the filter', () => { |
| 68 | + const handler = vi.fn(); |
| 69 | + addConsoleInstrumentationHandler(handler); |
| 70 | + addConsoleInstrumentationFilter(['test message']); |
| 71 | + mockConsoleMethods(); |
| 72 | + |
| 73 | + GLOBAL_OBJ.console.log('test message'); |
| 74 | + |
| 75 | + expect(originalConsoleMethods.log).not.toHaveBeenCalledWith('test message'); |
| 76 | + expect(handler).not.toHaveBeenCalled(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('does not filter out messages that do not match the filter', () => { |
| 80 | + const handler = vi.fn(); |
| 81 | + addConsoleInstrumentationHandler(handler); |
| 82 | + addConsoleInstrumentationFilter(['test message']); |
| 83 | + mockConsoleMethods(); |
| 84 | + |
| 85 | + GLOBAL_OBJ.console.log('other message'); |
| 86 | + |
| 87 | + expect(handler).toHaveBeenCalled(); |
| 88 | + expect(originalConsoleMethods.log).toHaveBeenCalledWith('other message'); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('when debug is enabled', () => { |
| 93 | + beforeEach(() => { |
| 94 | + vi.spyOn(debug, 'isEnabled').mockImplementation(() => true); |
| 95 | + }); |
| 96 | + |
| 97 | + it('logs filtered messages but does not call the handler for them', () => { |
| 98 | + const handler = vi.fn(); |
| 99 | + addConsoleInstrumentationHandler(handler); |
| 100 | + addConsoleInstrumentationFilter(['test message']); |
| 101 | + mockConsoleMethods(); |
| 102 | + |
| 103 | + GLOBAL_OBJ.console.log('test message'); |
| 104 | + |
| 105 | + expect(handler).not.toHaveBeenCalled(); |
| 106 | + expect(originalConsoleMethods.log).toHaveBeenCalledWith('test message'); |
| 107 | + }); |
| 108 | + }); |
| 109 | + }); |
22 | 110 | }); |
0 commit comments