Skip to content

Commit 2f72334

Browse files
committed
chore(shared): Add additional test cases
1 parent a85cd00 commit 2f72334

File tree

2 files changed

+70
-6
lines changed

2 files changed

+70
-6
lines changed

packages/shared/src/__tests__/telemetry.test.ts

+64-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,33 @@ import { TelemetryCollector } from '../telemetry';
22

33
jest.useFakeTimers();
44

5+
const TEST_PK = 'pk_test_Zm9vLWJhci0xMy5jbGVyay5hY2NvdW50cy5kZXYk';
6+
57
describe('TelemetryCollector', () => {
68
test('does nothing when disabled', async () => {
79
const fetchSpy = jest.spyOn(global, 'fetch');
810

911
const collector = new TelemetryCollector({
1012
disabled: true,
11-
publishableKey: 'pk_test_Zm9vLWJhci0xMy5jbGVyay5hY2NvdW50cy5kZXYk',
13+
publishableKey: TEST_PK,
14+
});
15+
16+
collector.record('TEST_EVENT', {});
17+
18+
jest.runAllTimers();
19+
20+
expect(fetchSpy).not.toHaveBeenCalled();
21+
22+
fetchSpy.mockRestore();
23+
});
24+
25+
test('does nothing when CLERK_TELEMETRY_DISABLED is set', async () => {
26+
process.env.CLERK_TELEMETRY_DISABLED = '1';
27+
28+
const fetchSpy = jest.spyOn(global, 'fetch');
29+
30+
const collector = new TelemetryCollector({
31+
publishableKey: TEST_PK,
1232
});
1333

1434
collector.record('TEST_EVENT', {});
@@ -18,6 +38,8 @@ describe('TelemetryCollector', () => {
1838
expect(fetchSpy).not.toHaveBeenCalled();
1939

2040
fetchSpy.mockRestore();
41+
42+
process.env.CLERK_TELEMETRY_DISABLED = undefined;
2143
});
2244

2345
test('does not send events when debug is enabled, logs them instead', async () => {
@@ -26,7 +48,7 @@ describe('TelemetryCollector', () => {
2648

2749
const collector = new TelemetryCollector({
2850
debug: true,
29-
publishableKey: 'pk_test_Zm9vLWJhci0xMy5jbGVyay5hY2NvdW50cy5kZXYk',
51+
publishableKey: TEST_PK,
3052
});
3153

3254
collector.record('TEST_EVENT', {});
@@ -55,11 +77,49 @@ describe('TelemetryCollector', () => {
5577
fetchSpy.mockRestore();
5678
});
5779

80+
test('enables debug via environment variable', async () => {
81+
process.env.CLERK_TELEMETRY_DEBUG = '1';
82+
83+
const fetchSpy = jest.spyOn(global, 'fetch');
84+
const consoleSpy = jest.spyOn(global.console, 'log');
85+
86+
const collector = new TelemetryCollector({
87+
publishableKey: TEST_PK,
88+
});
89+
90+
collector.record('TEST_EVENT', {});
91+
92+
jest.runAllTimers();
93+
94+
expect(fetchSpy).not.toHaveBeenCalled();
95+
96+
expect(consoleSpy.mock.calls).toMatchInlineSnapshot(`
97+
[
98+
[
99+
{
100+
"cv": "",
101+
"event": "TEST_EVENT",
102+
"it": "development",
103+
"payload": {},
104+
"pk": "pk_test_Zm9vLWJhci0xMy5jbGVyay5hY2NvdW50cy5kZXYk",
105+
"sdk": undefined,
106+
"sdkv": undefined,
107+
},
108+
],
109+
]
110+
`);
111+
112+
consoleSpy.mockRestore();
113+
fetchSpy.mockRestore();
114+
115+
process.env.CLERK_TELEMETRY_DEBUG = undefined;
116+
});
117+
58118
test('sends events after a delay when buffer is not full', async () => {
59119
const fetchSpy = jest.spyOn(global, 'fetch');
60120

61121
const collector = new TelemetryCollector({
62-
publishableKey: 'pk_test_Zm9vLWJhci0xMy5jbGVyay5hY2NvdW50cy5kZXYk',
122+
publishableKey: TEST_PK,
63123
});
64124

65125
collector.record('TEST_EVENT', {});
@@ -76,7 +136,7 @@ describe('TelemetryCollector', () => {
76136

77137
const collector = new TelemetryCollector({
78138
maxBufferSize: 2,
79-
publishableKey: 'pk_test_Zm9vLWJhci0xMy5jbGVyay5hY2NvdW50cy5kZXYk',
139+
publishableKey: TEST_PK,
80140
});
81141

82142
collector.record('TEST_EVENT', {});

packages/shared/src/telemetry.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ export class TelemetryCollector {
141141
return true;
142142
}
143143

144+
get isDebug(): boolean {
145+
return this.#config.debug || (typeof process !== 'undefined' && isTruthy(process.env.CLERK_TELEMETRY_DEBUG));
146+
}
147+
144148
record(event: TelemetryEvent['event'], payload: TelemetryEvent['payload']) {
145149
const preparedPayload = this.#preparePayload(event, payload);
146150

@@ -156,7 +160,7 @@ export class TelemetryCollector {
156160
}
157161

158162
#shouldRecord(): boolean {
159-
return this.isEnabled && !this.#config.debug && Math.random() <= this.#config.samplingRate;
163+
return this.isEnabled && !this.isDebug && Math.random() <= this.#config.samplingRate;
160164
}
161165

162166
#scheduleFlush(): void {
@@ -215,7 +219,7 @@ export class TelemetryCollector {
215219
* If running in debug mode, log the event and its payload to the console.
216220
*/
217221
#logEvent(event: TelemetryEvent['event'], payload: Record<string, any>) {
218-
if (!this.#config.debug) {
222+
if (!this.isDebug) {
219223
return;
220224
}
221225

0 commit comments

Comments
 (0)