11import { afterEach , beforeEach , describe , expect , it , jest , mock , spyOn } from 'bun:test' ;
2- import { metrics } from '@sentry/core' ;
2+
3+ const mockGauge = jest . fn ( ) ;
4+ const mockCount = jest . fn ( ) ;
35
46const mockElu = { idle : 700 , active : 300 , utilization : 0.3 } ;
57const mockEluDelta = { idle : 700 , active : 300 , utilization : 0.3 } ;
@@ -12,16 +14,19 @@ mock.module('perf_hooks', () => ({
1214 performance : { eventLoopUtilization : mockEventLoopUtilization } ,
1315} ) ) ;
1416
17+ const actualCore = await import ( '@sentry/core' ) ;
18+ mock . module ( '@sentry/core' , ( ) => ( {
19+ ...actualCore ,
20+ metrics : { ...actualCore . metrics , gauge : mockGauge , count : mockCount } ,
21+ } ) ) ;
22+
1523const { bunRuntimeMetricsIntegration } = await import ( '../../src/integrations/bunRuntimeMetrics' ) ;
1624
1725describe ( 'bunRuntimeMetricsIntegration' , ( ) => {
18- let gaugeSpy : ReturnType < typeof spyOn > ;
19- let countSpy : ReturnType < typeof spyOn > ;
20-
2126 beforeEach ( ( ) => {
2227 jest . useFakeTimers ( ) ;
23- gaugeSpy = spyOn ( metrics , 'gauge' ) . mockImplementation ( ( ) => undefined ) ;
24- countSpy = spyOn ( metrics , 'count' ) . mockImplementation ( ( ) => undefined ) ;
28+ mockGauge . mockClear ( ) ;
29+ mockCount . mockClear ( ) ;
2530
2631 spyOn ( process , 'cpuUsage' ) . mockReturnValue ( { user : 500_000 , system : 200_000 } ) ;
2732 spyOn ( process , 'memoryUsage' ) . mockReturnValue ( {
@@ -50,9 +55,9 @@ describe('bunRuntimeMetricsIntegration', () => {
5055 const integration = bunRuntimeMetricsIntegration ( { collectionIntervalMs : 1_000 } ) ;
5156 integration . setup ( ) ;
5257
53- expect ( gaugeSpy ) . not . toHaveBeenCalled ( ) ;
58+ expect ( mockGauge ) . not . toHaveBeenCalled ( ) ;
5459 jest . advanceTimersByTime ( 1_000 ) ;
55- expect ( gaugeSpy ) . toHaveBeenCalled ( ) ;
60+ expect ( mockGauge ) . toHaveBeenCalled ( ) ;
5661 } ) ;
5762
5863 it ( 'does not throw if performance.eventLoopUtilization is unavailable' , ( ) => {
@@ -75,16 +80,16 @@ describe('bunRuntimeMetricsIntegration', () => {
7580 integration . setup ( ) ;
7681 jest . advanceTimersByTime ( 1_000 ) ;
7782
78- expect ( gaugeSpy ) . toHaveBeenCalledWith ( 'bun.runtime.cpu.utilization' , expect . any ( Number ) , ORIGIN ) ;
83+ expect ( mockGauge ) . toHaveBeenCalledWith ( 'bun.runtime.cpu.utilization' , expect . any ( Number ) , ORIGIN ) ;
7984 } ) ;
8085
8186 it ( 'does not emit cpu.user / cpu.system by default (opt-in)' , ( ) => {
8287 const integration = bunRuntimeMetricsIntegration ( { collectionIntervalMs : 1_000 } ) ;
8388 integration . setup ( ) ;
8489 jest . advanceTimersByTime ( 1_000 ) ;
8590
86- expect ( gaugeSpy ) . not . toHaveBeenCalledWith ( 'bun.runtime.cpu.user' , expect . anything ( ) , expect . anything ( ) ) ;
87- expect ( gaugeSpy ) . not . toHaveBeenCalledWith ( 'bun.runtime.cpu.system' , expect . anything ( ) , expect . anything ( ) ) ;
91+ expect ( mockGauge ) . not . toHaveBeenCalledWith ( 'bun.runtime.cpu.user' , expect . anything ( ) , expect . anything ( ) ) ;
92+ expect ( mockGauge ) . not . toHaveBeenCalledWith ( 'bun.runtime.cpu.system' , expect . anything ( ) , expect . anything ( ) ) ;
8893 } ) ;
8994
9095 it ( 'emits cpu.user / cpu.system when cpuTime is opted in' , ( ) => {
@@ -95,27 +100,27 @@ describe('bunRuntimeMetricsIntegration', () => {
95100 integration . setup ( ) ;
96101 jest . advanceTimersByTime ( 1_000 ) ;
97102
98- expect ( gaugeSpy ) . toHaveBeenCalledWith ( 'bun.runtime.cpu.user' , expect . any ( Number ) , SECOND ) ;
99- expect ( gaugeSpy ) . toHaveBeenCalledWith ( 'bun.runtime.cpu.system' , expect . any ( Number ) , SECOND ) ;
103+ expect ( mockGauge ) . toHaveBeenCalledWith ( 'bun.runtime.cpu.user' , expect . any ( Number ) , SECOND ) ;
104+ expect ( mockGauge ) . toHaveBeenCalledWith ( 'bun.runtime.cpu.system' , expect . any ( Number ) , SECOND ) ;
100105 } ) ;
101106
102107 it ( 'emits mem.rss, mem.heap_used, mem.heap_total (default on)' , ( ) => {
103108 const integration = bunRuntimeMetricsIntegration ( { collectionIntervalMs : 1_000 } ) ;
104109 integration . setup ( ) ;
105110 jest . advanceTimersByTime ( 1_000 ) ;
106111
107- expect ( gaugeSpy ) . toHaveBeenCalledWith ( 'bun.runtime.mem.rss' , 50_000_000 , BYTE ) ;
108- expect ( gaugeSpy ) . toHaveBeenCalledWith ( 'bun.runtime.mem.heap_used' , 20_000_000 , BYTE ) ;
109- expect ( gaugeSpy ) . toHaveBeenCalledWith ( 'bun.runtime.mem.heap_total' , 30_000_000 , BYTE ) ;
112+ expect ( mockGauge ) . toHaveBeenCalledWith ( 'bun.runtime.mem.rss' , 50_000_000 , BYTE ) ;
113+ expect ( mockGauge ) . toHaveBeenCalledWith ( 'bun.runtime.mem.heap_used' , 20_000_000 , BYTE ) ;
114+ expect ( mockGauge ) . toHaveBeenCalledWith ( 'bun.runtime.mem.heap_total' , 30_000_000 , BYTE ) ;
110115 } ) ;
111116
112117 it ( 'does not emit mem.external / mem.array_buffers by default (opt-in)' , ( ) => {
113118 const integration = bunRuntimeMetricsIntegration ( { collectionIntervalMs : 1_000 } ) ;
114119 integration . setup ( ) ;
115120 jest . advanceTimersByTime ( 1_000 ) ;
116121
117- expect ( gaugeSpy ) . not . toHaveBeenCalledWith ( 'bun.runtime.mem.external' , expect . anything ( ) , expect . anything ( ) ) ;
118- expect ( gaugeSpy ) . not . toHaveBeenCalledWith ( 'bun.runtime.mem.array_buffers' , expect . anything ( ) , expect . anything ( ) ) ;
122+ expect ( mockGauge ) . not . toHaveBeenCalledWith ( 'bun.runtime.mem.external' , expect . anything ( ) , expect . anything ( ) ) ;
123+ expect ( mockGauge ) . not . toHaveBeenCalledWith ( 'bun.runtime.mem.array_buffers' , expect . anything ( ) , expect . anything ( ) ) ;
119124 } ) ;
120125
121126 it ( 'emits mem.external / mem.array_buffers when opted in' , ( ) => {
@@ -126,16 +131,16 @@ describe('bunRuntimeMetricsIntegration', () => {
126131 integration . setup ( ) ;
127132 jest . advanceTimersByTime ( 1_000 ) ;
128133
129- expect ( gaugeSpy ) . toHaveBeenCalledWith ( 'bun.runtime.mem.external' , 1_000_000 , BYTE ) ;
130- expect ( gaugeSpy ) . toHaveBeenCalledWith ( 'bun.runtime.mem.array_buffers' , 500_000 , BYTE ) ;
134+ expect ( mockGauge ) . toHaveBeenCalledWith ( 'bun.runtime.mem.external' , 1_000_000 , BYTE ) ;
135+ expect ( mockGauge ) . toHaveBeenCalledWith ( 'bun.runtime.mem.array_buffers' , 500_000 , BYTE ) ;
131136 } ) ;
132137
133138 it ( 'emits event loop utilization metric' , ( ) => {
134139 const integration = bunRuntimeMetricsIntegration ( { collectionIntervalMs : 1_000 } ) ;
135140 integration . setup ( ) ;
136141 jest . advanceTimersByTime ( 1_000 ) ;
137142
138- expect ( gaugeSpy ) . toHaveBeenCalledWith ( 'bun.runtime.event_loop.utilization' , 0.3 , ORIGIN ) ;
143+ expect ( mockGauge ) . toHaveBeenCalledWith ( 'bun.runtime.event_loop.utilization' , 0.3 , ORIGIN ) ;
139144 } ) ;
140145
141146 it ( 'does not emit event loop utilization if performance.eventLoopUtilization threw during setup' , ( ) => {
@@ -147,7 +152,7 @@ describe('bunRuntimeMetricsIntegration', () => {
147152 integration . setup ( ) ;
148153 jest . advanceTimersByTime ( 1_000 ) ;
149154
150- expect ( gaugeSpy ) . not . toHaveBeenCalledWith (
155+ expect ( mockGauge ) . not . toHaveBeenCalledWith (
151156 'bun.runtime.event_loop.utilization' ,
152157 expect . anything ( ) ,
153158 expect . anything ( ) ,
@@ -159,7 +164,7 @@ describe('bunRuntimeMetricsIntegration', () => {
159164 integration . setup ( ) ;
160165 jest . advanceTimersByTime ( 1_000 ) ;
161166
162- expect ( countSpy ) . toHaveBeenCalledWith ( 'bun.runtime.process.uptime' , expect . any ( Number ) , SECOND ) ;
167+ expect ( mockCount ) . toHaveBeenCalledWith ( 'bun.runtime.process.uptime' , expect . any ( Number ) , SECOND ) ;
163168 } ) ;
164169 } ) ;
165170
@@ -172,7 +177,7 @@ describe('bunRuntimeMetricsIntegration', () => {
172177 integration . setup ( ) ;
173178 jest . advanceTimersByTime ( 1_000 ) ;
174179
175- expect ( gaugeSpy ) . not . toHaveBeenCalledWith ( 'bun.runtime.cpu.utilization' , expect . anything ( ) , expect . anything ( ) ) ;
180+ expect ( mockGauge ) . not . toHaveBeenCalledWith ( 'bun.runtime.cpu.utilization' , expect . anything ( ) , expect . anything ( ) ) ;
176181 } ) ;
177182
178183 it ( 'skips mem.rss when memRss is false' , ( ) => {
@@ -183,7 +188,7 @@ describe('bunRuntimeMetricsIntegration', () => {
183188 integration . setup ( ) ;
184189 jest . advanceTimersByTime ( 1_000 ) ;
185190
186- expect ( gaugeSpy ) . not . toHaveBeenCalledWith ( 'bun.runtime.mem.rss' , expect . anything ( ) , expect . anything ( ) ) ;
191+ expect ( mockGauge ) . not . toHaveBeenCalledWith ( 'bun.runtime.mem.rss' , expect . anything ( ) , expect . anything ( ) ) ;
187192 } ) ;
188193
189194 it ( 'skips event loop utilization when eventLoopUtilization is false' , ( ) => {
@@ -194,7 +199,7 @@ describe('bunRuntimeMetricsIntegration', () => {
194199 integration . setup ( ) ;
195200 jest . advanceTimersByTime ( 1_000 ) ;
196201
197- expect ( gaugeSpy ) . not . toHaveBeenCalledWith (
202+ expect ( mockGauge ) . not . toHaveBeenCalledWith (
198203 'bun.runtime.event_loop.utilization' ,
199204 expect . anything ( ) ,
200205 expect . anything ( ) ,
@@ -209,7 +214,7 @@ describe('bunRuntimeMetricsIntegration', () => {
209214 integration . setup ( ) ;
210215 jest . advanceTimersByTime ( 1_000 ) ;
211216
212- expect ( countSpy ) . not . toHaveBeenCalledWith ( 'bun.runtime.process.uptime' , expect . anything ( ) , expect . anything ( ) ) ;
217+ expect ( mockCount ) . not . toHaveBeenCalledWith ( 'bun.runtime.process.uptime' , expect . anything ( ) , expect . anything ( ) ) ;
213218 } ) ;
214219 } ) ;
215220
@@ -225,10 +230,10 @@ describe('bunRuntimeMetricsIntegration', () => {
225230
226231 // Should fire at minimum 1000ms, not at 100ms
227232 jest . advanceTimersByTime ( 100 ) ;
228- expect ( gaugeSpy ) . not . toHaveBeenCalled ( ) ;
233+ expect ( mockGauge ) . not . toHaveBeenCalled ( ) ;
229234
230235 jest . advanceTimersByTime ( 900 ) ;
231- expect ( gaugeSpy ) . toHaveBeenCalled ( ) ;
236+ expect ( mockGauge ) . toHaveBeenCalled ( ) ;
232237 } ) ;
233238
234239 it ( 'falls back to default when NaN' , ( ) => {
@@ -241,10 +246,10 @@ describe('bunRuntimeMetricsIntegration', () => {
241246
242247 // Should fire at the default 30000ms, not at 1000ms
243248 jest . advanceTimersByTime ( 1000 ) ;
244- expect ( gaugeSpy ) . not . toHaveBeenCalled ( ) ;
249+ expect ( mockGauge ) . not . toHaveBeenCalled ( ) ;
245250
246251 jest . advanceTimersByTime ( 29_000 ) ;
247- expect ( gaugeSpy ) . toHaveBeenCalled ( ) ;
252+ expect ( mockGauge ) . toHaveBeenCalled ( ) ;
248253 } ) ;
249254 } ) ;
250255} ) ;
0 commit comments