|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 2 | +import { sentryOnBuildEnd } from '../../../src/vite/buildEnd/handleOnBuildEnd'; |
| 3 | +import SentryCli from '@sentry/cli'; |
| 4 | +import * as fs from 'fs'; |
| 5 | +import glob from 'glob'; |
| 6 | +import type { ResolvedConfig } from 'vite'; |
| 7 | + |
| 8 | +// Mock dependencies |
| 9 | +vi.mock('@sentry/cli'); |
| 10 | +vi.mock('fs'); |
| 11 | +vi.mock('glob'); |
| 12 | + |
| 13 | +describe('sentryOnBuildEnd', () => { |
| 14 | + const mockSentryCliInstance = { |
| 15 | + releases: { |
| 16 | + new: vi.fn(), |
| 17 | + uploadSourceMaps: vi.fn(), |
| 18 | + }, |
| 19 | + }; |
| 20 | + |
| 21 | + const defaultConfig = { |
| 22 | + buildManifest: undefined, |
| 23 | + reactRouterConfig: { |
| 24 | + appDirectory: '/app', |
| 25 | + basename: '/', |
| 26 | + buildDirectory: '/build', |
| 27 | + future: { |
| 28 | + unstable_optimizeDeps: false, |
| 29 | + }, |
| 30 | + prerender: undefined, |
| 31 | + routes: {}, |
| 32 | + serverBuildFile: 'server.js', |
| 33 | + serverModuleFormat: 'esm' as const, |
| 34 | + ssr: true, |
| 35 | + }, |
| 36 | + viteConfig: { |
| 37 | + build: { |
| 38 | + sourcemap: true, |
| 39 | + }, |
| 40 | + } as ResolvedConfig, |
| 41 | + sentryConfig: { |
| 42 | + authToken: 'test-token', |
| 43 | + org: 'test-org', |
| 44 | + project: 'test-project', |
| 45 | + debug: false, |
| 46 | + }, |
| 47 | + }; |
| 48 | + |
| 49 | + beforeEach(() => { |
| 50 | + vi.clearAllMocks(); |
| 51 | + // @ts-expect-error - mocking constructor |
| 52 | + SentryCli.mockImplementation(() => mockSentryCliInstance); |
| 53 | + vi.mocked(glob).mockResolvedValue(['/build/file1.map', '/build/file2.map']); |
| 54 | + vi.mocked(fs.promises.rm).mockResolvedValue(undefined); |
| 55 | + }); |
| 56 | + |
| 57 | + afterEach(() => { |
| 58 | + vi.resetModules(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should create a new Sentry release when release name is provided', async () => { |
| 62 | + const config = { |
| 63 | + ...defaultConfig, |
| 64 | + sentryConfig: { |
| 65 | + ...defaultConfig.sentryConfig, |
| 66 | + release: { |
| 67 | + name: 'v1.0.0', |
| 68 | + }, |
| 69 | + }, |
| 70 | + }; |
| 71 | + |
| 72 | + await sentryOnBuildEnd(config); |
| 73 | + |
| 74 | + expect(mockSentryCliInstance.releases.new).toHaveBeenCalledWith('v1.0.0'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should upload source maps when enabled', async () => { |
| 78 | + const config = { |
| 79 | + ...defaultConfig, |
| 80 | + sentryConfig: { |
| 81 | + ...defaultConfig.sentryConfig, |
| 82 | + sourceMapsUploadOptions: { |
| 83 | + enabled: true, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }; |
| 87 | + |
| 88 | + await sentryOnBuildEnd(config); |
| 89 | + |
| 90 | + expect(mockSentryCliInstance.releases.uploadSourceMaps).toHaveBeenCalledWith('undefined', { |
| 91 | + include: [{ paths: ['/build'] }], |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should not upload source maps when explicitly disabled', async () => { |
| 96 | + const config = { |
| 97 | + ...defaultConfig, |
| 98 | + sentryConfig: { |
| 99 | + ...defaultConfig.sentryConfig, |
| 100 | + sourceMapsUploadOptions: { |
| 101 | + enabled: false, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }; |
| 105 | + |
| 106 | + await sentryOnBuildEnd(config); |
| 107 | + |
| 108 | + expect(mockSentryCliInstance.releases.uploadSourceMaps).not.toHaveBeenCalled(); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should delete source maps after upload with default pattern', async () => { |
| 112 | + await sentryOnBuildEnd(defaultConfig); |
| 113 | + |
| 114 | + expect(glob).toHaveBeenCalledWith(['/build/**/*.map'], { |
| 115 | + absolute: true, |
| 116 | + nodir: true, |
| 117 | + }); |
| 118 | + expect(fs.promises.rm).toHaveBeenCalledTimes(2); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should delete custom files after upload when specified', async () => { |
| 122 | + const config = { |
| 123 | + ...defaultConfig, |
| 124 | + sentryConfig: { |
| 125 | + ...defaultConfig.sentryConfig, |
| 126 | + sourceMapsUploadOptions: { |
| 127 | + filesToDeleteAfterUpload: '/custom/**/*.map', |
| 128 | + }, |
| 129 | + }, |
| 130 | + }; |
| 131 | + |
| 132 | + await sentryOnBuildEnd(config); |
| 133 | + |
| 134 | + expect(glob).toHaveBeenCalledWith('/custom/**/*.map', { |
| 135 | + absolute: true, |
| 136 | + nodir: true, |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should handle errors during release creation gracefully', async () => { |
| 141 | + const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 142 | + mockSentryCliInstance.releases.new.mockRejectedValueOnce(new Error('Release creation failed')); |
| 143 | + |
| 144 | + const config = { |
| 145 | + ...defaultConfig, |
| 146 | + sentryConfig: { |
| 147 | + ...defaultConfig.sentryConfig, |
| 148 | + release: { |
| 149 | + name: 'v1.0.0', |
| 150 | + }, |
| 151 | + }, |
| 152 | + }; |
| 153 | + |
| 154 | + await sentryOnBuildEnd(config); |
| 155 | + |
| 156 | + expect(consoleSpy).toHaveBeenCalledWith('[Sentry] Could not create release', expect.any(Error)); |
| 157 | + consoleSpy.mockRestore(); |
| 158 | + }); |
| 159 | + |
| 160 | + it('should handle errors during source map upload gracefully', async () => { |
| 161 | + const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 162 | + mockSentryCliInstance.releases.uploadSourceMaps.mockRejectedValueOnce(new Error('Upload failed')); |
| 163 | + |
| 164 | + await sentryOnBuildEnd(defaultConfig); |
| 165 | + |
| 166 | + expect(consoleSpy).toHaveBeenCalledWith('[Sentry] Could not upload sourcemaps', expect.any(Error)); |
| 167 | + consoleSpy.mockRestore(); |
| 168 | + }); |
| 169 | + |
| 170 | + it('should log debug information when debug is enabled', async () => { |
| 171 | + const consoleSpy = vi.spyOn(console, 'info').mockImplementation(() => {}); |
| 172 | + |
| 173 | + const config = { |
| 174 | + ...defaultConfig, |
| 175 | + sentryConfig: { |
| 176 | + ...defaultConfig.sentryConfig, |
| 177 | + debug: true, |
| 178 | + }, |
| 179 | + }; |
| 180 | + |
| 181 | + await sentryOnBuildEnd(config); |
| 182 | + |
| 183 | + expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('[Sentry] Automatically setting')); |
| 184 | + expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('Deleting asset after upload:')); |
| 185 | + consoleSpy.mockRestore(); |
| 186 | + }); |
| 187 | +}); |
0 commit comments