@@ -15,16 +15,16 @@ const { loadUserConfig, parseEntryFromFunction } = vi.hoisted(() => ({
1515 parseEntryFromFunction : vi . fn ( )
1616} ) ) ;
1717
18+ const esbuildMock = {
19+ build : vi . fn ( )
20+ } ;
21+
1822vi . mock ( '../load.js' , ( ) => ( { loadUserConfig } ) ) ;
1923vi . mock ( '../parse.js' , ( ) => ( { parseEntryFromFunction } ) ) ;
2024
2125describe ( 'bundle' , ( ) => {
22- const esbuild = {
23- build : vi . fn ( )
24- } ;
25-
2626 beforeAll ( ( ) => {
27- vi . doMock ( 'esbuild' , ( ) => esbuild ) ;
27+ vi . doMock ( 'esbuild' , ( ) => esbuildMock ) ;
2828 } ) ;
2929
3030 afterAll ( ( ) => {
@@ -33,7 +33,7 @@ describe('bundle', () => {
3333
3434 it ( 'should return an error if esbuild throws' , async ( ) => {
3535 const cause = new Error ( 'Something went wrong' ) ;
36- esbuild . build . mockImplementationOnce ( ( ) => {
36+ esbuildMock . build . mockImplementationOnce ( ( ) => {
3737 throw cause ;
3838 } ) ;
3939 await expect (
@@ -47,7 +47,7 @@ describe('bundle', () => {
4747 } ) ;
4848
4949 it ( 'should an ok result on success' , async ( ) => {
50- esbuild . build . mockResolvedValueOnce ( { } ) ;
50+ esbuildMock . build . mockResolvedValueOnce ( { } ) ;
5151 const result = await bundle ( {
5252 config : { build : { outfile : 'out.js' } } ,
5353 entrySpecifier : './app.ts' ,
@@ -98,10 +98,12 @@ describe('buildProd', () => {
9898
9999 it ( 'should correctly bundle the example application as a module' , { timeout : 10000 } , async ( ) => {
100100 const outfile = path . join ( outdir , 'module.js' ) ;
101+ const onComplete = vi . fn ( ) ;
101102 loadUserConfig . mockReturnValue (
102103 okAsync ( {
103104 build : {
104105 mode : 'module' ,
106+ onComplete,
105107 outfile
106108 } ,
107109 entry : vi . fn ( ) ,
@@ -118,5 +120,35 @@ describe('buildProd', () => {
118120 const appContainer = await import ( outfile ) . then ( ( module ) => module . default as AppContainer ) ;
119121 const app = appContainer . getApplicationInstance ( ) ;
120122 expect ( app ) . toBeDefined ( ) ;
123+ expect ( onComplete ) . toHaveBeenCalledOnce ( ) ;
124+ } ) ;
125+
126+ it ( 'should handle errors in the onComplete callback' , async ( ) => {
127+ vi . doMock ( 'esbuild' , ( ) => esbuildMock ) ;
128+ const callbackError = new Error ( 'Something went wrong' ) ;
129+ const onComplete = vi . fn ( ) . mockImplementation ( ( ) => {
130+ throw callbackError ;
131+ } ) ;
132+ loadUserConfig . mockReturnValue (
133+ okAsync ( {
134+ build : {
135+ mode : 'module' ,
136+ onComplete,
137+ outfile : '/dev/null'
138+ } ,
139+ entry : vi . fn ( )
140+ } satisfies UserConfigOptions )
141+ ) ;
142+ parseEntryFromFunction . mockReturnValueOnce ( ok ( './example/app.js' ) ) ;
143+ const result = await buildProd ( { configFile } ) ;
144+ expect ( result . isErr ( ) ) . toBe ( true ) ;
145+ expect ( result ) . toMatchObject ( {
146+ error : {
147+ cause : callbackError ,
148+ message : 'An error occurred in the user-specified `onComplete` callback'
149+ }
150+ } ) ;
151+ expect ( onComplete ) . toHaveBeenCalledOnce ( ) ;
152+ vi . doUnmock ( 'esbuild' ) ;
121153 } ) ;
122154} ) ;
0 commit comments