11import * as fs from 'fs' ;
2- import { describe , expect , it , vi } from 'vitest' ;
2+ import { afterEach , describe , expect , it , vi } from 'vitest' ;
33import { getBuildPluginOptions } from '../../src/config/getBuildPluginOptions' ;
44import type { SentryBuildOptions } from '../../src/config/types' ;
55
@@ -12,6 +12,14 @@ describe('getBuildPluginOptions', () => {
1212 const mockReleaseName = 'test-release-1.0.0' ;
1313 const mockDistDirAbsPath = '/path/to/.next' ;
1414
15+ function mockExistingDirectories ( existingDirectories : string [ ] ) : void {
16+ vi . mocked ( fs . existsSync ) . mockImplementation ( p => existingDirectories . includes ( String ( p ) ) ) ;
17+ }
18+
19+ afterEach ( ( ) => {
20+ vi . mocked ( fs . existsSync ) . mockReset ( ) ;
21+ } ) ;
22+
1523 describe ( 'basic functionality' , ( ) => {
1624 it ( 'returns correct build plugin options with minimal configuration for after-production-compile-webpack' , ( ) => {
1725 const sentryBuildOptions : SentryBuildOptions = {
@@ -273,6 +281,8 @@ describe('getBuildPluginOptions', () => {
273281 } ) ;
274282
275283 it ( 'configures after-production-compile-turbopack build correctly' , ( ) => {
284+ mockExistingDirectories ( [ '/path/to/.next/static/chunks' ] ) ;
285+
276286 const result = getBuildPluginOptions ( {
277287 sentryBuildOptions : baseSentryOptions ,
278288 releaseName : mockReleaseName ,
@@ -302,8 +312,8 @@ describe('getBuildPluginOptions', () => {
302312 expect ( result . reactComponentAnnotation ) . toBeUndefined ( ) ;
303313 } ) ;
304314
305- it ( 'includes the immutable chunks directory in after-production-compile-turbopack assets when it exists ' , ( ) => {
306- vi . mocked ( fs . existsSync ) . mockImplementationOnce ( p => p === '/path/to/.next/static/immutable/chunks' ) ;
315+ it ( 'includes both chunk directories in after-production-compile-turbopack assets when both exist ' , ( ) => {
316+ mockExistingDirectories ( [ '/path/to/.next/static/chunks' , '/path/to/.next/static/immutable/chunks' ] ) ;
307317
308318 const result = getBuildPluginOptions ( {
309319 sentryBuildOptions : baseSentryOptions ,
@@ -320,7 +330,7 @@ describe('getBuildPluginOptions', () => {
320330 } ) ;
321331
322332 it ( 'does not include the immutable chunks directory in after-production-compile-turbopack assets when it does not exist' , ( ) => {
323- vi . mocked ( fs . existsSync ) . mockReturnValueOnce ( false ) ;
333+ mockExistingDirectories ( [ '/path/to/.next/static/chunks' ] ) ;
324334
325335 const result = getBuildPluginOptions ( {
326336 sentryBuildOptions : baseSentryOptions ,
@@ -331,6 +341,32 @@ describe('getBuildPluginOptions', () => {
331341
332342 expect ( result . sourcemaps ?. assets ) . toEqual ( [ '/path/to/.next/server' , '/path/to/.next/static/chunks' ] ) ;
333343 } ) ;
344+
345+ it ( 'does not include the static chunks directory in after-production-compile-turbopack assets when only the immutable chunks directory exists' , ( ) => {
346+ mockExistingDirectories ( [ '/path/to/.next/static/immutable/chunks' ] ) ;
347+
348+ const result = getBuildPluginOptions ( {
349+ sentryBuildOptions : baseSentryOptions ,
350+ releaseName : mockReleaseName ,
351+ distDirAbsPath : mockDistDirAbsPath ,
352+ buildTool : 'after-production-compile-turbopack' ,
353+ } ) ;
354+
355+ expect ( result . sourcemaps ?. assets ) . toEqual ( [ '/path/to/.next/server' , '/path/to/.next/static/immutable/chunks' ] ) ;
356+ } ) ;
357+
358+ it ( 'only includes the server directory in after-production-compile-turbopack assets when no chunk directory exists' , ( ) => {
359+ mockExistingDirectories ( [ ] ) ;
360+
361+ const result = getBuildPluginOptions ( {
362+ sentryBuildOptions : baseSentryOptions ,
363+ releaseName : mockReleaseName ,
364+ distDirAbsPath : mockDistDirAbsPath ,
365+ buildTool : 'after-production-compile-turbopack' ,
366+ } ) ;
367+
368+ expect ( result . sourcemaps ?. assets ) . toEqual ( [ '/path/to/.next/server' ] ) ;
369+ } ) ;
334370 } ) ;
335371
336372 describe ( 'useRunAfterProductionCompileHook functionality' , ( ) => {
@@ -1077,6 +1113,7 @@ describe('getBuildPluginOptions', () => {
10771113
10781114 it ( 'handles complex nested path structures' , ( ) => {
10791115 const complexPath = '/very/deep/nested/path/with/multiple/segments/.next' ;
1116+ mockExistingDirectories ( [ `${ complexPath } /static/chunks` ] ) ;
10801117 const sentryBuildOptions : SentryBuildOptions = {
10811118 org : 'test-org' ,
10821119 project : 'test-project' ,
0 commit comments