@@ -41,7 +41,12 @@ describe('makeAutoInstrumentationPlugin()', () => {
4141 } ) ;
4242
4343 it ( 'returns the auto instrumentation plugin' , async ( ) => {
44- const plugin = makeAutoInstrumentationPlugin ( { debug : true , load : true , serverLoad : true } ) ;
44+ const plugin = makeAutoInstrumentationPlugin ( {
45+ debug : true ,
46+ load : true ,
47+ serverLoad : true ,
48+ onlyInstrumentClient : false ,
49+ } ) ;
4550 expect ( plugin . name ) . toEqual ( 'sentry-auto-instrumentation' ) ;
4651 expect ( plugin . enforce ) . toEqual ( 'pre' ) ;
4752 expect ( plugin . load ) . toEqual ( expect . any ( Function ) ) ;
@@ -58,7 +63,12 @@ describe('makeAutoInstrumentationPlugin()', () => {
5863 'path/to/+layout.mjs' ,
5964 ] ) ( 'transform %s files' , ( path : string ) => {
6065 it ( 'wraps universal load if `load` option is `true`' , async ( ) => {
61- const plugin = makeAutoInstrumentationPlugin ( { debug : false , load : true , serverLoad : true } ) ;
66+ const plugin = makeAutoInstrumentationPlugin ( {
67+ debug : false ,
68+ load : true ,
69+ serverLoad : true ,
70+ onlyInstrumentClient : false ,
71+ } ) ;
6272 // @ts -expect-error this exists
6373 const loadResult = await plugin . load ( path ) ;
6474 expect ( loadResult ) . toEqual (
@@ -74,6 +84,7 @@ describe('makeAutoInstrumentationPlugin()', () => {
7484 debug : false ,
7585 load : false ,
7686 serverLoad : false ,
87+ onlyInstrumentClient : false ,
7788 } ) ;
7889 // @ts -expect-error this exists
7990 const loadResult = await plugin . load ( path ) ;
@@ -92,7 +103,12 @@ describe('makeAutoInstrumentationPlugin()', () => {
92103 'path/to/+layout.server.mjs' ,
93104 ] ) ( 'transform %s files' , ( path : string ) => {
94105 it ( 'wraps universal load if `load` option is `true`' , async ( ) => {
95- const plugin = makeAutoInstrumentationPlugin ( { debug : false , load : false , serverLoad : true } ) ;
106+ const plugin = makeAutoInstrumentationPlugin ( {
107+ debug : false ,
108+ load : false ,
109+ serverLoad : true ,
110+ onlyInstrumentClient : false ,
111+ } ) ;
96112 // @ts -expect-error this exists
97113 const loadResult = await plugin . load ( path ) ;
98114 expect ( loadResult ) . toEqual (
@@ -108,12 +124,101 @@ describe('makeAutoInstrumentationPlugin()', () => {
108124 debug : false ,
109125 load : false ,
110126 serverLoad : false ,
127+ onlyInstrumentClient : false ,
111128 } ) ;
112129 // @ts -expect-error this exists
113130 const loadResult = await plugin . load ( path ) ;
114131 expect ( loadResult ) . toEqual ( null ) ;
115132 } ) ;
116133 } ) ;
134+
135+ describe ( 'when `onlyInstrumentClient` is `true`' , ( ) => {
136+ it . each ( [
137+ // server-only files
138+ 'path/to/+page.server.ts' ,
139+ 'path/to/+layout.server.js' ,
140+ // universal files
141+ 'path/to/+page.mts' ,
142+ 'path/to/+layout.mjs' ,
143+ ] ) ( "doesn't wrap code in SSR build in %s" , async ( path : string ) => {
144+ const plugin = makeAutoInstrumentationPlugin ( {
145+ debug : false ,
146+ load : true ,
147+ serverLoad : true ,
148+ onlyInstrumentClient : true ,
149+ } ) ;
150+
151+ // @ts -expect-error this exists and is callable
152+ plugin . configResolved ( {
153+ build : {
154+ ssr : true ,
155+ } ,
156+ } ) ;
157+
158+ // @ts -expect-error this exists
159+ const loadResult = await plugin . load ( path ) ;
160+
161+ expect ( loadResult ) . toEqual ( null ) ;
162+ } ) ;
163+
164+ it . each ( [ 'path/to/+page.ts' , 'path/to/+layout.js' ] ) (
165+ 'wraps client-side code in universal files in %s' ,
166+ async ( path : string ) => {
167+ const plugin = makeAutoInstrumentationPlugin ( {
168+ debug : false ,
169+ load : true ,
170+ serverLoad : true ,
171+ onlyInstrumentClient : true ,
172+ } ) ;
173+
174+ // @ts -expect-error this exists and is callable
175+ plugin . configResolved ( {
176+ build : {
177+ ssr : false ,
178+ } ,
179+ } ) ;
180+
181+ // @ts -expect-error this exists and is callable
182+ const loadResult = await plugin . load ( path ) ;
183+
184+ expect ( loadResult ) . toBe (
185+ 'import { wrapLoadWithSentry } from "@sentry/sveltekit";' +
186+ `import * as userModule from "${ path } ?sentry-auto-wrap";` +
187+ 'export const load = userModule.load ? wrapLoadWithSentry(userModule.load) : undefined;' +
188+ `export * from "${ path } ?sentry-auto-wrap";` ,
189+ ) ;
190+ } ,
191+ ) ;
192+
193+ /**
194+ * This is a bit of a constructed case because in a client build, server-only files
195+ * shouldn't even be passed into the load hook. But just to be extra careful, let's
196+ * make sure we don't wrap server-only files in a client build.
197+ */
198+ it . each ( [ 'path/to/+page.server.ts' , 'path/to/+layout.server.js' ] ) (
199+ "doesn't wrap client-side code in server-only files in %s" ,
200+ async ( path : string ) => {
201+ const plugin = makeAutoInstrumentationPlugin ( {
202+ debug : false ,
203+ load : true ,
204+ serverLoad : true ,
205+ onlyInstrumentClient : true ,
206+ } ) ;
207+
208+ // @ts -expect-error this exists and is callable
209+ plugin . configResolved ( {
210+ build : {
211+ ssr : false ,
212+ } ,
213+ } ) ;
214+
215+ // @ts -expect-error this exists and is callable
216+ const loadResult = await plugin . load ( path ) ;
217+
218+ expect ( loadResult ) . toBe ( null ) ;
219+ } ,
220+ ) ;
221+ } ) ;
117222} ) ;
118223
119224describe ( 'canWrapLoad' , ( ) => {
0 commit comments