|
5 | 5 | import { afterEach, describe, expect, it, vi } from 'vitest'; |
6 | 6 |
|
7 | 7 | import { defaultStackParser } from '../src'; |
8 | | -import { eventFromUnknownInput } from '../src/eventbuilder'; |
| 8 | +import { eventFromUnknownInput, extractMessage, extractType } from '../src/eventbuilder'; |
9 | 9 |
|
10 | 10 | vi.mock('@sentry/core', async requireActual => { |
11 | 11 | return { |
@@ -169,3 +169,65 @@ describe('eventFromUnknownInput', () => { |
169 | 169 | }); |
170 | 170 | }); |
171 | 171 | }); |
| 172 | + |
| 173 | +describe('extractMessage', () => { |
| 174 | + it('should extract message from a standard Error object', () => { |
| 175 | + const error = new Error('Test error message'); |
| 176 | + const message = extractMessage(error); |
| 177 | + expect(message).toBe('Test error message'); |
| 178 | + }); |
| 179 | + |
| 180 | + it('should extract message from a WebAssembly.Exception object', () => { |
| 181 | + // https://developer.mozilla.org/en-US/docs/WebAssembly/JavaScript_interface/Exception/Exception#examples |
| 182 | + // @ts-expect-error - WebAssembly.Tag is a valid constructor |
| 183 | + const tag = new WebAssembly.Tag({ parameters: ['i32', 'f32'] }); |
| 184 | + // @ts-expect-error - WebAssembly.Exception is a valid constructor |
| 185 | + const wasmException = new WebAssembly.Exception(tag, [42, 42.3]); |
| 186 | + |
| 187 | + const message = extractMessage(wasmException); |
| 188 | + expect(message).toBe('wasm exception'); |
| 189 | + }); |
| 190 | + |
| 191 | + it('should extract nested error message', () => { |
| 192 | + const nestedError = { |
| 193 | + message: { |
| 194 | + error: new Error('Nested error message'), |
| 195 | + }, |
| 196 | + }; |
| 197 | + const message = extractMessage(nestedError as any); |
| 198 | + expect(message).toBe('Nested error message'); |
| 199 | + }); |
| 200 | + |
| 201 | + it('should return "No error message" if message is undefined', () => { |
| 202 | + const error = new Error(); |
| 203 | + error.message = undefined as any; |
| 204 | + const message = extractMessage(error); |
| 205 | + expect(message).toBe('No error message'); |
| 206 | + }); |
| 207 | +}); |
| 208 | + |
| 209 | +describe('extractName', () => { |
| 210 | + it('should extract name from a standard Error object', () => { |
| 211 | + const error = new Error('Test error message'); |
| 212 | + const name = extractType(error); |
| 213 | + expect(name).toBe('Error'); |
| 214 | + }); |
| 215 | + |
| 216 | + it('should extract name from a WebAssembly.Exception object', () => { |
| 217 | + // https://developer.mozilla.org/en-US/docs/WebAssembly/JavaScript_interface/Exception/Exception#examples |
| 218 | + // @ts-expect-error - WebAssembly.Tag is a valid constructor |
| 219 | + const tag = new WebAssembly.Tag({ parameters: ['i32', 'f32'] }); |
| 220 | + // @ts-expect-error - WebAssembly.Exception is a valid constructor |
| 221 | + const wasmException = new WebAssembly.Exception(tag, [42, 42.3]); |
| 222 | + |
| 223 | + const name = extractType(wasmException); |
| 224 | + expect(name).toBe('WebAssembly.Exception'); |
| 225 | + }); |
| 226 | + |
| 227 | + it('should return undefined if name is not present', () => { |
| 228 | + const error = new Error('Test error message'); |
| 229 | + error.name = undefined as any; |
| 230 | + const name = extractType(error); |
| 231 | + expect(name).toBeUndefined(); |
| 232 | + }); |
| 233 | +}); |
0 commit comments