-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcatcher.context.test.ts
More file actions
69 lines (54 loc) · 2.49 KB
/
catcher.context.test.ts
File metadata and controls
69 lines (54 loc) · 2.49 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
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;
});
// ── Context enrichment ────────────────────────────────────────────────────
//
// The Catcher attaches arbitrary developer-supplied context data to every event.
describe('context enrichment', () => {
it('should include global context set via setContext()', async () => {
const { sendSpy, transport } = createTransport();
const hawk = createCatcher(transport);
hawk.setContext({ env: 'production' });
hawk.send(new Error('e'));
await wait();
expect(getLastPayload(sendSpy).context).toMatchObject({ env: 'production' });
});
it('should include per-send context passed to send()', async () => {
const { sendSpy, transport } = createTransport();
createCatcher(transport).send(new Error('e'), { requestId: 'abc123' });
await wait();
expect(getLastPayload(sendSpy).context).toMatchObject({ requestId: 'abc123' });
});
it('should ignore setContext when called with a non-object value', async () => {
const { sendSpy, transport } = createTransport();
const hawk = createCatcher(transport);
hawk.setContext({ original: true });
hawk.setContext(42 as never);
hawk.send(new Error('e'));
await wait();
expect(getLastPayload(sendSpy).context).toMatchObject({ original: true });
});
it('should merge global and per-send context, per-send wins on key collision', async () => {
const { sendSpy, transport } = createTransport();
const hawk = createCatcher(transport, { context: { key: 'global', shared: 1 } });
hawk.send(new Error('e'), { key: 'local', extra: 2 });
await wait();
expect(getLastPayload(sendSpy).context).toMatchObject({
key: 'local',
shared: 1,
extra: 2,
});
});
});
});