|
| 1 | +import { captureMessage } from '@sentry/nextjs'; |
| 2 | +import { sentryLogger } from './utils.server'; |
| 3 | + |
| 4 | +jest.mock('@sentry/nextjs', () => ({ |
| 5 | + captureMessage: jest.fn(), |
| 6 | +})); |
| 7 | + |
| 8 | +jest.mock('@/lib/config.server', () => ({ |
| 9 | + IS_IN_AUTOMATED_TEST: false, |
| 10 | +})); |
| 11 | + |
| 12 | +const mockCaptureMessage = jest.mocked(captureMessage); |
| 13 | + |
| 14 | +describe('sentryLogger', () => { |
| 15 | + const debugSpy = jest.spyOn(console, 'debug').mockImplementation(() => {}); |
| 16 | + const logSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); |
| 17 | + const infoSpy = jest.spyOn(console, 'info').mockImplementation(() => {}); |
| 18 | + const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); |
| 19 | + const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + jest.clearAllMocks(); |
| 23 | + }); |
| 24 | + |
| 25 | + afterAll(() => { |
| 26 | + debugSpy.mockRestore(); |
| 27 | + logSpy.mockRestore(); |
| 28 | + infoSpy.mockRestore(); |
| 29 | + warnSpy.mockRestore(); |
| 30 | + errorSpy.mockRestore(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('logs debug messages without creating a Sentry event', () => { |
| 34 | + sentryLogger('test-source', 'debug')('debug message', { foo: 'bar' }); |
| 35 | + |
| 36 | + expect(debugSpy).toHaveBeenCalledWith('debug message', { foo: 'bar' }); |
| 37 | + expect(mockCaptureMessage).not.toHaveBeenCalled(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('logs info messages without creating a Sentry event', () => { |
| 41 | + sentryLogger('test-source', 'info')('informational message', { foo: 'bar' }); |
| 42 | + |
| 43 | + expect(infoSpy).toHaveBeenCalledWith('informational message', { foo: 'bar' }); |
| 44 | + expect(mockCaptureMessage).not.toHaveBeenCalled(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('logs default messages without creating a Sentry event', () => { |
| 48 | + sentryLogger('test-source')('default message', { foo: 'bar' }); |
| 49 | + |
| 50 | + expect(logSpy).toHaveBeenCalledWith('default message', { foo: 'bar' }); |
| 51 | + expect(mockCaptureMessage).not.toHaveBeenCalled(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('captures warning messages in Sentry', () => { |
| 55 | + sentryLogger('test-source', 'warning')('warning message', { foo: 'bar' }); |
| 56 | + |
| 57 | + expect(warnSpy).toHaveBeenCalledWith('warning message', { foo: 'bar' }); |
| 58 | + expect(mockCaptureMessage).toHaveBeenCalledWith('warning message', { |
| 59 | + level: 'warning', |
| 60 | + tags: { source: 'test-source' }, |
| 61 | + extra: { args: [{ foo: 'bar' }] }, |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('captures error messages in Sentry', () => { |
| 66 | + sentryLogger('test-source', 'error')('error message', { foo: 'bar' }); |
| 67 | + |
| 68 | + expect(errorSpy).toHaveBeenCalledWith('error message', { foo: 'bar' }); |
| 69 | + expect(mockCaptureMessage).toHaveBeenCalledWith('error message', { |
| 70 | + level: 'error', |
| 71 | + tags: { source: 'test-source' }, |
| 72 | + extra: { args: [{ foo: 'bar' }] }, |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('routes fatal messages to console.error and captures them in Sentry', () => { |
| 77 | + sentryLogger('test-source', 'fatal')('fatal message'); |
| 78 | + |
| 79 | + expect(errorSpy).toHaveBeenCalledWith('fatal message'); |
| 80 | + expect(mockCaptureMessage).toHaveBeenCalledWith('fatal message', { |
| 81 | + level: 'fatal', |
| 82 | + tags: { source: 'test-source' }, |
| 83 | + extra: undefined, |
| 84 | + }); |
| 85 | + }); |
| 86 | +}); |
0 commit comments