Skip to content

Commit 596fae0

Browse files
test(analytics): fix inner beforeEach + add missing captureException source test
Add jest.resetModules() in capture source attribution beforeEach (latent flake risk). Add properties.source > system precedence test for captureException.
1 parent f293e80 commit 596fae0

3 files changed

Lines changed: 38 additions & 1 deletion

File tree

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,30 @@ git merge devkit-node/master
263263

264264
> Caution: resolve conflicts manually to preserve downstream customizations before pushing.
265265
266+
## :bar_chart: Analytics (PostHog)
267+
268+
The devkit ships an `analyticsService` backed by `posthog-node`. Enable via `config.analytics.posthog.enabled = true` + a valid `key`.
269+
270+
### Source attribution convention
271+
272+
Every `analytics.capture()` and `analytics.captureException()` event gets a `source` property. The lookup order is:
273+
274+
1. Explicit `source` param: `analytics.capture({ ..., source: 'cron' })`
275+
2. `properties.source` legacy/inline form
276+
3. `req.posthogContext.source` (auto-set by `posthogContextMiddleware`)
277+
4. `'system'` default
278+
279+
Canonical sources used downstream:
280+
281+
| Source | Meaning |
282+
|---|---|
283+
| `web` | Request from browser (UA not matched as CLI) |
284+
| `cli` | Request from `@trawlme/cli/<version>` (UA-parsed) |
285+
| `stripe-webhook` | Stripe POST `/api/billing/webhook` |
286+
| `worker-callback` | worker-puppeteer scrap completion callback |
287+
| `cron` | Scheduled background job |
288+
| `system` | Server-side fallback (no req, no caller override) |
289+
266290
## :pencil2: Contribute
267291

268292
Open issues and pull requests on [GitHub](https://github.com/pierreb-devkit/Node).

lib/services/tests/analytics.capture.unit.tests.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,10 @@ describe('Analytics capture() and enabled-flag:', () => {
286286
describe('source attribution:', () => {
287287
let AnalyticsService;
288288
beforeEach(async () => {
289+
jest.resetModules();
290+
jest.unstable_mockModule('posthog-node', () => ({
291+
PostHog: jest.fn().mockImplementation(() => mockPostHogInstance),
292+
}));
289293
jest.unstable_mockModule('../../../config/index.js', () => ({
290294
default: { analytics: { posthog: { enabled: true, key: 'phc_test', host: 'https://eu.i.posthog.com', appTag: 'trawl' } } },
291295
}));

lib/services/tests/analytics.captureException.unit.tests.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,16 @@ describe('Analytics captureException():', () => {
5757
}));
5858
});
5959

60-
test('ctx.properties.source wins over default but explicit ctx.source wins over ctx.properties.source', () => {
60+
test('ctx.properties.source wins over system default', () => {
61+
AnalyticsService.captureException(new Error('boom'), {
62+
properties: { source: 'stripe-webhook' },
63+
});
64+
expect(mockPostHogInstance.capture).toHaveBeenCalledWith(expect.objectContaining({
65+
properties: expect.objectContaining({ source: 'stripe-webhook' }),
66+
}));
67+
});
68+
69+
test('explicit ctx.source wins over ctx.properties.source', () => {
6170
AnalyticsService.captureException(new Error('boom'), {
6271
source: 'cron',
6372
properties: { source: 'web' },

0 commit comments

Comments
 (0)