-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathutils.test.ts
151 lines (128 loc) · 4.62 KB
/
utils.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { captureException, getClient } from '@sentry/core';
import { type Mock, afterEach, beforeEach, describe, expect, it, test, vi } from 'vitest';
import type { ComponentPublicInstance } from 'vue';
import { extractErrorContext, reportNuxtError } from '../../src/runtime/utils';
describe('extractErrorContext', () => {
it('returns empty object for undefined or empty context', () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
expect(extractErrorContext(undefined)).toEqual({});
expect(extractErrorContext({})).toEqual({});
});
it('extracts properties from errorContext and drops them if missing', () => {
const context = {
event: {
_method: 'GET',
_path: '/test',
},
tags: ['tag1', 'tag2'],
};
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
expect(extractErrorContext(context)).toEqual({
method: 'GET',
path: '/test',
tags: ['tag1', 'tag2'],
});
const partialContext = {
event: {
_path: '/test',
},
};
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
expect(extractErrorContext(partialContext)).toEqual({ path: '/test' });
});
it('handles errorContext.tags correctly, including when absent or of unexpected type', () => {
const contextWithTags = {
tags: ['tag1', 'tag2'],
};
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
expect(extractErrorContext(contextWithTags)).toEqual({
tags: ['tag1', 'tag2'],
});
const contextWithoutTags = {
event: {},
};
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
expect(extractErrorContext(contextWithoutTags)).toEqual({});
const contextWithInvalidTags = {
event: {},
tags: 'not-an-array',
};
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
expect(extractErrorContext(contextWithInvalidTags)).toEqual({});
});
it('gracefully handles unexpected context structure without throwing errors', () => {
const weirdContext1 = {
unexpected: 'value',
};
const weirdContext2 = ['value'];
const weirdContext3 = 123;
expect(() => extractErrorContext(weirdContext1)).not.toThrow();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
expect(() => extractErrorContext(weirdContext2)).not.toThrow();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
expect(() => extractErrorContext(weirdContext3)).not.toThrow();
});
});
describe('reportNuxtError', () => {
vi.mock('@sentry/core', () => ({
captureException: vi.fn(),
getClient: vi.fn(),
}));
const mockError = new Error('Test error');
const mockInstance: ComponentPublicInstance = {
$props: { foo: 'bar' },
} as any;
const mockClient = {
getOptions: vi.fn().mockReturnValue({ attachProps: true }),
};
beforeEach(() => {
// Using fake timers as setTimeout is used in `reportNuxtError`
vi.useFakeTimers();
vi.clearAllMocks();
(getClient as Mock).mockReturnValue(mockClient);
});
afterEach(() => {
vi.clearAllMocks();
});
test('captures exception with correct error and metadata', () => {
reportNuxtError({ error: mockError });
vi.runAllTimers();
expect(captureException).toHaveBeenCalledWith(mockError, {
captureContext: { contexts: { nuxt: { info: undefined } } },
mechanism: { handled: false },
});
});
test('includes instance props if attachProps is not explicitly defined', () => {
reportNuxtError({ error: mockError, instance: mockInstance });
vi.runAllTimers();
expect(captureException).toHaveBeenCalledWith(mockError, {
captureContext: { contexts: { nuxt: { info: undefined, propsData: { foo: 'bar' } } } },
mechanism: { handled: false },
});
});
test('does not include instance props if attachProps is disabled', () => {
mockClient.getOptions.mockReturnValue({ attachProps: false });
reportNuxtError({ error: mockError, instance: mockInstance });
vi.runAllTimers();
expect(captureException).toHaveBeenCalledWith(mockError, {
captureContext: { contexts: { nuxt: { info: undefined } } },
mechanism: { handled: false },
});
});
test('handles absence of instance correctly', () => {
reportNuxtError({ error: mockError, info: 'Some info' });
vi.runAllTimers();
expect(captureException).toHaveBeenCalledWith(mockError, {
captureContext: { contexts: { nuxt: { info: 'Some info' } } },
mechanism: { handled: false },
});
});
});