1
1
import { vi } from 'vitest' ;
2
2
3
- import { makeCustomSentryVitePlugin } from '../../src/vite/sourceMaps' ;
3
+ import type { Plugin } from 'vite' ;
4
+ import { makeCustomSentryVitePlugins } from '../../src/vite/sourceMaps' ;
4
5
5
6
const mockedSentryVitePlugin = {
6
- buildStart : vi . fn ( ) ,
7
- resolveId : vi . fn ( ) ,
8
- renderChunk : vi . fn ( ) ,
9
- transform : vi . fn ( ) . mockImplementation ( ( code : string , _id : string ) => code ) ,
7
+ name : 'sentry-vite-debug-id-upload-plugin' ,
10
8
writeBundle : vi . fn ( ) ,
11
9
} ;
12
10
@@ -15,34 +13,45 @@ vi.mock('@sentry/vite-plugin', async () => {
15
13
16
14
return {
17
15
...original ,
18
- sentryVitePlugin : ( ) => mockedSentryVitePlugin ,
16
+ sentryVitePlugin : ( ) => [ mockedSentryVitePlugin ] ,
19
17
} ;
20
18
} ) ;
21
19
22
20
beforeEach ( ( ) => {
23
21
vi . clearAllMocks ( ) ;
24
22
} ) ;
25
23
24
+ async function getCustomSentryViteUploadSourcemapsPlugin ( ) : Promise < Plugin | undefined > {
25
+ const plugins = await makeCustomSentryVitePlugins ( {
26
+ authToken : 'token' ,
27
+ org : 'org' ,
28
+ project : 'project' ,
29
+ adapter : 'other' ,
30
+ } ) ;
31
+ return plugins . find ( plugin => plugin . name === 'sentry-upload-sveltekit-source-maps' ) ;
32
+ }
33
+
26
34
describe ( 'makeCustomSentryVitePlugin()' , ( ) => {
27
35
it ( 'returns the custom sentry source maps plugin' , async ( ) => {
28
- const plugin = await makeCustomSentryVitePlugin ( ) ;
29
- expect ( plugin . name ) . toEqual ( 'sentry-upload-source-maps' ) ;
30
- expect ( plugin . apply ) . toEqual ( 'build' ) ;
31
- expect ( plugin . enforce ) . toEqual ( 'post' ) ;
32
-
33
- expect ( plugin . buildStart ) . toBeInstanceOf ( Function ) ;
34
- expect ( plugin . resolveId ) . toBeInstanceOf ( Function ) ;
35
- expect ( plugin . renderChunk ) . toBeInstanceOf ( Function ) ;
36
- expect ( plugin . transform ) . toBeInstanceOf ( Function ) ;
37
-
38
- expect ( plugin . config ) . toBeInstanceOf ( Function ) ;
39
- expect ( plugin . configResolved ) . toBeInstanceOf ( Function ) ;
40
- expect ( plugin . closeBundle ) . toBeInstanceOf ( Function ) ;
36
+ const plugin = await getCustomSentryViteUploadSourcemapsPlugin ( ) ;
37
+ expect ( plugin ?. name ) . toEqual ( 'sentry-upload-sveltekit-source-maps' ) ;
38
+ expect ( plugin ?. apply ) . toEqual ( 'build' ) ;
39
+ expect ( plugin ?. enforce ) . toEqual ( 'post' ) ;
40
+
41
+ expect ( plugin ?. resolveId ) . toBeInstanceOf ( Function ) ;
42
+ expect ( plugin ?. transform ) . toBeInstanceOf ( Function ) ;
43
+
44
+ expect ( plugin ?. config ) . toBeInstanceOf ( Function ) ;
45
+ expect ( plugin ?. configResolved ) . toBeInstanceOf ( Function ) ;
46
+
47
+ // instead of writeBundle, this plugin uses closeBundle
48
+ expect ( plugin ?. closeBundle ) . toBeInstanceOf ( Function ) ;
49
+ expect ( plugin ?. writeBundle ) . toBeUndefined ( ) ;
41
50
} ) ;
42
51
43
52
describe ( 'Custom sentry vite plugin' , ( ) => {
44
53
it ( 'enables source map generation' , async ( ) => {
45
- const plugin = await makeCustomSentryVitePlugin ( ) ;
54
+ const plugin = await getCustomSentryViteUploadSourcemapsPlugin ( ) ;
46
55
// @ts -expect-error this function exists!
47
56
const sentrifiedConfig = plugin . config ( { build : { foo : { } } , test : { } } ) ;
48
57
expect ( sentrifiedConfig ) . toEqual ( {
@@ -55,16 +64,18 @@ describe('makeCustomSentryVitePlugin()', () => {
55
64
} ) ;
56
65
57
66
it ( 'injects the output dir into the server hooks file' , async ( ) => {
58
- const plugin = await makeCustomSentryVitePlugin ( ) ;
67
+ const plugin = await getCustomSentryViteUploadSourcemapsPlugin ( ) ;
59
68
// @ts -expect-error this function exists!
60
- const transformedCode = await plugin . transform ( 'foo' , '/src/hooks.server.ts' ) ;
61
- const expectedtransformedCode = 'foo\n; import "\0sentry-inject-global-values-file";\n' ;
62
- expect ( mockedSentryVitePlugin . transform ) . toHaveBeenCalledWith ( expectedtransformedCode , '/src/hooks.server.ts' ) ;
63
- expect ( transformedCode ) . toEqual ( expectedtransformedCode ) ;
69
+ const transformOutput = await plugin . transform ( 'foo' , '/src/hooks.server.ts' ) ;
70
+ const transformedCode = transformOutput . code ;
71
+ const transformedSourcemap = transformOutput . map ;
72
+ const expectedTransformedCode = 'foo\n; import "\0sentry-inject-global-values-file";\n' ;
73
+ expect ( transformedCode ) . toEqual ( expectedTransformedCode ) ;
74
+ expect ( transformedSourcemap ) . toBeDefined ( ) ;
64
75
} ) ;
65
76
66
77
it ( 'uploads source maps during the SSR build' , async ( ) => {
67
- const plugin = await makeCustomSentryVitePlugin ( ) ;
78
+ const plugin = await getCustomSentryViteUploadSourcemapsPlugin ( ) ;
68
79
// @ts -expect-error this function exists!
69
80
plugin . configResolved ( { build : { ssr : true } } ) ;
70
81
// @ts -expect-error this function exists!
@@ -73,7 +84,7 @@ describe('makeCustomSentryVitePlugin()', () => {
73
84
} ) ;
74
85
75
86
it ( "doesn't upload source maps during the non-SSR builds" , async ( ) => {
76
- const plugin = await makeCustomSentryVitePlugin ( ) ;
87
+ const plugin = await getCustomSentryViteUploadSourcemapsPlugin ( ) ;
77
88
78
89
// @ts -expect-error this function exists!
79
90
plugin . configResolved ( { build : { ssr : false } } ) ;
@@ -91,7 +102,7 @@ describe('makeCustomSentryVitePlugin()', () => {
91
102
const consoleWarnSpy = vi . spyOn ( console , 'warn' ) . mockImplementation ( ( ) => { } ) ;
92
103
const consoleLogSpy = vi . spyOn ( console , 'log' ) . mockImplementation ( ( ) => { } ) ;
93
104
94
- const plugin = await makeCustomSentryVitePlugin ( ) ;
105
+ const plugin = await getCustomSentryViteUploadSourcemapsPlugin ( ) ;
95
106
96
107
// @ts -expect-error this function exists!
97
108
expect ( plugin . closeBundle ) . not . toThrow ( ) ;
0 commit comments