-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcatcher.user.test.ts
More file actions
75 lines (58 loc) · 2.6 KB
/
catcher.user.test.ts
File metadata and controls
75 lines (58 loc) · 2.6 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
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { BreadcrumbManager } from '../src/addons/breadcrumbs';
import { wait, createTransport, getLastPayload, createCatcher } from './catcher.helpers';
const mockParse = vi.hoisted(() => vi.fn().mockResolvedValue([]));
vi.mock('@hawk.so/core', async (importOriginal) => {
const actual = await importOriginal<typeof import('@hawk.so/core')>();
return { ...actual, StackParser: class { parse = mockParse; } };
});
describe('Catcher', () => {
beforeEach(() => {
localStorage.clear();
mockParse.mockResolvedValue([]);
(BreadcrumbManager as any).instance = null;
});
// ── User identity ─────────────────────────────────────────────────────────
//
// The Catcher tracks who caused the error. When no user is configured it
// falls back to a generated anonymous ID that persists across events.
describe('user identity', () => {
it('should generate and persist anonymous ID when no user is configured', async () => {
const { sendSpy, transport } = createTransport();
const hawk = createCatcher(transport);
hawk.send(new Error('first'));
await wait();
const id1 = getLastPayload(sendSpy).user?.id;
hawk.send(new Error('second'));
await wait();
const id2 = getLastPayload(sendSpy).user?.id;
expect(id1).toBeTruthy();
expect(id1).toBe(id2);
});
it('should include user configured via setUser()', async () => {
const { sendSpy, transport } = createTransport();
const hawk = createCatcher(transport);
hawk.setUser({ id: 'user-1', name: 'Alice' });
hawk.send(new Error('e'));
await wait();
expect(getLastPayload(sendSpy).user).toMatchObject({ id: 'user-1', name: 'Alice' });
});
it('should include user configured via constructor', async () => {
const { sendSpy, transport } = createTransport();
createCatcher(transport, { user: { id: 'user-2' } }).send(new Error('e'));
await wait();
expect(getLastPayload(sendSpy).user).toMatchObject({ id: 'user-2' });
});
it('should revert to an anonymous identity after clearUser()', async () => {
const { sendSpy, transport } = createTransport();
const hawk = createCatcher(transport);
hawk.setUser({ id: 'user-1' });
hawk.clearUser();
hawk.send(new Error('e'));
await wait();
const user = getLastPayload(sendSpy).user;
expect(user?.id).toBeTruthy();
expect(user?.id).not.toBe('user-1');
});
});
});