-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathanalytics.captureException.unit.tests.js
More file actions
84 lines (75 loc) · 3.1 KB
/
analytics.captureException.unit.tests.js
File metadata and controls
84 lines (75 loc) · 3.1 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
import { jest, beforeEach, afterEach, describe, test, expect } from '@jest/globals';
describe('Analytics captureException():', () => {
let AnalyticsService;
let mockPostHogInstance;
beforeEach(async () => {
jest.resetModules();
mockPostHogInstance = {
capture: jest.fn(),
identify: jest.fn(),
groupIdentify: jest.fn(),
getFeatureFlag: jest.fn().mockResolvedValue(undefined),
isFeatureEnabled: jest.fn().mockResolvedValue(undefined),
shutdown: jest.fn().mockResolvedValue(undefined),
};
jest.unstable_mockModule('posthog-node', () => ({
PostHog: jest.fn().mockImplementation(() => mockPostHogInstance),
}));
jest.unstable_mockModule('../../../config/index.js', () => ({
default: { analytics: { posthog: { enabled: true, key: 'phc_test', host: 'https://eu.i.posthog.com', appTag: 'trawl' } } },
}));
const mod = await import('../analytics.js');
AnalyticsService = mod.default;
await AnalyticsService.init();
});
afterEach(() => { jest.restoreAllMocks(); });
test('emits $exception with default source="system" when no ctx', () => {
AnalyticsService.captureException(new Error('boom'));
expect(mockPostHogInstance.capture).toHaveBeenCalledWith(expect.objectContaining({
event: '$exception',
properties: expect.objectContaining({ source: 'system', $exception_message: 'boom' }),
}));
});
test('honours explicit ctx.source', () => {
AnalyticsService.captureException(new Error('boom'), { source: 'worker-callback' });
expect(mockPostHogInstance.capture).toHaveBeenCalledWith(expect.objectContaining({
properties: expect.objectContaining({ source: 'worker-callback' }),
}));
});
test('merges ctx.properties (logMessage/logLevel) into event', () => {
AnalyticsService.captureException(new Error('boom'), {
distinctId: 'u1',
properties: { logMessage: 'something failed', logLevel: 'error' },
});
expect(mockPostHogInstance.capture).toHaveBeenCalledWith(expect.objectContaining({
distinctId: 'u1',
properties: expect.objectContaining({
logMessage: 'something failed',
logLevel: 'error',
source: 'system',
}),
}));
});
test('ctx.properties.source wins over system default', () => {
AnalyticsService.captureException(new Error('boom'), {
properties: { source: 'stripe-webhook' },
});
expect(mockPostHogInstance.capture).toHaveBeenCalledWith(expect.objectContaining({
properties: expect.objectContaining({ source: 'stripe-webhook' }),
}));
});
test('explicit ctx.source wins over ctx.properties.source', () => {
AnalyticsService.captureException(new Error('boom'), {
source: 'cron',
properties: { source: 'web' },
});
expect(mockPostHogInstance.capture).toHaveBeenCalledWith(expect.objectContaining({
properties: expect.objectContaining({ source: 'cron' }),
}));
});
test('no-op when err is null/undefined', () => {
AnalyticsService.captureException(null);
AnalyticsService.captureException(undefined);
expect(mockPostHogInstance.capture).not.toHaveBeenCalled();
});
});