@@ -354,6 +354,66 @@ describe('sentryCloudflareAutoInstrumentPlugin', () => {
354354 } ) ;
355355} ) ;
356356
357+ describe ( 'wranglerConfigPath option' , ( ) => {
358+ it ( 'reads a custom-named wrangler config (e.g. wrangler.agent.jsonc)' , async ( ) => {
359+ const dir = writeTempDir ( { 'wrangler.agent.jsonc' : '{ "main": "src/agent.ts" }' } ) ;
360+ const plugin = sentryCloudflareAutoInstrumentPlugin ( { wranglerConfigPath : './wrangler.agent.jsonc' } ) ;
361+ plugin . configResolved ( { root : dir } ) ;
362+
363+ const code = 'export default { fetch() { return new Response("ok"); } };' ;
364+ const result = await plugin . transform . call ( { parse : ( c : string ) => parseJS ( c ) } , code , join ( dir , 'src/agent.ts' ) ) ;
365+
366+ expect ( result ) . toBeDefined ( ) ;
367+ expect ( result . code ) . toBe (
368+ [
369+ "import * as __SENTRY__ from '@sentry/cloudflare';" ,
370+ 'const __SENTRY_DEFAULT_EXPORT__ = { fetch() { return new Response("ok"); } };' ,
371+ 'export default __SENTRY__.withSentry(() => undefined, __SENTRY_DEFAULT_EXPORT__);' ,
372+ '' ,
373+ ] . join ( '\n' ) ,
374+ ) ;
375+ } ) ;
376+
377+ it ( 'prefers the explicit path over default-name configs' , async ( ) => {
378+ const dir = writeTempDir ( {
379+ 'wrangler.toml' : 'main = "src/default.ts"' ,
380+ 'wrangler.agent.jsonc' : '{ "main": "src/agent.ts" }' ,
381+ } ) ;
382+ const plugin = sentryCloudflareAutoInstrumentPlugin ( { wranglerConfigPath : 'wrangler.agent.jsonc' } ) ;
383+ plugin . configResolved ( { root : dir } ) ;
384+
385+ const code = 'export default { fetch() { return new Response("ok"); } };' ;
386+ const tx = ( id : string ) => plugin . transform . call ( { parse : ( c : string ) => parseJS ( c ) } , code , id ) ;
387+
388+ // The probed default config's entry must not be treated as the worker entry…
389+ expect ( await tx ( join ( dir , 'src/default.ts' ) ) ) . toBeUndefined ( ) ;
390+ // …while the explicit config's entry is.
391+ expect ( await tx ( join ( dir , 'src/agent.ts' ) ) ) . toBeDefined ( ) ;
392+ } ) ;
393+
394+ it ( 'warns with only the basename when the explicit path cannot be read' , ( ) => {
395+ const dir = writeTempDir ( { } ) ;
396+ const warnings : string [ ] = [ ] ;
397+ const plugin = sentryCloudflareAutoInstrumentPlugin ( { wranglerConfigPath : 'nested/dir/wrangler.agent.jsonc' } ) ;
398+ plugin . configResolved ( { root : dir , logger : { warn : msg => warnings . push ( msg ) } } ) ;
399+
400+ expect ( warnings ) . toHaveLength ( 1 ) ;
401+ expect ( warnings [ 0 ] ) . toContain ( 'wrangler.agent.jsonc' ) ;
402+ // The full path may leak a location the user doesn't want in build logs.
403+ expect ( warnings [ 0 ] ) . not . toContain ( 'nested/dir' ) ;
404+ } ) ;
405+
406+ it ( 'hints at the option when no default-named config is found' , ( ) => {
407+ const dir = writeTempDir ( { } ) ;
408+ const warnings : string [ ] = [ ] ;
409+ const plugin = sentryCloudflareAutoInstrumentPlugin ( ) ;
410+ plugin . configResolved ( { root : dir , logger : { warn : msg => warnings . push ( msg ) } } ) ;
411+
412+ expect ( warnings ) . toHaveLength ( 1 ) ;
413+ expect ( warnings [ 0 ] ) . toContain ( '`wranglerConfigPath`' ) ;
414+ } ) ;
415+ } ) ;
416+
357417// ---------------------------------------------------------------------------
358418// instrument.server.* auto-detection (config from a conventional module)
359419// ---------------------------------------------------------------------------
0 commit comments