-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathanalytics.captureException.unit.tests.js
More file actions
124 lines (113 loc) · 4.01 KB
/
Copy pathanalytics.captureException.unit.tests.js
File metadata and controls
124 lines (113 loc) · 4.01 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* Module dependencies.
*/
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(),
captureException: 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('calls SDK captureException with default source="system" when no ctx', () => {
const err = new Error('boom');
AnalyticsService.captureException(err);
expect(mockPostHogInstance.captureException).toHaveBeenCalledWith(
err,
'anonymous',
expect.objectContaining({ source: 'system' }),
);
});
test('honours explicit ctx.source', () => {
const err = new Error('boom');
AnalyticsService.captureException(err, { source: 'worker-callback' });
expect(mockPostHogInstance.captureException).toHaveBeenCalledWith(
err,
'anonymous',
expect.objectContaining({ source: 'worker-callback' }),
);
});
test('merges ctx.properties (logMessage/logLevel) into additionalProperties', () => {
const err = new Error('boom');
AnalyticsService.captureException(err, {
distinctId: 'u1',
properties: { logMessage: 'something failed', logLevel: 'error' },
});
expect(mockPostHogInstance.captureException).toHaveBeenCalledWith(
err,
'u1',
expect.objectContaining({
logMessage: 'something failed',
logLevel: 'error',
source: 'system',
}),
);
});
test('ctx.properties.source wins over system default', () => {
const err = new Error('boom');
AnalyticsService.captureException(err, {
properties: { source: 'stripe-webhook' },
});
expect(mockPostHogInstance.captureException).toHaveBeenCalledWith(
err,
'anonymous',
expect.objectContaining({ source: 'stripe-webhook' }),
);
});
test('explicit ctx.source wins over ctx.properties.source', () => {
const err = new Error('boom');
AnalyticsService.captureException(err, {
source: 'cron',
properties: { source: 'web' },
});
expect(mockPostHogInstance.captureException).toHaveBeenCalledWith(
err,
'anonymous',
expect.objectContaining({ source: 'cron' }),
);
});
test('no-op when err is null/undefined', () => {
AnalyticsService.captureException(null);
AnalyticsService.captureException(undefined);
expect(mockPostHogInstance.captureException).not.toHaveBeenCalled();
});
test('passes requestId in additionalProperties', () => {
const err = new Error('with-req');
AnalyticsService.captureException(err, { distinctId: 'u2', requestId: 'req-xyz' });
expect(mockPostHogInstance.captureException).toHaveBeenCalledWith(
err,
'u2',
expect.objectContaining({ requestId: 'req-xyz' }),
);
});
test('ctx.requestId wins over ctx.properties.requestId (authoritative)', () => {
const err = new Error('req-precedence');
AnalyticsService.captureException(err, {
requestId: 'authoritative-req',
properties: { requestId: 'should-be-overridden' },
});
expect(mockPostHogInstance.captureException).toHaveBeenCalledWith(
err,
'anonymous',
expect.objectContaining({ requestId: 'authoritative-req' }),
);
});
});