@@ -122,4 +122,88 @@ describe('sentryAstro integration', () => {
122122 expect ( injectScript ) . toHaveBeenCalledWith ( 'page' , expect . stringContaining ( 'my-client-init-path.js' ) ) ;
123123 expect ( injectScript ) . toHaveBeenCalledWith ( 'page-ssr' , expect . stringContaining ( 'my-server-init-path.js' ) ) ;
124124 } ) ;
125+
126+ it . each ( [ 'server' , 'hybrid' ] ) (
127+ 'adds middleware by default if in %s mode and `addMiddleware` is available' ,
128+ async mode => {
129+ const integration = sentryAstro ( { } ) ;
130+ const addMiddleware = vi . fn ( ) ;
131+ const updateConfig = vi . fn ( ) ;
132+ const injectScript = vi . fn ( ) ;
133+
134+ expect ( integration . hooks [ 'astro:config:setup' ] ) . toBeDefined ( ) ;
135+ // @ts -expect-error - the hook exists and we only need to pass what we actually use
136+ await integration . hooks [ 'astro:config:setup' ] ( {
137+ // @ts -expect-error - we only need to pass what we actually use
138+ config : { output : mode } ,
139+ addMiddleware,
140+ updateConfig,
141+ injectScript,
142+ } ) ;
143+
144+ expect ( addMiddleware ) . toHaveBeenCalledTimes ( 1 ) ;
145+ expect ( addMiddleware ) . toHaveBeenCalledWith ( {
146+ order : 'pre' ,
147+ entrypoint : '@sentry/astro/middleware' ,
148+ } ) ;
149+ } ,
150+ ) ;
151+
152+ it . each ( [ { output : 'static' } , { output : undefined } ] ) (
153+ "doesn't add middleware if in static mode (config %s)" ,
154+ async config => {
155+ const integration = sentryAstro ( { } ) ;
156+ const addMiddleware = vi . fn ( ) ;
157+ const updateConfig = vi . fn ( ) ;
158+ const injectScript = vi . fn ( ) ;
159+
160+ expect ( integration . hooks [ 'astro:config:setup' ] ) . toBeDefined ( ) ;
161+ // @ts -expect-error - the hook exists and we only need to pass what we actually use
162+ await integration . hooks [ 'astro:config:setup' ] ( {
163+ config,
164+ addMiddleware,
165+ updateConfig,
166+ injectScript,
167+ } ) ;
168+
169+ expect ( addMiddleware ) . toHaveBeenCalledTimes ( 0 ) ;
170+ } ,
171+ ) ;
172+
173+ it ( "doesn't add middleware if disabled by users" , async ( ) => {
174+ const integration = sentryAstro ( { autoInstrumentation : { requestHandler : false } } ) ;
175+ const addMiddleware = vi . fn ( ) ;
176+ const updateConfig = vi . fn ( ) ;
177+ const injectScript = vi . fn ( ) ;
178+
179+ expect ( integration . hooks [ 'astro:config:setup' ] ) . toBeDefined ( ) ;
180+ // @ts -expect-error - the hook exists and we only need to pass what we actually use
181+ await integration . hooks [ 'astro:config:setup' ] ( {
182+ // @ts -expect-error - we only need to pass what we actually use
183+ config : { output : 'server' } ,
184+ addMiddleware,
185+ updateConfig,
186+ injectScript,
187+ } ) ;
188+
189+ expect ( addMiddleware ) . toHaveBeenCalledTimes ( 0 ) ;
190+ } ) ;
191+
192+ it ( "doesn't add middleware (i.e. crash) if `addMiddleware` is N/A" , async ( ) => {
193+ const integration = sentryAstro ( { autoInstrumentation : { requestHandler : false } } ) ;
194+ const updateConfig = vi . fn ( ) ;
195+ const injectScript = vi . fn ( ) ;
196+
197+ expect ( integration . hooks [ 'astro:config:setup' ] ) . toBeDefined ( ) ;
198+ // @ts -expect-error - the hook exists and we only need to pass what we actually use
199+ await integration . hooks [ 'astro:config:setup' ] ( {
200+ // @ts -expect-error - we only need to pass what we actually use
201+ config : { output : 'server' } ,
202+ updateConfig,
203+ injectScript,
204+ } ) ;
205+
206+ expect ( updateConfig ) . toHaveBeenCalledTimes ( 1 ) ;
207+ expect ( injectScript ) . toHaveBeenCalledTimes ( 2 ) ;
208+ } ) ;
125209} ) ;
0 commit comments