@@ -4,7 +4,7 @@ import type { Transaction } from '@sentry/types';
4
4
import type { Handle } from '@sveltejs/kit' ;
5
5
import { vi } from 'vitest' ;
6
6
7
- import { sentryHandle } from '../../src/server/handle' ;
7
+ import { sentryHandle , transformPageChunk } from '../../src/server/handle' ;
8
8
import { getDefaultNodeClientOptions } from '../utils' ;
9
9
10
10
const mockCaptureException = vi . fn ( ) ;
@@ -94,22 +94,22 @@ function resolve(type: Type, isError: boolean): Parameters<Handle>[0]['resolve']
94
94
let hub : Hub ;
95
95
let client : NodeClient ;
96
96
97
- describe ( 'handleSentry' , ( ) => {
98
- before
8000
All ( ( ) => {
99
- addTracingExtensions ( ) ;
100
- } ) ;
97
+ beforeAll ( ( ) => {
98
+ addTracingExtensions ( ) ;
99
+ } ) ;
101
100
102
- beforeEach ( ( ) => {
103
- mockScope = new Scope ( ) ;
104
- const options = getDefaultNodeClientOptions ( { tracesSampleRate : 1.0 } ) ;
105
- client = new NodeClient ( options ) ;
106
- hub = new Hub ( client ) ;
107
- makeMain ( hub ) ;
101
+ beforeEach ( ( ) => {
102
+ mockScope = new Scope ( ) ;
103
+ const options = getDefaultNodeClientOptions ( { tracesSampleRate : 1.0 } ) ;
104
+ client = new NodeClient ( options ) ;
105
+ hub = new Hub ( client ) ;
106
+ makeMain ( hub ) ;
108
107
109
- mockCaptureException . mockClear ( ) ;
110
- mockAddExceptionMechanism . mockClear ( ) ;
111
- } ) ;
108
+ mockCaptureException . mockClear ( ) ;
109
+ mockAddExceptionMechanism . mockClear ( ) ;
110
+ } ) ;
112
111
112
+ describe ( 'handleSentry' , ( ) => {
113
113
describe . each ( [
114
114
// isSync, isError, expectedResponse
115
115
[ Type . Sync , true , undefined ] ,
@@ -247,5 +247,48 @@ describe('handleSentry', () => {
247
247
) ;
248
248
}
249
249
} ) ;
250
+
251
+ it ( 'calls `transformPageChunk`' , async ( ) => {
252
+ const mockResolve = vi . fn ( ) . mockImplementation ( resolve ( type , isError ) ) ;
253
+ const event = mockEvent ( ) ;
254
+ try {
255
+ await sentryHandle ( { event, resolve : mockResolve } ) ;
256
+ } catch ( e ) {
257
+ expect ( e ) . toBeInstanceOf ( Error ) ;
258
+ expect ( e . message ) . toEqual ( type ) ;
259
+ }
260
+
261
+ expect ( mockResolve ) . toHaveBeenCalledTimes ( 1 ) ;
262
+ expect ( mockResolve ) . toHaveBeenCalledWith ( event , { transformPageChunk : expect . any ( Function ) } ) ;
263
+ } ) ;
264
+ } ) ;
265
+ } ) ;
266
+
267
+ describe ( 'transformPageChunk' , ( ) => {
268
+ const html = `<!DOCTYPE html>
269
+ <html lang="en">
270
+ <head>
271
+ <meta charset="utf-8" />
272
+ <link rel="icon" href="%sveltekit.assets%/favicon.png" />
273
+ <meta name="viewport" content="width=device-width" />
274
+ %sveltekit.head%
275
+ </head>
276
+ <body data-sveltekit-preload-data="hover">
277
+ <div style="display: contents">%sveltekit.body%</div>
278
+ </body>
279
+ </html>` ;
280
+
281
+ it ( 'does not add meta tags if no active transaction' , ( ) => {
282
+ const transformed = transformPageChunk ( { html, done : true } ) ;
283
+ expect ( transformed ) . toEqual ( html ) ;
284
+ } ) ;
285
+
286
+ it ( 'adds meta tags if there is an active transaction' , ( ) => {
287
+ const transaction = hub . startTransaction ( { name : 'test' } ) ;
288
+ hub . getScope ( ) . setSpan ( transaction ) ;
289
+ const transformed = transformPageChunk ( { html, done : true } ) as string ;
290
+
291
+ expect ( transformed . includes ( '<meta name="sentry-trace"' ) ) . toEqual ( true ) ;
292
+ expect ( transformed . includes ( '<meta name="baggage"' ) ) . toEqual ( true ) ;
250
293
} ) ;
251
294
} ) ;
0 commit comments