@@ -2,6 +2,8 @@ import type { Plugin } from 'vite';
2
2
import * as vite from 'vite' ;
3
3
import { beforeEach , describe , expect , it , vi } from 'vitest' ;
4
4
import { _getUpdatedSourceMapSettings , makeCustomSentryVitePlugins } from '../../src/vite/sourceMaps' ;
5
+ import { ViteUserConfig } from 'vitest/config' ;
6
+ import { sentryVitePlugin } from '@sentry/vite-plugin' ;
5
7
6
8
const mockedViteDebugIdUploadPlugin = {
7
9
name : 'sentry-vite-debug-id-upload-plugin' ,
@@ -18,25 +20,21 @@ const mockedFileDeletionPlugin = {
18
20
writeBundle : vi . fn ( ) ,
19
21
} ;
20
22
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 ;
23
25
24
26
return {
25
27
...original ,
26
- loadConfigFromFile : vi . fn ( ) ,
28
+ sentryVitePlugin : vi . fn ( ) ,
27
29
} ;
28
30
} ) ;
29
31
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 ;
32
34
33
35
return {
34
36
...original ,
35
- sentryVitePlugin : ( ) => [
36
- mockedViteReleaseManagementPlugin ,
37
- mockedViteDebugIdUploadPlugin ,
38
- mockedFileDeletionPlugin ,
39
- ] ,
37
+ loadConfigFromFile : vi . fn ( ) ,
40
38
} ;
41
39
} ) ;
42
40
@@ -65,6 +63,15 @@ async function getSentryViteSubPlugin(name: string): Promise<Plugin | undefined>
65
63
}
66
64
67
65
describe ( 'makeCustomSentryVitePlugins()' , ( ) => {
66
+ beforeEach ( ( ) => {
67
+ // @ts -expect-error - this function exists!
68
+ sentryVitePlugin . mockReturnValue ( [
69
+ mockedViteReleaseManagementPlugin ,
70
+ mockedViteDebugIdUploadPlugin ,
71
+ mockedFileDeletionPlugin ,
72
+ ] ) ;
73
+ } ) ;
74
+
68
75
it ( 'returns the custom sentry source maps plugin' , async ( ) => {
69
76
const plugin = await getSentryViteSubPlugin ( 'sentry-sveltekit-debug-id-upload-plugin' ) ;
70
77
@@ -87,6 +94,7 @@ describe('makeCustomSentryVitePlugins()', () => {
87
94
// @ts -expect-error - this global variable is set/accessed in src/vite/sourceMaps.ts
88
95
globalThis . _sentry_sourceMapSetting = undefined ;
89
96
} ) ;
97
+
90
98
it ( 'returns the custom sentry source maps plugin' , async ( ) => {
91
99
const plugin = await getSentryViteSubPlugin ( 'sentry-sveltekit-update-source-map-setting-plugin' ) ;
92
100
@@ -117,6 +125,8 @@ describe('makeCustomSentryVitePlugins()', () => {
117
125
} ) ;
118
126
119
127
it ( 'keeps source map generation settings when previously disabled' , async ( ) => {
128
+ const consoleWarnSpy = vi . spyOn ( console , 'warn' ) . mockImplementationOnce ( ( ) => { } ) ;
129
+
120
130
const originalConfig = {
121
131
build : { sourcemap : false , assetsDir : 'assets' } ,
122
132
} ;
@@ -138,6 +148,10 @@ describe('makeCustomSentryVitePlugins()', () => {
138
148
sourcemap : false ,
139
149
} ,
140
150
} ) ;
151
+
152
+ expect ( consoleWarnSpy ) . toHaveBeenCalledWith (
153
+ '[Sentry] Source map generation is disabled in your Vite configuration.' ,
154
+ ) ;
141
155
} ) ;
142
156
143
157
it ( 'enables source map generation with "hidden" when unset' , async ( ) => {
@@ -201,8 +215,8 @@ describe('makeCustomSentryVitePlugins()', () => {
201
215
throw new Error ( 'test error' ) ;
202
216
} ) ;
203
217
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 ( ( ) => { } ) ;
206
220
207
221
const plugin = await getSentryViteSubPlugin ( 'sentry-sveltekit-debug-id-upload-plugin' ) ;
208
222
@@ -395,3 +409,147 @@ describe('_getUpdatedSourceMapSettings', () => {
395
409
} ) ;
396
410
} ) ;
397
411
} ) ;
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