@@ -2,13 +2,33 @@ import { TelemetryCollector } from '../telemetry';
2
2
3
3
jest . useFakeTimers ( ) ;
4
4
5
+ const TEST_PK = 'pk_test_Zm9vLWJhci0xMy5jbGVyay5hY2NvdW50cy5kZXYk' ;
6
+
5
7
describe ( 'TelemetryCollector' , ( ) => {
6
8
test ( 'does nothing when disabled' , async ( ) => {
7
9
const fetchSpy = jest . spyOn ( global , 'fetch' ) ;
8
10
9
11
const collector = new TelemetryCollector ( {
10
12
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 ,
12
32
} ) ;
13
33
14
34
collector . record ( 'TEST_EVENT' , { } ) ;
@@ -18,6 +38,8 @@ describe('TelemetryCollector', () => {
18
38
expect ( fetchSpy ) . not . toHaveBeenCalled ( ) ;
19
39
20
40
fetchSpy . mockRestore ( ) ;
41
+
42
+ process . env . CLERK_TELEMETRY_DISABLED = undefined ;
21
43
} ) ;
22
44
23
45
test ( 'does not send events when debug is enabled, logs them instead' , async ( ) => {
@@ -26,7 +48,7 @@ describe('TelemetryCollector', () => {
26
48
27
49
const collector = new TelemetryCollector ( {
28
50
debug : true ,
29
- publishableKey : 'pk_test_Zm9vLWJhci0xMy5jbGVyay5hY2NvdW50cy5kZXYk' ,
51
+ publishableKey : TEST_PK ,
30
52
} ) ;
31
53
32
54
collector . record ( 'TEST_EVENT' , { } ) ;
@@ -55,11 +77,49 @@ describe('TelemetryCollector', () => {
55
77
fetchSpy . mockRestore ( ) ;
56
78
} ) ;
57
79
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
+
58
118
test ( 'sends events after a delay when buffer is not full' , async ( ) => {
59
119
const fetchSpy = jest . spyOn ( global , 'fetch' ) ;
60
120
61
121
const collector = new TelemetryCollector ( {
62
- publishableKey : 'pk_test_Zm9vLWJhci0xMy5jbGVyay5hY2NvdW50cy5kZXYk' ,
122
+ publishableKey : TEST_PK ,
63
123
} ) ;
64
124
65
125
collector . record ( 'TEST_EVENT' , { } ) ;
@@ -76,7 +136,7 @@ describe('TelemetryCollector', () => {
76
136
77
137
const collector = new TelemetryCollector ( {
78
138
maxBufferSize : 2 ,
79
- publishableKey : 'pk_test_Zm9vLWJhci0xMy5jbGVyay5hY2NvdW50cy5kZXYk' ,
139
+ publishableKey : TEST_PK ,
80
140
} ) ;
81
141
82
142
collector . record ( 'TEST_EVENT' , { } ) ;
0 commit comments