@@ -3,17 +3,31 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
3
3
import type { Plugin } from 'vite' ;
4
4
import { makeCustomSentryVitePlugins } from '../../src/vite/sourceMaps' ;
5
5
6
- const mockedSentryVitePlugin = {
6
+ const mockedViteDebugIdUploadPlugin = {
7
7
name : 'sentry-vite-debug-id-upload-plugin' ,
8
8
writeBundle : vi . fn ( ) ,
9
9
} ;
10
10
11
+ const mockedViteReleaseManagementPlugin = {
12
+ name : 'sentry-release-management-plugin' ,
13
+ writeBundle : vi . fn ( ) ,
14
+ } ;
15
+
16
+ const mockedFileDeletionPlugin = {
17
+ name : 'sentry-file-deletion-plugin' ,
18
+ writeBundle : vi . fn ( ) ,
19
+ } ;
20
+
11
21
vi . mock ( '@sentry/vite-plugin' , async ( ) => {
12
22
const original = ( await vi . importActual ( '@sentry/vite-plugin' ) ) as any ;
13
23
14
24
return {
15
25
...original ,
16
- sentryVitePlugin : ( ) => [ mockedSentryVitePlugin ] ,
26
+ sentryVitePlugin : ( ) => [
27
+ mockedViteReleaseManagementPlugin ,
28
+ mockedViteDebugIdUploadPlugin ,
29
+ mockedFileDeletionPlugin ,
30
+ ] ,
17
31
} ;
18
32
} ) ;
19
33
@@ -30,20 +44,22 @@ beforeEach(() => {
30
44
vi . clearAllMocks ( ) ;
31
45
} ) ;
32
46
33
- async function getCustomSentryViteUploadSourcemapsPlugin ( ) : Promise < Plugin | undefined > {
47
+ async function getSentryViteSubPlugin ( name : string ) : Promise < Plugin | undefined > {
34
48
const plugins = await makeCustomSentryVitePlugins ( {
35
49
authToken : 'token' ,
36
50
org : 'org' ,
37
51
project : 'project' ,
38
52
adapter : 'other' ,
39
53
} ) ;
40
- return plugins . find ( plugin => plugin . name === 'sentry-upload-sveltekit-source-maps' ) ;
54
+
55
+ return plugins . find ( plugin => plugin . name === name ) ;
41
56
}
42
57
43
58
describe ( 'makeCustomSentryVitePlugin()' , ( ) => {
44
59
it ( 'returns the custom sentry source maps plugin' , async ( ) => {
45
- const plugin = await getCustomSentryViteUploadSourcemapsPlugin ( ) ;
46
- expect ( plugin ?. name ) . toEqual ( 'sentry-upload-sveltekit-source-maps' ) ;
60
+ const plugin = await getSentryViteSubPlugin ( 'sentry-sveltekit-debug-id-upload-plugin' ) ;
61
+
62
+ expect ( plugin ?. name ) . toEqual ( 'sentry-sveltekit-debug-id-upload-plugin' ) ;
47
63
expect ( plugin ?. apply ) . toEqual ( 'build' ) ;
48
64
expect ( plugin ?. enforce ) . toEqual ( 'post' ) ;
49
65
@@ -58,9 +74,9 @@ describe('makeCustomSentryVitePlugin()', () => {
58
74
expect ( plugin ?. writeBundle ) . toBeUndefined ( ) ;
59
75
} ) ;
60
76
61
- describe ( 'Custom sentry vite plugin' , ( ) => {
77
+ describe ( 'Custom debug id source maps plugin plugin' , ( ) => {
62
78
it ( 'enables source map generation' , async ( ) => {
63
- const plugin = await getCustomSentryViteUploadSourcemapsPlugin ( ) ;
79
+ const plugin = await getSentryViteSubPlugin ( 'sentry-sveltekit-debug-id-upload-plugin' ) ;
64
80
// @ts -expect-error this function exists!
65
81
const sentrifiedConfig = plugin . config ( { build : { foo : { } } , test : { } } ) ;
66
82
expect ( sentrifiedConfig ) . toEqual ( {
@@ -73,7 +89,7 @@ describe('makeCustomSentryVitePlugin()', () => {
73
89
} ) ;
74
90
75
91
it ( 'injects the output dir into the server hooks file' , async ( ) => {
76
- const plugin = await getCustomSentryViteUploadSourcemapsPlugin ( ) ;
92
+ const plugin = await getSentryViteSubPlugin ( 'sentry-sveltekit-debug-id-upload-plugin' ) ;
77
93
// @ts -expect-error this function exists!
78
94
const transformOutput = await plugin . transform ( 'foo' , '/src/hooks.server.ts' ) ;
79
95
const transformedCode = transformOutput . code ;
@@ -84,34 +100,34 @@ describe('makeCustomSentryVitePlugin()', () => {
84
100
} ) ;
85
101
86
102
it ( 'uploads source maps during the SSR build' , async ( ) => {
87
- const plugin = await getCustomSentryViteUploadSourcemapsPlugin ( ) ;
103
+ const plugin = await getSentryViteSubPlugin ( 'sentry-sveltekit-debug-id-upload-plugin' ) ;
88
104
// @ts -expect-error this function exists!
89
105
plugin . configResolved ( { build : { ssr : true } } ) ;
90
106
// @ts -expect-error this function exists!
91
107
await plugin . closeBundle ( ) ;
92
- expect ( mockedSentryVitePlugin . writeBundle ) . toHaveBeenCalledTimes ( 1 ) ;
108
+ expect ( mockedViteDebugIdUploadPlugin . writeBundle ) . toHaveBeenCalledTimes ( 1 ) ;
93
109
} ) ;
94
110
95
111
it ( "doesn't upload source maps during the non-SSR builds" , async ( ) => {
96
- const plugin = await getCustomSentryViteUploadSourcemapsPlugin ( ) ;
112
+ const plugin = await getSentryViteSubPlugin ( 'sentry-sveltekit-debug-id-upload-plugin' ) ;
97
113
98
114
// @ts -expect-error this function exists!
99
115
plugin . configResolved ( { build : { ssr : false } } ) ;
100
116
// @ts -expect-error this function exists!
101
117
await plugin . closeBundle ( ) ;
102
- expect ( mockedSentryVitePlugin . writeBundle ) . not . toHaveBeenCalled ( ) ;
118
+ expect ( mockedViteDebugIdUploadPlugin . writeBundle ) . not . toHaveBeenCalled ( ) ;
103
119
} ) ;
104
120
} ) ;
105
121
106
122
it ( 'catches errors while uploading source maps' , async ( ) => {
107
- mockedSentryVitePlugin . writeBundle . mockImplementationOnce ( ( ) => {
123
+ mockedViteDebugIdUploadPlugin . writeBundle . mockImplementationOnce ( ( ) => {
108
124
throw new Error ( 'test error' ) ;
109
125
} ) ;
110
126
111
- const consoleWarnSpy = vi . spyOn ( console , 'warn' ) . mockImplementation ( ( ) => { } ) ;
112
- const consoleLogSpy = vi . spyOn ( console , 'log' ) . mockImplementation ( ( ) => { } ) ;
127
+ const consoleWarnSpy = vi . spyOn ( console , 'warn' ) . mockImplementationOnce ( ( ) => { } ) ;
128
+ const consoleLogSpy = vi . spyOn ( console , 'log' ) . mockImplementationOnce ( ( ) => { } ) ;
113
129
114
- const plugin = await getCustomSentryViteUploadSourcemapsPlugin ( ) ;
130
+ const plugin = await getSentryViteSubPlugin ( 'sentry-sveltekit-debug-id-upload-plugin' ) ;
115
131
116
132
// @ts -expect-error this function exists!
117
133
expect ( plugin . closeBundle ) . not . toThrow ( ) ;
@@ -124,4 +140,100 @@ describe('makeCustomSentryVitePlugin()', () => {
124
140
expect ( consoleWarnSpy ) . toHaveBeenCalledWith ( expect . stringContaining ( 'Failed to upload source maps' ) ) ;
125
141
expect ( consoleLogSpy ) . toHaveBeenCalled ( ) ;
126
142
} ) ;
143
+
144
+ describe ( 'Custom release management plugin' , ( ) => {
145
+ it ( 'has the expected hooks and properties' , async ( ) => {
146
+ const plugin = await getSentryViteSubPlugin ( 'sentry-sveltekit-release-management-plugin' ) ;
147
+
148
+ expect ( plugin ) . toEqual ( {
149
+ name : 'sentry-sveltekit-release-management-plugin' ,
150
+ apply : 'build' ,
151
+ enforce : 'post' ,
152
+ closeBundle : expect . any ( Function ) ,
153
+ } ) ;
154
+ } ) ;
155
+
156
+ it ( 'calls the original release management plugin to start the release creation pipeline' , async ( ) => {
157
+ const plugin = await getSentryViteSubPlugin ( 'sentry-sveltekit-release-management-plugin' ) ;
158
+ // @ts -expect-error this function exists!
159
+ await plugin . closeBundle ( ) ;
160
+ expect ( mockedViteReleaseManagementPlugin . writeBundle ) . toHaveBeenCalledTimes ( 1 ) ;
161
+ } ) ;
162
+
163
+ it ( 'catches errors during release creation' , async ( ) => {
164
+ mockedViteReleaseManagementPlugin . writeBundle . mockImplementationOnce ( ( ) => {
165
+ throw new Error ( 'test error' ) ;
166
+ } ) ;
167
+
168
+ const consoleWarnSpy = vi . spyOn ( console , 'warn' ) . mockImplementationOnce ( ( ) => { } ) ;
169
+
170
+ const plugin = await getSentryViteSubPlugin ( 'sentry-sveltekit-release-management-plugin' ) ;
171
+
172
+ // @ts -expect-error this function exists!
173
+ expect ( plugin . closeBundle ) . not . toThrow ( ) ;
174
+
175
+ // @ts -expect-error this function exists!
176
+ await plugin . closeBundle ( ) ;
177
+
178
+ expect ( consoleWarnSpy ) . toHaveBeenCalledWith (
179
+ expect . stringContaining ( 'Failed to upload release data' ) ,
180
+ expect . any ( Error ) ,
181
+ ) ;
182
+ } ) ;
183
+
184
+ it ( 'also works correctly if the original release management plugin has its old name' , async ( ) => {
185
+ const currentName = mockedViteReleaseManagementPlugin . name ;
186
+ mockedViteReleaseManagementPlugin . name = 'sentry-debug-id-upload-plugin' ;
187
+
188
+ const plugin = await getSentryViteSubPlugin ( 'sentry-sveltekit-release-management-plugin' ) ;
189
+
190
+ // @ts -expect-error this function exists!
191
+ await plugin . closeBundle ( ) ;
192
+
193
+ expect ( mockedViteReleaseManagementPlugin . writeBundle ) . toHaveBeenCalledTimes ( 1 ) ;
194
+
195
+ mockedViteReleaseManagementPlugin . name = currentName ;
196
+ } ) ;
197
+ } ) ;
198
+
199
+ describe ( 'Custom file deletion plugin' , ( ) => {
200
+ it ( 'has the expected hooks and properties' , async ( ) => {
201
+ const plugin = await getSentryViteSubPlugin ( 'sentry-sveltekit-file-deletion-plugin' ) ;
202
+
203
+ expect ( plugin ) . toEqual ( {
204
+ name : 'sentry-sveltekit-file-deletion-plugin' ,
205
+ apply : 'build' ,
206
+ enforce : 'post' ,
207
+ closeBundle : expect . any ( Function ) ,
208
+ } ) ;
209
+ } ) ;
210
+
211
+ it ( 'calls the original file deletion plugin to delete files' , async ( ) => {
212
+ const plugin = await getSentryViteSubPlugin ( 'sentry-sveltekit-file-deletion-plugin' ) ;
213
+ // @ts -expect-error this function exists!
214
+ await plugin . closeBundle ( ) ;
215
+ expect ( mockedFileDeletionPlugin . writeBundle ) . toHaveBeenCalledTimes ( 1 ) ;
216
+ } ) ;
217
+
218
+ it ( 'catches errors during file deletion' , async ( ) => {
219
+ mockedFileDeletionPlugin . writeBundle . mockImplementationOnce ( ( ) => {
220
+ throw new Error ( 'test error' ) ;
221
+ } ) ;
222
+
223
+ const consoleWarnSpy = vi . spyOn ( console , 'warn' ) . mockImplementationOnce ( ( ) => { } ) ;
224
+
225
+ const plugin = await getSentryViteSubPlugin ( 'sentry-sveltekit-file-deletion-plugin' ) ;
226
+
227
+ // @ts -expect-error this function exists!
228
+ expect ( plugin . closeBundle ) . not . toThrow ( ) ;
229
+
230
+ // @ts -expect-error this function exists!
231
+ await plugin . closeBundle ( ) ;
232
+
233
+ expect ( consoleWarnSpy ) . toHaveBeenCalledWith (
234
+ expect . stringContaining ( 'Failed to delete source maps' ) ,
235
+ expect . any ( Error ) ,
236
+ ) ;
237
+ } ) ;
238
+ } ) ;
127
239
} ) ;
0 commit comments