|
1 | | -import { addExperimentalFeatures, ExperimentalFeature, HookNames } from '@datadog/browser-core' |
| 1 | +import { HookNames } from '@datadog/browser-core' |
2 | 2 | import type { RelativeTime } from '@datadog/browser-core' |
3 | 3 | import type { AssembleHookParams, Hooks } from '../hooks' |
4 | 4 | import { createHooks } from '../hooks' |
@@ -36,177 +36,150 @@ describe('sourceCodeContext', () => { |
36 | 36 | }) |
37 | 37 | } |
38 | 38 |
|
39 | | - describe('assemble hook when FF disabled', () => { |
40 | | - it('should not add source code context', () => { |
41 | | - setupBrowserWindowWithContext() |
42 | | - startSourceCodeContext(hooks) |
43 | | - |
44 | | - const result = hooks.triggerHook(HookNames.Assemble, { |
45 | | - eventType: 'error', |
46 | | - startTime: 0 as RelativeTime, |
47 | | - domainContext: {}, |
48 | | - rawRumEvent: { |
49 | | - type: 'error', |
50 | | - error: { |
51 | | - stack: MATCHING_TEST_STACK, |
52 | | - }, |
| 39 | + it('should add source code context matching the error stack first frame URL', () => { |
| 40 | + setupBrowserWindowWithContext() |
| 41 | + startSourceCodeContext(hooks) |
| 42 | + |
| 43 | + const result = hooks.triggerHook(HookNames.Assemble, { |
| 44 | + eventType: 'error', |
| 45 | + startTime: 0 as RelativeTime, |
| 46 | + domainContext: {}, |
| 47 | + rawRumEvent: { |
| 48 | + type: 'error', |
| 49 | + error: { |
| 50 | + stack: MATCHING_TEST_STACK, |
53 | 51 | }, |
54 | | - } as AssembleHookParams) |
| 52 | + }, |
| 53 | + } as AssembleHookParams) |
55 | 54 |
|
56 | | - expect(result).toBeUndefined() |
| 55 | + expect(result).toEqual({ |
| 56 | + type: 'error', |
| 57 | + service: 'my-service', |
| 58 | + version: '1.0.0', |
57 | 59 | }) |
58 | 60 | }) |
59 | 61 |
|
60 | | - describe('assemble hook when FF enabled', () => { |
61 | | - beforeEach(() => { |
62 | | - addExperimentalFeatures([ExperimentalFeature.SOURCE_CODE_CONTEXT]) |
63 | | - }) |
| 62 | + it('should add source code context matching the handling_stack first frame URL', () => { |
| 63 | + setupBrowserWindowWithContext() |
| 64 | + startSourceCodeContext(hooks) |
64 | 65 |
|
65 | | - it('should add source code context matching the error stack first frame URL', () => { |
66 | | - setupBrowserWindowWithContext() |
67 | | - startSourceCodeContext(hooks) |
68 | | - |
69 | | - const result = hooks.triggerHook(HookNames.Assemble, { |
70 | | - eventType: 'error', |
71 | | - startTime: 0 as RelativeTime, |
72 | | - domainContext: {}, |
73 | | - rawRumEvent: { |
74 | | - type: 'error', |
75 | | - error: { |
76 | | - stack: MATCHING_TEST_STACK, |
77 | | - }, |
78 | | - }, |
79 | | - } as AssembleHookParams) |
| 66 | + const result = hooks.triggerHook(HookNames.Assemble, { |
| 67 | + eventType: 'action', |
| 68 | + startTime: 0 as RelativeTime, |
| 69 | + rawRumEvent: { |
| 70 | + type: 'action', |
| 71 | + }, |
| 72 | + domainContext: { |
| 73 | + handlingStack: MATCHING_TEST_STACK, |
| 74 | + }, |
| 75 | + } as AssembleHookParams) |
80 | 76 |
|
81 | | - expect(result).toEqual({ |
82 | | - type: 'error', |
83 | | - service: 'my-service', |
84 | | - version: '1.0.0', |
85 | | - }) |
| 77 | + expect(result).toEqual({ |
| 78 | + type: 'action', |
| 79 | + service: 'my-service', |
| 80 | + version: '1.0.0', |
86 | 81 | }) |
| 82 | + }) |
87 | 83 |
|
88 | | - it('should add source code context matching the handling_stack first frame URL', () => { |
89 | | - setupBrowserWindowWithContext() |
90 | | - startSourceCodeContext(hooks) |
| 84 | + it('should add source code context matching the LoAF first script source URL', () => { |
| 85 | + setupBrowserWindowWithContext() |
| 86 | + startSourceCodeContext(hooks) |
91 | 87 |
|
92 | | - const result = hooks.triggerHook(HookNames.Assemble, { |
93 | | - eventType: 'action', |
94 | | - startTime: 0 as RelativeTime, |
95 | | - rawRumEvent: { |
96 | | - type: 'action', |
97 | | - }, |
98 | | - domainContext: { |
99 | | - handlingStack: MATCHING_TEST_STACK, |
| 88 | + const result = hooks.triggerHook(HookNames.Assemble, { |
| 89 | + eventType: 'long_task', |
| 90 | + startTime: 0 as RelativeTime, |
| 91 | + domainContext: {}, |
| 92 | + rawRumEvent: { |
| 93 | + type: 'long_task', |
| 94 | + long_task: { |
| 95 | + entry_type: 'long-animation-frame', |
| 96 | + scripts: [ |
| 97 | + { |
| 98 | + source_url: 'http://localhost:8080/file.js', |
| 99 | + }, |
| 100 | + ], |
100 | 101 | }, |
101 | | - } as AssembleHookParams) |
| 102 | + } as RawRumLongAnimationFrameEvent, |
| 103 | + } as AssembleHookParams) |
102 | 104 |
|
103 | | - expect(result).toEqual({ |
104 | | - type: 'action', |
105 | | - service: 'my-service', |
106 | | - version: '1.0.0', |
107 | | - }) |
| 105 | + expect(result).toEqual({ |
| 106 | + type: 'long_task', |
| 107 | + service: 'my-service', |
| 108 | + version: '1.0.0', |
108 | 109 | }) |
| 110 | + }) |
109 | 111 |
|
110 | | - it('should add source code context matching the LoAF first script source URL', () => { |
111 | | - setupBrowserWindowWithContext() |
112 | | - startSourceCodeContext(hooks) |
113 | | - |
114 | | - const result = hooks.triggerHook(HookNames.Assemble, { |
115 | | - eventType: 'long_task', |
116 | | - startTime: 0 as RelativeTime, |
117 | | - domainContext: {}, |
118 | | - rawRumEvent: { |
119 | | - type: 'long_task', |
120 | | - long_task: { |
121 | | - entry_type: 'long-animation-frame', |
122 | | - scripts: [ |
123 | | - { |
124 | | - source_url: 'http://localhost:8080/file.js', |
125 | | - }, |
126 | | - ], |
127 | | - }, |
128 | | - } as RawRumLongAnimationFrameEvent, |
129 | | - } as AssembleHookParams) |
130 | | - |
131 | | - expect(result).toEqual({ |
132 | | - type: 'long_task', |
133 | | - service: 'my-service', |
134 | | - version: '1.0.0', |
135 | | - }) |
136 | | - }) |
| 112 | + it('should not add source code context matching no stack', () => { |
| 113 | + setupBrowserWindowWithContext() |
| 114 | + startSourceCodeContext(hooks) |
137 | 115 |
|
138 | | - it('should not add source code context matching no stack', () => { |
139 | | - setupBrowserWindowWithContext() |
140 | | - startSourceCodeContext(hooks) |
141 | | - |
142 | | - const result = hooks.triggerHook(HookNames.Assemble, { |
143 | | - eventType: 'error', |
144 | | - startTime: 0 as RelativeTime, |
145 | | - domainContext: {}, |
146 | | - rawRumEvent: { |
147 | | - type: 'error', |
148 | | - error: { |
149 | | - stack: `Error: Another error |
| 116 | + const result = hooks.triggerHook(HookNames.Assemble, { |
| 117 | + eventType: 'error', |
| 118 | + startTime: 0 as RelativeTime, |
| 119 | + domainContext: {}, |
| 120 | + rawRumEvent: { |
| 121 | + type: 'error', |
| 122 | + error: { |
| 123 | + stack: `Error: Another error |
150 | 124 | at anotherFunction (http://localhost:8080/another-file.js:41:27)`, |
151 | | - }, |
152 | 125 | }, |
153 | | - } as AssembleHookParams) |
| 126 | + }, |
| 127 | + } as AssembleHookParams) |
154 | 128 |
|
155 | | - expect(result).toBeUndefined() |
156 | | - }) |
| 129 | + expect(result).toBeUndefined() |
| 130 | + }) |
157 | 131 |
|
158 | | - it('should support late updates to DD_SOURCE_CODE_CONTEXT', () => { |
159 | | - startSourceCodeContext(hooks) |
160 | | - |
161 | | - // Add context AFTER initialization |
162 | | - setupBrowserWindowWithContext() |
163 | | - |
164 | | - const result = hooks.triggerHook(HookNames.Assemble, { |
165 | | - eventType: 'error', |
166 | | - startTime: 0 as RelativeTime, |
167 | | - domainContext: {}, |
168 | | - rawRumEvent: { |
169 | | - type: 'error', |
170 | | - error: { |
171 | | - stack: TEST_STACK, |
172 | | - }, |
173 | | - }, |
174 | | - } as AssembleHookParams) |
| 132 | + it('should support late updates to DD_SOURCE_CODE_CONTEXT', () => { |
| 133 | + startSourceCodeContext(hooks) |
175 | 134 |
|
176 | | - expect(result).toEqual({ |
| 135 | + // Add context AFTER initialization |
| 136 | + setupBrowserWindowWithContext() |
| 137 | + |
| 138 | + const result = hooks.triggerHook(HookNames.Assemble, { |
| 139 | + eventType: 'error', |
| 140 | + startTime: 0 as RelativeTime, |
| 141 | + domainContext: {}, |
| 142 | + rawRumEvent: { |
177 | 143 | type: 'error', |
178 | | - service: 'my-service', |
179 | | - version: '1.0.0', |
180 | | - }) |
| 144 | + error: { |
| 145 | + stack: TEST_STACK, |
| 146 | + }, |
| 147 | + }, |
| 148 | + } as AssembleHookParams) |
| 149 | + |
| 150 | + expect(result).toEqual({ |
| 151 | + type: 'error', |
| 152 | + service: 'my-service', |
| 153 | + version: '1.0.0', |
181 | 154 | }) |
| 155 | + }) |
182 | 156 |
|
183 | | - it('should ignore updates to existing source code context after initialization', () => { |
184 | | - setupBrowserWindowWithContext() |
185 | | - startSourceCodeContext(hooks) |
186 | | - |
187 | | - // Update existing entry |
188 | | - browserWindow.DD_SOURCE_CODE_CONTEXT![TEST_STACK] = { |
189 | | - service: 'updated-service', |
190 | | - version: '1.1.0', |
191 | | - } |
192 | | - |
193 | | - const result = hooks.triggerHook(HookNames.Assemble, { |
194 | | - eventType: 'error', |
195 | | - startTime: 0 as RelativeTime, |
196 | | - domainContext: {}, |
197 | | - rawRumEvent: { |
198 | | - type: 'error', |
199 | | - error: { |
200 | | - stack: TEST_STACK, |
201 | | - }, |
202 | | - }, |
203 | | - } as AssembleHookParams) |
| 157 | + it('should ignore updates to existing source code context after initialization', () => { |
| 158 | + setupBrowserWindowWithContext() |
| 159 | + startSourceCodeContext(hooks) |
| 160 | + |
| 161 | + // Update existing entry |
| 162 | + browserWindow.DD_SOURCE_CODE_CONTEXT![TEST_STACK] = { |
| 163 | + service: 'updated-service', |
| 164 | + version: '1.1.0', |
| 165 | + } |
204 | 166 |
|
205 | | - expect(result).toEqual({ |
| 167 | + const result = hooks.triggerHook(HookNames.Assemble, { |
| 168 | + eventType: 'error', |
| 169 | + startTime: 0 as RelativeTime, |
| 170 | + domainContext: {}, |
| 171 | + rawRumEvent: { |
206 | 172 | type: 'error', |
207 | | - service: 'my-service', |
208 | | - version: '1.0.0', |
209 | | - }) |
| 173 | + error: { |
| 174 | + stack: TEST_STACK, |
| 175 | + }, |
| 176 | + }, |
| 177 | + } as AssembleHookParams) |
| 178 | + |
| 179 | + expect(result).toEqual({ |
| 180 | + type: 'error', |
| 181 | + service: 'my-service', |
| 182 | + version: '1.0.0', |
210 | 183 | }) |
211 | 184 | }) |
212 | 185 | }) |
0 commit comments