-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcatcher.breadcrumbs.test.ts
More file actions
64 lines (50 loc) · 2.45 KB
/
catcher.breadcrumbs.test.ts
File metadata and controls
64 lines (50 loc) · 2.45 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
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;
});
// ── Breadcrumbs trail ─────────────────────────────────────────────────────
//
// The Catcher maintains a chronological trail of events leading up to the
// error. The trail is included in delivered events only when non-empty.
describe('breadcrumbs trail', () => {
it('should include recorded breadcrumbs', async () => {
const { sendSpy, transport } = createTransport();
const hawk = createCatcher(transport, { breadcrumbs: {} });
hawk.breadcrumbs.add({ message: 'button clicked', timestamp: Date.now() });
hawk.send(new Error('e'));
await wait();
const breadcrumbs = getLastPayload(sendSpy).breadcrumbs;
expect(Array.isArray(breadcrumbs)).toBe(true);
expect(breadcrumbs[0].message).toBe('button clicked');
});
it('should omit breadcrumbs when none have been recorded', async () => {
const { sendSpy, transport } = createTransport();
createCatcher(transport, { breadcrumbs: {} }).send(new Error('e'));
await wait();
expect(getLastPayload(sendSpy).breadcrumbs).toBeFalsy();
});
it('should return an empty array from breadcrumbs.get when breadcrumbs are disabled', () => {
const { transport } = createTransport();
expect(createCatcher(transport).breadcrumbs.get()).toEqual([]);
});
it('should omit breadcrumbs cleared before payload was sent', async () => {
const { sendSpy, transport } = createTransport();
const hawk = createCatcher(transport, { breadcrumbs: {} });
hawk.breadcrumbs.add({ message: 'click', timestamp: Date.now() });
hawk.breadcrumbs.clear();
hawk.send(new Error('e'));
await wait();
expect(getLastPayload(sendSpy).breadcrumbs).toBeFalsy();
});
});
});