-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathanalytics.service.unit.tests.js
More file actions
297 lines (232 loc) · 9.71 KB
/
Copy pathanalytics.service.unit.tests.js
File metadata and controls
297 lines (232 loc) · 9.71 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/**
* Module dependencies.
*/
import { jest, beforeEach, afterEach, describe, test, expect } from '@jest/globals';
/**
* Unit tests for analytics service
*/
describe('Analytics service unit tests:', () => {
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('variant-a'),
isFeatureEnabled: jest.fn().mockResolvedValue(true),
shutdown: jest.fn().mockResolvedValue(undefined),
};
jest.unstable_mockModule('posthog-node', () => ({
PostHog: jest.fn().mockImplementation(() => mockPostHogInstance),
}));
});
afterEach(() => {
jest.restoreAllMocks();
});
describe('no-op mode (PostHog not configured)', () => {
test('should not create a client when key is missing', async () => {
jest.unstable_mockModule('../../../config/index.js', () => ({
default: { analytics: { posthog: {} } },
}));
const mod = await import('../analytics.js');
AnalyticsService = mod.default;
await AnalyticsService.init();
AnalyticsService.track('user-1', 'test-event');
AnalyticsService.identify('user-1', { name: 'Alice' });
AnalyticsService.groupIdentify('company', 'org-1', { name: 'Acme' });
const { PostHog } = await import('posthog-node');
expect(PostHog).not.toHaveBeenCalled();
});
test('should not create a client when analytics.posthog config section is missing', async () => {
jest.unstable_mockModule('../../../config/index.js', () => ({
default: {},
}));
const mod = await import('../analytics.js');
AnalyticsService = mod.default;
await AnalyticsService.init();
AnalyticsService.track('user-1', 'test-event');
const { PostHog } = await import('posthog-node');
expect(PostHog).not.toHaveBeenCalled();
});
test('getFeatureFlag should return undefined when not configured', async () => {
jest.unstable_mockModule('../../../config/index.js', () => ({
default: { analytics: { posthog: {} } },
}));
const mod = await import('../analytics.js');
AnalyticsService = mod.default;
await AnalyticsService.init();
const result = await AnalyticsService.getFeatureFlag('my-flag', 'user-1');
expect(result).toBeUndefined();
});
test('isFeatureEnabled should return undefined when not configured', async () => {
jest.unstable_mockModule('../../../config/index.js', () => ({
default: { analytics: { posthog: {} } },
}));
const mod = await import('../analytics.js');
AnalyticsService = mod.default;
await AnalyticsService.init();
const result = await AnalyticsService.isFeatureEnabled('my-flag', 'user-1');
expect(result).toBeUndefined();
});
test('shutdown should be safe when not configured', async () => {
jest.unstable_mockModule('../../../config/index.js', () => ({
default: { analytics: { posthog: {} } },
}));
const mod = await import('../analytics.js');
AnalyticsService = mod.default;
await AnalyticsService.init();
await expect(AnalyticsService.shutdown()).resolves.toBeUndefined();
});
});
describe('configured mode', () => {
beforeEach(async () => {
jest.unstable_mockModule('../../../config/index.js', () => ({
default: {
analytics: {
posthog: {
enabled: true,
key: 'phc_test_key',
host: 'https://test.posthog.com',
},
},
},
}));
});
test('should create PostHog client with correct config', async () => {
const mod = await import('../analytics.js');
AnalyticsService = mod.default;
await AnalyticsService.init();
const { PostHog } = await import('posthog-node');
expect(PostHog).toHaveBeenCalledWith('phc_test_key', { host: 'https://test.posthog.com' });
});
test('should use default host when not provided', async () => {
jest.resetModules();
jest.unstable_mockModule('posthog-node', () => ({
PostHog: jest.fn().mockImplementation(() => mockPostHogInstance),
}));
jest.unstable_mockModule('../../../config/index.js', () => ({
default: {
analytics: {
posthog: {
enabled: true,
key: 'phc_test_key',
host: '',
},
},
},
}));
const mod = await import('../analytics.js');
AnalyticsService = mod.default;
await AnalyticsService.init();
const { PostHog } = await import('posthog-node');
expect(PostHog).toHaveBeenCalledWith('phc_test_key', { host: 'https://eu.i.posthog.com' });
});
test('track should call client.capture with correct params', async () => {
const mod = await import('../analytics.js');
AnalyticsService = mod.default;
await AnalyticsService.init();
AnalyticsService.track('user-1', 'page_view', { path: '/' }, { company: 'org-1' });
expect(mockPostHogInstance.capture).toHaveBeenCalledWith({
distinctId: 'user-1',
event: 'page_view',
properties: { path: '/' },
groups: { company: 'org-1' },
});
});
test('identify should call client.identify with correct params', async () => {
const mod = await import('../analytics.js');
AnalyticsService = mod.default;
await AnalyticsService.init();
AnalyticsService.identify('user-1', { email: 'a@b.com' });
expect(mockPostHogInstance.identify).toHaveBeenCalledWith({
distinctId: 'user-1',
properties: { email: 'a@b.com' },
});
});
test('groupIdentify should call client.groupIdentify with correct params', async () => {
const mod = await import('../analytics.js');
AnalyticsService = mod.default;
await AnalyticsService.init();
AnalyticsService.groupIdentify('company', 'org-1', { name: 'Acme' });
expect(mockPostHogInstance.groupIdentify).toHaveBeenCalledWith({
groupType: 'company',
groupKey: 'org-1',
properties: { name: 'Acme' },
});
});
test('getFeatureFlag should delegate to client', async () => {
const mod = await import('../analytics.js');
AnalyticsService = mod.default;
await AnalyticsService.init();
const result = await AnalyticsService.getFeatureFlag('my-flag', 'user-1', { groups: {} });
expect(mockPostHogInstance.getFeatureFlag).toHaveBeenCalledWith('my-flag', 'user-1', { groups: {} });
expect(result).toBe('variant-a');
});
test('isFeatureEnabled should delegate to client', async () => {
const mod = await import('../analytics.js');
AnalyticsService = mod.default;
await AnalyticsService.init();
const result = await AnalyticsService.isFeatureEnabled('my-flag', 'user-1', { groups: {} });
expect(mockPostHogInstance.isFeatureEnabled).toHaveBeenCalledWith('my-flag', 'user-1', { groups: {} });
expect(result).toBe(true);
});
test('shutdown should flush and close the client', async () => {
const mod = await import('../analytics.js');
AnalyticsService = mod.default;
await AnalyticsService.init();
await AnalyticsService.shutdown();
expect(mockPostHogInstance.shutdown).toHaveBeenCalled();
});
test('shutdown should reset client to null (subsequent calls are no-op)', async () => {
const mod = await import('../analytics.js');
AnalyticsService = mod.default;
await AnalyticsService.init();
await AnalyticsService.shutdown();
// After shutdown, track should be a no-op (no error thrown)
AnalyticsService.track('user-1', 'test');
expect(mockPostHogInstance.capture).not.toHaveBeenCalled();
});
test('captureException should call SDK captureException with correct args', async () => {
const mod = await import('../analytics.js');
AnalyticsService = mod.default;
await AnalyticsService.init();
const err = new Error('test error');
err.stack = 'Error: test error\n at test.js:1:1';
AnalyticsService.captureException(err, { distinctId: 'user-1', requestId: 'req-abc' });
expect(mockPostHogInstance.captureException).toHaveBeenCalledWith(
err,
'user-1',
expect.objectContaining({ requestId: 'req-abc', source: 'system' }),
);
});
test('captureException should use anonymous distinctId when not provided', async () => {
const mod = await import('../analytics.js');
AnalyticsService = mod.default;
await AnalyticsService.init();
AnalyticsService.captureException(new Error('anon error'), {});
expect(mockPostHogInstance.captureException).toHaveBeenCalledWith(
expect.any(Error),
'anonymous',
expect.any(Object),
);
});
test('captureException should be a no-op when client is not initialised', async () => {
const mod = await import('../analytics.js');
AnalyticsService = mod.default;
// Do NOT call init — client stays null
AnalyticsService.captureException(new Error('no client'), { distinctId: 'user-1' });
expect(mockPostHogInstance.captureException).not.toHaveBeenCalled();
});
test('captureException should silently swallow client errors', async () => {
const mod = await import('../analytics.js');
AnalyticsService = mod.default;
await AnalyticsService.init();
mockPostHogInstance.captureException.mockImplementation(() => { throw new Error('client error'); });
// Should not throw
expect(() => AnalyticsService.captureException(new Error('err'), {})).not.toThrow();
});
});
});