@@ -4,7 +4,7 @@ import type { Transaction } from '@sentry/types';
44import type { Handle } from '@sveltejs/kit' ;
55import { vi } from 'vitest' ;
66
7- import { sentryHandle } from '../../src/server/handle' ;
7+ import { sentryHandle , transformPageChunk } from '../../src/server/handle' ;
88import { getDefaultNodeClientOptions } from '../utils' ;
99
1010const mockCaptureException = vi . fn ( ) ;
@@ -94,22 +94,22 @@ function resolve(type: Type, isError: boolean): Parameters<Handle>[0]['resolve']
9494let hub : Hub ;
9595let client : NodeClient ;
9696
97- describe ( 'handleSentry' , ( ) => {
98- beforeAll ( ( ) => {
99- addTracingExtensions ( ) ;
100- } ) ;
97+ beforeAll ( ( ) => {
98+ addTracingExtensions ( ) ;
99+ } ) ;
101100
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 ) ;
108107
109- mockCaptureException . mockClear ( ) ;
110- mockAddExceptionMechanism . mockClear ( ) ;
111- } ) ;
108+ mockCaptureException . mockClear ( ) ;
109+ mockAddExceptionMechanism . mockClear ( ) ;
110+ } ) ;
112111
112+ describe ( 'handleSentry' , ( ) => {
113113 describe . each ( [
114114 // isSync, isError, expectedResponse
115115 [ Type . Sync , true , undefined ] ,
@@ -247,5 +247,48 @@ describe('handleSentry', () => {
247247 ) ;
248248 }
249249 } ) ;
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 ) ;
250293 } ) ;
251294} ) ;
0 commit comments