@@ -2,6 +2,8 @@ import type { Plugin } from 'vite';
22import  *  as  vite  from  'vite' ; 
33import  {  beforeEach ,  describe ,  expect ,  it ,  vi  }  from  'vitest' ; 
44import  {  _getUpdatedSourceMapSettings ,  makeCustomSentryVitePlugins  }  from  '../../src/vite/sourceMaps' ; 
5+ import  {  ViteUserConfig  }  from  'vitest/config' ; 
6+ import  {  sentryVitePlugin  }  from  '@sentry/vite-plugin' ; 
57
68const  mockedViteDebugIdUploadPlugin  =  { 
79  name : 'sentry-vite-debug-id-upload-plugin' , 
@@ -18,25 +20,21 @@ const mockedFileDeletionPlugin = {
1820  writeBundle : vi . fn ( ) , 
1921} ; 
2022
21- vi . mock ( 'vite' ,  async  ( )  =>  { 
22-   const  original  =  ( await  vi . importActual ( 'vite' ) )  as  any ; 
23+ vi . mock ( '@sentry/ vite-plugin ' ,  async  ( )  =>  { 
24+   const  original  =  ( await  vi . importActual ( '@sentry/ vite-plugin ' ) )  as  any ; 
2325
2426  return  { 
2527    ...original , 
26-     loadConfigFromFile : vi . fn ( ) , 
28+     sentryVitePlugin : vi . fn ( ) , 
2729  } ; 
2830} ) ; 
2931
30- vi . mock ( '@sentry/ vite-plugin ' ,  async  ( )  =>  { 
31-   const  original  =  ( await  vi . importActual ( '@sentry/ vite-plugin ' ) )  as  any ; 
32+ vi . mock ( 'vite' ,  async  ( )  =>  { 
33+   const  original  =  ( await  vi . importActual ( 'vite' ) )  as  any ; 
3234
3335  return  { 
3436    ...original , 
35-     sentryVitePlugin : ( )  =>  [ 
36-       mockedViteReleaseManagementPlugin , 
37-       mockedViteDebugIdUploadPlugin , 
38-       mockedFileDeletionPlugin , 
39-     ] , 
37+     loadConfigFromFile : vi . fn ( ) , 
4038  } ; 
4139} ) ; 
4240
@@ -65,6 +63,15 @@ async function getSentryViteSubPlugin(name: string): Promise<Plugin | undefined>
6563} 
6664
6765describe ( 'makeCustomSentryVitePlugins()' ,  ( )  =>  { 
66+   beforeEach ( ( )  =>  { 
67+     // @ts -expect-error - this function exists! 
68+     sentryVitePlugin . mockReturnValue ( [ 
69+       mockedViteReleaseManagementPlugin , 
70+       mockedViteDebugIdUploadPlugin , 
71+       mockedFileDeletionPlugin , 
72+     ] ) ; 
73+   } ) ; 
74+ 
6875  it ( 'returns the custom sentry source maps plugin' ,  async  ( )  =>  { 
6976    const  plugin  =  await  getSentryViteSubPlugin ( 'sentry-sveltekit-debug-id-upload-plugin' ) ; 
7077
@@ -87,6 +94,7 @@ describe('makeCustomSentryVitePlugins()', () => {
8794      // @ts -expect-error - this global variable is set/accessed in src/vite/sourceMaps.ts 
8895      globalThis . _sentry_sourceMapSetting  =  undefined ; 
8996    } ) ; 
97+ 
9098    it ( 'returns the custom sentry source maps plugin' ,  async  ( )  =>  { 
9199      const  plugin  =  await  getSentryViteSubPlugin ( 'sentry-sveltekit-update-source-map-setting-plugin' ) ; 
92100
@@ -117,6 +125,8 @@ describe('makeCustomSentryVitePlugins()', () => {
117125    } ) ; 
118126
119127    it ( 'keeps source map generation settings when previously disabled' ,  async  ( )  =>  { 
128+       const  consoleWarnSpy  =  vi . spyOn ( console ,  'warn' ) . mockImplementationOnce ( ( )  =>  { } ) ; 
129+ 
120130      const  originalConfig  =  { 
121131        build : {  sourcemap : false ,  assetsDir : 'assets'  } , 
122132      } ; 
@@ -138,6 +148,10 @@ describe('makeCustomSentryVitePlugins()', () => {
138148          sourcemap : false , 
139149        } , 
140150      } ) ; 
151+ 
152+       expect ( consoleWarnSpy ) . toHaveBeenCalledWith ( 
153+         '[Sentry] Source map generation is disabled in your Vite configuration.' , 
154+       ) ; 
141155    } ) ; 
142156
143157    it ( 'enables source map generation with "hidden" when unset' ,  async  ( )  =>  { 
@@ -201,8 +215,8 @@ describe('makeCustomSentryVitePlugins()', () => {
201215      throw  new  Error ( 'test error' ) ; 
202216    } ) ; 
203217
204-     const  consoleWarnSpy  =  vi . spyOn ( console ,  'warn' ) . mockImplementationOnce ( ( )  =>  { } ) ; 
205-     const  consoleLogSpy  =  vi . spyOn ( console ,  'log' ) . mockImplementationOnce ( ( )  =>  { } ) ; 
218+     const  consoleWarnSpy  =  vi . spyOn ( console ,  'warn' ) . mockImplementation ( ( )  =>  { } ) ; 
219+     const  consoleLogSpy  =  vi . spyOn ( console ,  'log' ) . mockImplementation ( ( )  =>  { } ) ; 
206220
207221    const  plugin  =  await  getSentryViteSubPlugin ( 'sentry-sveltekit-debug-id-upload-plugin' ) ; 
208222
@@ -395,3 +409,147 @@ describe('_getUpdatedSourceMapSettings', () => {
395409    } ) ; 
396410  } ) ; 
397411} ) ; 
412+ 
413+ describe ( 'deleteFilesAfterUpload' ,  ( )  =>  { 
414+   it ( 'works with defauts' ,  async  ( )  =>  { 
415+     const  viteConfig : ViteUserConfig  =  { } ; 
416+ 
417+     vi . mock ( '@sentry/vite-plugin' ,  async  ( )  =>  { 
418+       const  original  =  ( await  vi . importActual ( '@sentry/vite-plugin' ) )  as  any ; 
419+ 
420+       return  { 
421+         ...original , 
422+         sentryVitePlugin : vi . fn ( original . sentryVitePlugin ) , 
423+       } ; 
424+     } ) ; 
425+ 
426+     const  plugins  =  await  makeCustomSentryVitePlugins ( { 
427+       authToken : 'token' , 
428+       org : 'org' , 
429+       project : 'project' , 
430+       adapter : 'other' , 
431+     } ) ; 
432+ 
433+     // @ts -expect-error this function exists! 
434+     const  mergedOptions  =  sentryVitePlugin . mock . calls [ 0 ] [ 0 ] ; 
435+ 
436+     expect ( mergedOptions ) . toEqual ( { 
437+       _metaOptions : { 
438+         telemetry : { 
439+           metaFramework : 'sveltekit' , 
440+         } , 
441+       } , 
442+       authToken : 'token' , 
443+       org : 'org' , 
444+       project : 'project' , 
445+       adapter : 'other' , 
446+       release : { 
447+         name : expect . any ( String ) , 
448+       } , 
449+       sourcemaps : { 
450+         filesToDeleteAfterUpload : expect . any ( Promise ) , 
451+       } , 
452+     } ) ; 
453+ 
454+     const  sourceMapSettingPlugin  =  plugins . find ( 
455+       plugin  =>  plugin . name  ===  'sentry-sveltekit-update-source-map-setting-plugin' , 
456+     ) ! ; 
457+ 
458+     // @ts -expect-error this function exists! 
459+     const  sourceMapSettingConfig  =  await  sourceMapSettingPlugin . config ( viteConfig ) ; 
460+     expect ( sourceMapSettingConfig ) . toEqual ( {  build : {  sourcemap : 'hidden'  }  } ) ; 
461+ 
462+     const  filesToDeleteAfterUploadSettingPlugin  =  plugins . find ( 
463+       plugin  =>  plugin . name  ===  'sentry-sveltekit-files-to-delete-after-upload-setting-plugin' , 
464+     ) ! ; 
465+ 
466+     // call this to ensure the filesToDeleteAfterUpload setting is resolved 
467+     // @ts -expect-error this function exists! 
468+     await  filesToDeleteAfterUploadSettingPlugin . config ( viteConfig ) ; 
469+ 
470+     await  expect ( mergedOptions . sourcemaps . filesToDeleteAfterUpload ) . resolves . toEqual ( [ 
471+       './.*/**/*.map' , 
472+       './.svelte-kit/output/**/*.map' , 
473+     ] ) ; 
474+   } ) ; 
475+ 
476+   it . each ( [ 
477+     [ [ 'blub/' ] ,  undefined ,  'hidden' ,  [ 'blub/' ] ] , 
478+     [ [ 'blub/' ] ,  false ,  false ,  [ 'blub/' ] ] , 
479+     [ undefined ,  'hidden'  as  const ,  'hidden' ,  undefined ] , 
480+     [ undefined ,  false ,  false ,  undefined ] , 
481+     [ undefined ,  true ,  true ,  undefined ] , 
482+     [ [ '/blub/' ] ,  true ,  true ,  [ '/blub/' ] ] , 
483+   ] ) ( 
484+     'works with filesToDeleteAfterUpload: %j & sourcemap: %s' , 
485+     async  ( filesToDeleteAfterUpload ,  sourcemap ,  sourcemapExpected ,  filesToDeleteAfterUploadExpected )  =>  { 
486+       const  viteConfig : ViteUserConfig  =  { 
487+         build : { 
488+           sourcemap, 
489+         } , 
490+       } ; 
491+ 
492+       vi . mock ( '@sentry/vite-plugin' ,  async  ( )  =>  { 
493+         const  original  =  ( await  vi . importActual ( '@sentry/vite-plugin' ) )  as  any ; 
494+ 
495+         return  { 
496+           ...original , 
497+           sentryVitePlugin : vi . fn ( original . sentryVitePlugin ) , 
498+         } ; 
499+       } ) ; 
500+ 
501+       const  plugins  =  await  makeCustomSentryVitePlugins ( { 
502+         authToken : 'token' , 
503+         org : 'org' , 
504+         project : 'project' , 
505+         adapter : 'other' , 
506+         sourcemaps : { 
507+           filesToDeleteAfterUpload, 
508+         } , 
509+       } ) ; 
510+ 
511+       // @ts -expect-error this function exists! 
512+       const  mergedOptions  =  sentryVitePlugin . mock . calls [ 0 ] [ 0 ] ; 
513+ 
514+       expect ( mergedOptions ) . toEqual ( { 
515+         _metaOptions : { 
516+           telemetry : { 
517+             metaFramework : 'sveltekit' , 
518+           } , 
519+         } , 
520+         authToken : 'token' , 
521+         org : 'org' , 
522+         project : 'project' , 
523+         adapter : 'other' , 
524+         release : { 
525+           name : expect . any ( String ) , 
526+         } , 
527+         sourcemaps : { 
528+           filesToDeleteAfterUpload : expect . any ( Promise ) , 
529+         } , 
530+       } ) ; 
531+ 
532+       const  sourceMapSettingPlugin  =  plugins . find ( 
533+         plugin  =>  plugin . name  ===  'sentry-sveltekit-update-source-map-setting-plugin' , 
534+       ) ! ; 
535+ 
536+       // @ts -expect-error this function exists! 
537+       const  sourceMapSettingConfig  =  await  sourceMapSettingPlugin . config ( viteConfig ) ; 
538+       expect ( sourceMapSettingConfig ) . toEqual ( {  build : {  sourcemap : sourcemapExpected  }  } ) ; 
539+ 
540+       const  filesToDeleteAfterUploadSettingPlugin  =  plugins . find ( 
541+         plugin  =>  plugin . name  ===  'sentry-sveltekit-files-to-delete-after-upload-setting-plugin' , 
542+       ) ! ; 
543+ 
544+       console . log ( filesToDeleteAfterUploadSettingPlugin ) ; 
545+ 
546+       // call this to ensure the filesToDeleteAfterUpload setting is resolved 
547+       // @ts -expect-error this function exists! 
548+       await  filesToDeleteAfterUploadSettingPlugin . config ( viteConfig ) ; 
549+ 
550+       await  expect ( mergedOptions . sourcemaps . filesToDeleteAfterUpload ) . resolves . toEqual ( 
551+         filesToDeleteAfterUploadExpected , 
552+       ) ; 
553+     } , 
554+   ) ; 
555+ } ) ; 
0 commit comments