Skip to content

Commit 90d7ecb

Browse files
committed
test: tests added for getErrorFromEvent
1 parent 5e5ac22 commit 90d7ecb

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import { describe, it, expect, vi, beforeEach } from 'vitest';
2+
import { getErrorFromEvent } from '../../src/utils/event';
3+
4+
vi.mock('@hawk.so/core', () => ({ log: vi.fn(), isLoggerSet: vi.fn(() => true), setLogger: vi.fn() }));
5+
6+
vi.mock('../../src/modules/sanitizer', () => ({
7+
default: {
8+
sanitize: vi.fn((data) => data),
9+
},
10+
}));
11+
12+
import Sanitizer from '../../src/modules/sanitizer';
13+
14+
describe('getErrorFromEvent', () => {
15+
beforeEach(() => {
16+
vi.clearAllMocks();
17+
});
18+
19+
describe('ErrorEvent', () => {
20+
it('should return the Error when event.error is an Error instance', () => {
21+
const error = new Error('Test error');
22+
const event = new ErrorEvent('error', { error });
23+
24+
const result = getErrorFromEvent(event);
25+
26+
expect(result).toBe(error);
27+
expect(Sanitizer.sanitize).toHaveBeenCalledWith(error);
28+
});
29+
30+
it('should return the DOMException when event.error is a DOMException', () => {
31+
const error = new DOMException('Network error', 'NetworkError');
32+
const event = new ErrorEvent('error', { error });
33+
34+
const result = getErrorFromEvent(event);
35+
36+
expect(result).toBe(error);
37+
expect(Sanitizer.sanitize).toHaveBeenCalledWith(error);
38+
});
39+
40+
it('should return the message when event.error is not provided and message is a string', () => {
41+
const event = new ErrorEvent('error', { message: 'Script error.' });
42+
43+
const result = getErrorFromEvent(event);
44+
45+
expect(result).toBe('Script error.');
46+
expect(Sanitizer.sanitize).toHaveBeenCalledWith('Script error.');
47+
});
48+
49+
it('should return empty string when event.error is not provided and message is empty', () => {
50+
const event = new ErrorEvent('error', { message: '' });
51+
52+
const result = getErrorFromEvent(event);
53+
54+
expect(result).toBe('');
55+
expect(Sanitizer.sanitize).toHaveBeenCalledWith('');
56+
});
57+
});
58+
59+
describe('PromiseRejectionEvent', () => {
60+
it('should return the Error when event.reason is an Error instance', () => {
61+
const reason = new Error('Promise rejected');
62+
const event = new PromiseRejectionEvent('unhandledrejection', { promise: Promise.resolve(), reason });
63+
64+
const result = getErrorFromEvent(event);
65+
66+
expect(result).toBe(reason);
67+
expect(Sanitizer.sanitize).toHaveBeenCalledWith(reason);
68+
});
69+
70+
it('should return the string when event.reason is a string', () => {
71+
const reason = 'Promise rejected with string';
72+
const event = new PromiseRejectionEvent('unhandledrejection', { promise: Promise.resolve(), reason });
73+
74+
const result = getErrorFromEvent(event);
75+
76+
expect(result).toBe(reason);
77+
expect(Sanitizer.sanitize).toHaveBeenCalledWith(reason);
78+
});
79+
80+
it('should return stringified object when event.reason is a plain object', () => {
81+
const reason = { code: 'ERR_001', details: 'Something went wrong' };
82+
const event = new PromiseRejectionEvent('unhandledrejection', { promise: Promise.resolve(), reason });
83+
84+
const result = getErrorFromEvent(event);
85+
86+
expect(result).toBe('Promise rejected with {"code":"ERR_001","details":"Something went wrong"}');
87+
});
88+
89+
it('should return undefined when event.reason is not provided', () => {
90+
const event = new PromiseRejectionEvent('unhandledrejection', { promise: Promise.resolve(), reason: undefined });
91+
92+
const result = getErrorFromEvent(event);
93+
94+
expect(result).toBeUndefined();
95+
expect(Sanitizer.sanitize).toHaveBeenCalledWith(undefined);
96+
});
97+
98+
it('should return null when event.reason is null', () => {
99+
const event = new PromiseRejectionEvent('unhandledrejection', { promise: Promise.resolve(), reason: null });
100+
101+
const result = getErrorFromEvent(event);
102+
103+
expect(result).toBeNull();
104+
expect(Sanitizer.sanitize).toHaveBeenCalledWith(null);
105+
});
106+
107+
it('should handle circular references in object reason', () => {
108+
vi.mocked(Sanitizer.sanitize).mockImplementation((data) => {
109+
if (data !== null && typeof data === 'object') {
110+
const seen = new WeakSet<object>();
111+
const sanitize = (obj: unknown): unknown => {
112+
if (obj !== null && typeof obj === 'object') {
113+
if (seen.has(obj as object)) {
114+
return '<circular>';
115+
}
116+
seen.add(obj as object);
117+
if (Array.isArray(obj)) {
118+
return obj.map(sanitize);
119+
}
120+
const result: Record<string, unknown> = {};
121+
for (const [key, value] of Object.entries(obj)) {
122+
result[key] = sanitize(value);
123+
}
124+
return result;
125+
}
126+
return obj;
127+
};
128+
return sanitize(data);
129+
}
130+
return data;
131+
});
132+
133+
const circularObj: Record<string, unknown> = { name: 'test' };
134+
circularObj.self = circularObj;
135+
136+
const event = new PromiseRejectionEvent('unhandledrejection', { promise: Promise.resolve(), reason: circularObj });
137+
138+
const result = getErrorFromEvent(event);
139+
140+
expect(result).toContain('Promise rejected with');
141+
expect(result).toContain('<circular>');
142+
});
143+
});
144+
});

0 commit comments

Comments
 (0)