-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy patherror.test.ts
More file actions
105 lines (71 loc) · 3.68 KB
/
error.test.ts
File metadata and controls
105 lines (71 loc) · 3.68 KB
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
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { getErrorFromErrorEvent } from '../../src/utils/error';
vi.mock('@hawk.so/core', () => ({ log: vi.fn(), isLoggerSet: vi.fn(() => true), setLogger: vi.fn() }));
import Sanitizer from '../../src/modules/sanitizer';
describe('getErrorFromErrorEvent', () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe('ErrorEvent', () => {
it('should return the Error when event.error is an Error instance', () => {
const error = new Error('Test error');
const event = new ErrorEvent('error', { error });
const result = getErrorFromErrorEvent(event);
expect(result).toBe(error);
});
it('should return the DOMException when event.error is a DOMException', () => {
const error = new DOMException('Network error', 'NetworkError');
const event = new ErrorEvent('error', { error });
const result = getErrorFromErrorEvent(event);
expect(result).toBe('<instance of DOMException>');
});
it('should return the message when event.error is not provided and message is a string', () => {
const event = new ErrorEvent('error', { message: 'Script error.' });
const result = getErrorFromErrorEvent(event);
expect(result).toBe('Script error.');
});
it('should return empty string when event.error is not provided and message is empty', () => {
const event = new ErrorEvent('error', { message: '' });
const result = getErrorFromErrorEvent(event);
expect(result).toBe('');
});
});
describe('PromiseRejectionEvent', () => {
it('should return the Error when event.reason is an Error instance', () => {
const reason = new Error('Promise rejected');
const event = new PromiseRejectionEvent('unhandledrejection', { promise: Promise.resolve(), reason });
const result = getErrorFromErrorEvent(event);
expect(result).toBe(reason);
});
it('should return the string when event.reason is a string', () => {
const reason = 'Promise rejected with string';
const event = new PromiseRejectionEvent('unhandledrejection', { promise: Promise.resolve(), reason });
const result = getErrorFromErrorEvent(event);
expect(result).toBe(reason);
});
it('should return stringified object when event.reason is a plain object', () => {
const reason = { code: 'ERR_001', details: 'Something went wrong' };
const event = new PromiseRejectionEvent('unhandledrejection', { promise: Promise.resolve(), reason });
const result = getErrorFromErrorEvent(event);
expect(result).toBe('Promise rejected with {"code":"ERR_001","details":"Something went wrong"}');
});
it('should return undefined when event.reason is not provided', () => {
const event = new PromiseRejectionEvent('unhandledrejection', { promise: Promise.resolve(), reason: undefined });
const result = getErrorFromErrorEvent(event);
expect(result).toBeUndefined();
});
it('should return null when event.reason is null', () => {
const event = new PromiseRejectionEvent('unhandledrejection', { promise: Promise.resolve(), reason: null });
const result = getErrorFromErrorEvent(event);
expect(result).toBeNull();
});
it('should handle circular references in object reason', () => {
const circularObj: Record<string, unknown> = { name: 'test' };
circularObj.self = circularObj;
const event = new PromiseRejectionEvent('unhandledrejection', { promise: Promise.resolve(), reason: circularObj });
const result = getErrorFromErrorEvent(event);
expect(result).toContain('Promise rejected with');
expect(result).toContain('<circular>');
});
});
});