|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { captureUnderscoreErrorException } from '../../../src/common/pages-router-instrumentation/_error'; |
| 3 | + |
| 4 | +const mockCaptureException = vi.fn(() => 'test-event-id'); |
| 5 | +const mockWithScope = vi.fn((callback: (scope: any) => any) => { |
| 6 | + const mockScope = { |
| 7 | + setSDKProcessingMetadata: vi.fn(), |
| 8 | + }; |
| 9 | + return callback(mockScope); |
| 10 | +}); |
| 11 | + |
| 12 | +vi.mock('@sentry/core', async () => { |
| 13 | + const actual = await vi.importActual('@sentry/core'); |
| 14 | + return { |
| 15 | + ...actual, |
| 16 | + captureException: (...args: unknown[]) => mockCaptureException(...args), |
| 17 | + withScope: (callback: (scope: any) => any) => mockWithScope(callback), |
| 18 | + httpRequestToRequestData: vi.fn(() => ({ url: 'http://test.com' })), |
| 19 | + }; |
| 20 | +}); |
| 21 | + |
| 22 | +vi.mock('../../../src/common/utils/responseEnd', () => ({ |
| 23 | + flushSafelyWithTimeout: vi.fn(() => Promise.resolve()), |
| 24 | + waitUntil: vi.fn(), |
| 25 | +})); |
| 26 | + |
| 27 | +describe('captureUnderscoreErrorException', () => { |
| 28 | + beforeEach(() => { |
| 29 | + vi.clearAllMocks(); |
| 30 | + }); |
| 31 | + |
| 32 | + afterEach(() => { |
| 33 | + vi.clearAllMocks(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should return the event ID when capturing an exception', async () => { |
| 37 | + const error = new Error('Test error'); |
| 38 | + const result = await captureUnderscoreErrorException({ |
| 39 | + err: error, |
| 40 | + pathname: '/test', |
| 41 | + res: { statusCode: 500 } as any, |
| 42 | + }); |
| 43 | + |
| 44 | + expect(result).toBe('test-event-id'); |
| 45 | + expect(mockCaptureException).toHaveBeenCalledWith(error, { |
| 46 | + mechanism: { |
| 47 | + type: 'auto.function.nextjs.underscore_error', |
| 48 | + handled: false, |
| 49 | + data: { |
| 50 | + function: '_error.getInitialProps', |
| 51 | + }, |
| 52 | + }, |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should return undefined for 4xx status codes', async () => { |
| 57 | + const result = await captureUnderscoreErrorException({ |
| 58 | + err: new Error('Not found'), |
| 59 | + pathname: '/test', |
| 60 | + res: { statusCode: 404 } as any, |
| 61 | + }); |
| 62 | + |
| 63 | + expect(result).toBeUndefined(); |
| 64 | + expect(mockCaptureException).not.toHaveBeenCalled(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should return undefined when pathname is not provided (render call)', async () => { |
| 68 | + const result = await captureUnderscoreErrorException({ |
| 69 | + err: new Error('Test error'), |
| 70 | + res: { statusCode: 500 } as any, |
| 71 | + }); |
| 72 | + |
| 73 | + expect(result).toBeUndefined(); |
| 74 | + expect(mockCaptureException).not.toHaveBeenCalled(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should capture falsy errors as messages', async () => { |
| 78 | + const result = await captureUnderscoreErrorException({ |
| 79 | + err: undefined, |
| 80 | + pathname: '/test', |
| 81 | + res: { statusCode: 500 } as any, |
| 82 | + }); |
| 83 | + |
| 84 | + expect(result).toBe('test-event-id'); |
| 85 | + expect(mockCaptureException).toHaveBeenCalledWith('_error.js called with falsy error (undefined)', { |
| 86 | + mechanism: { |
| 87 | + type: 'auto.function.nextjs.underscore_error', |
| 88 | + handled: false, |
| 89 | + data: { |
| 90 | + function: '_error.getInitialProps', |
| 91 | + }, |
| 92 | + }, |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should use statusCode from contextOrProps when res is not available', async () => { |
| 97 | + const result = await captureUnderscoreErrorException({ |
| 98 | + err: new Error('Test error'), |
| 99 | + pathname: '/test', |
| 100 | + statusCode: 500, |
| 101 | + }); |
| 102 | + |
| 103 | + expect(result).toBe('test-event-id'); |
| 104 | + expect(mockCaptureException).toHaveBeenCalled(); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should return undefined when statusCode from contextOrProps is 4xx', async () => { |
| 108 | + const result = await captureUnderscoreErrorException({ |
| 109 | + err: new Error('Bad request'), |
| 110 | + pathname: '/test', |
| 111 | + statusCode: 400, |
| 112 | + }); |
| 113 | + |
| 114 | + expect(result).toBeUndefined(); |
| 115 | + expect(mockCaptureException).not.toHaveBeenCalled(); |
| 116 | + }); |
| 117 | +}); |
0 commit comments