|
1 |
| -import { describe, expect, it } from 'vitest'; |
2 |
| -import { extractErrorContext } from '../../src/runtime/utils'; |
| 1 | +import { captureException, getClient } from '@sentry/core'; |
| 2 | +import { type Mock, afterEach, beforeEach, describe, expect, it, test, vi } from 'vitest'; |
| 3 | +import type { ComponentPublicInstance } from 'vue'; |
| 4 | +import { extractErrorContext, reportNuxtError } from '../../src/runtime/utils'; |
3 | 5 |
|
4 | 6 | describe('extractErrorContext', () => {
|
5 | 7 | it('returns empty object for undefined or empty context', () => {
|
@@ -77,3 +79,73 @@ describe('extractErrorContext', () => {
|
77 | 79 | expect(() => extractErrorContext(weirdContext3)).not.toThrow();
|
78 | 80 | });
|
79 | 81 | });
|
| 82 | + |
| 83 | +describe('reportNuxtError', () => { |
| 84 | + vi.mock('@sentry/core', () => ({ |
| 85 | + captureException: vi.fn(), |
| 86 | + getClient: vi.fn(), |
| 87 | + })); |
| 88 | + |
| 89 | + const mockError = new Error('Test error'); |
| 90 | + |
| 91 | + const mockInstance: ComponentPublicInstance = { |
| 92 | + $props: { foo: 'bar' }, |
| 93 | + } as any; |
| 94 | + |
| 95 | + const mockClient = { |
| 96 | + getOptions: vi.fn().mockReturnValue({ attachProps: true }), |
| 97 | + }; |
| 98 | + |
| 99 | + beforeEach(() => { |
| 100 | + // Using fake timers as setTimeout is used in `reportNuxtError` |
| 101 | + vi.useFakeTimers(); |
| 102 | + vi.clearAllMocks(); |
| 103 | + (getClient as Mock).mockReturnValue(mockClient); |
| 104 | + }); |
| 105 | + |
| 106 | + afterEach(() => { |
| 107 | + vi.clearAllMocks(); |
| 108 | + }); |
| 109 | + |
| 110 | + test('captures exception with correct error and metadata', () => { |
| 111 | + reportNuxtError({ error: mockError }); |
| 112 | + vi.runAllTimers(); |
| 113 | + |
| 114 | + expect(captureException).toHaveBeenCalledWith(mockError, { |
| 115 | + captureContext: { contexts: { nuxt: { info: undefined } } }, |
| 116 | + mechanism: { handled: false }, |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + test('includes instance props if attachProps is not explicitly defined', () => { |
| 121 | + reportNuxtError({ error: mockError, instance: mockInstance }); |
| 122 | + vi.runAllTimers(); |
| 123 | + |
| 124 | + expect(captureException).toHaveBeenCalledWith(mockError, { |
| 125 | + captureContext: { contexts: { nuxt: { info: undefined, propsData: { foo: 'bar' } } } }, |
| 126 | + mechanism: { handled: false }, |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + test('does not include instance props if attachProps is disabled', () => { |
| 131 | + mockClient.getOptions.mockReturnValue({ attachProps: false }); |
| 132 | + |
| 133 | + reportNuxtError({ error: mockError, instance: mockInstance }); |
| 134 | + vi.runAllTimers(); |
| 135 | + |
| 136 | + expect(captureException).toHaveBeenCalledWith(mockError, { |
| 137 | + captureContext: { contexts: { nuxt: { info: undefined } } }, |
| 138 | + mechanism: { handled: false }, |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + test('handles absence of instance correctly', () => { |
| 143 | + reportNuxtError({ error: mockError, info: 'Some info' }); |
| 144 | + vi.runAllTimers(); |
| 145 | + |
| 146 | + expect(captureException).toHaveBeenCalledWith(mockError, { |
| 147 | + captureContext: { contexts: { nuxt: { info: 'Some info' } } }, |
| 148 | + mechanism: { handled: false }, |
| 149 | + }); |
| 150 | + }); |
| 151 | +}); |
0 commit comments