@@ -41,7 +41,12 @@ describe('makeAutoInstrumentationPlugin()', () => {
41
41
} ) ;
42
42
43
43
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
+ } ) ;
45
50
expect ( plugin . name ) . toEqual ( 'sentry-auto-instrumentation' ) ;
46
51
expect ( plugin . enforce ) . toEqual ( 'pre' ) ;
47
52
expect ( plugin . load ) . toEqual ( expect . any ( Function ) ) ;
@@ -58,7 +63,12 @@ describe('makeAutoInstrumentationPlugin()', () => {
58
63
'path/to/+layout.mjs' ,
59
64
] ) ( 'transform %s files' , ( path : string ) => {
60
65
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
+ } ) ;
62
72
// @ts -expect-error this exists
63
73
const loadResult = await plugin . load ( path ) ;
64
74
expect ( loadResult ) . toEqual (
@@ -74,6 +84,7 @@ describe('makeAutoInstrumentationPlugin()', () => {
74
84
debug : false ,
75
85
load : false ,
76
86
serverLoad : false ,
87
+ onlyInstrumentClient : false ,
77
88
} ) ;
78
89
// @ts -expect-error this exists
79
90
const loadResult = await plugin . load ( path ) ;
@@ -92,7 +103,12 @@ describe('makeAutoInstrumentationPlugin()', () => {
92
103
'path/to/+layout.server.mjs' ,
93
104
] ) ( 'transform %s files' , ( path : string ) => {
94
105
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
+ } ) ;
96
112
// @ts -expect-error this exists
97
113
const loadResult = await plugin . load ( path ) ;
98
114
expect ( loadResult ) . toEqual (
@@ -108,12 +124,101 @@ describe('makeAutoInstrumentationPlugin()', () => {
108
124
debug : false ,
109
125
load : false ,
110
126
serverLoad : false ,
127
+ onlyInstrumentClient : false ,
111
128
} ) ;
112
129
// @ts -expect-error this exists
113
130
const loadResult = await plugin . load ( path ) ;
114
131
expect ( loadResult ) . toEqual ( null ) ;
115
132
} ) ;
116
133
} ) ;
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
+ } ) ;
117
222
} ) ;
118
223
119
224
describe ( 'canWrapLoad' , ( ) => {
0 commit comments