@@ -41,9 +41,10 @@ jest.mock('vm', () => ({
41
41
} ,
42
42
} ) ) ;
43
43
44
- global . fetch = jest . fn ( ) . mockResolvedValue ( {
44
+ ( global as unknown as any ) . fetch = jest . fn ( ) . mockResolvedValue ( {
45
45
text : jest . fn ( ) . mockResolvedValue ( '// mock chunk content' ) ,
46
46
} ) ;
47
+ const globalFetch = ( global as unknown as any ) . fetch as jest . Mock ;
47
48
48
49
const mockWebpackRequire = {
49
50
u : jest . fn ( ( chunkId : string ) => `/chunks/${ chunkId } .js` ) ,
@@ -77,7 +78,7 @@ const mockNonWebpackRequire = jest.fn().mockImplementation((id: string) => {
77
78
if ( id === 'path' ) return require ( 'path' ) ;
78
79
if ( id === 'fs' ) return require ( 'fs' ) ;
79
80
if ( id === 'vm' ) return require ( 'vm' ) ;
80
- if ( id === 'node-fetch' ) return { default : global . fetch } ;
81
+ if ( id === 'node-fetch' ) return { default : globalFetch } ;
81
82
return { } ;
82
83
} ) ;
83
84
@@ -343,11 +344,11 @@ describe('runtimePlugin', () => {
343
344
344
345
describe ( 'fetchAndRun' , ( ) => {
345
346
beforeEach ( ( ) => {
346
- ( global . fetch as jest . Mock ) . mockReset ( ) ;
347
+ ( globalFetch as jest . Mock ) . mockReset ( ) ;
347
348
} ) ;
348
349
349
350
it ( 'should fetch and execute remote content' , async ( ) => {
350
- ( global . fetch as jest . Mock ) . mockResolvedValue ( {
351
+ ( globalFetch as jest . Mock ) . mockResolvedValue ( {
351
352
text : jest . fn ( ) . mockResolvedValue ( '// mock script content' ) ,
352
353
} ) ;
353
354
@@ -381,7 +382,7 @@ describe('runtimePlugin', () => {
381
382
382
383
it ( 'should handle fetch errors' , async ( ) => {
383
384
const fetchError = new Error ( 'Fetch failed' ) ;
384
- ( global . fetch as jest . Mock ) . mockRejectedValue ( fetchError ) ;
385
+ ( globalFetch as jest . Mock ) . mockRejectedValue ( fetchError ) ;
385
386
386
387
const url = new URL ( 'http://example.com/chunk.js' ) ;
387
388
const callback = jest . fn ( ) ;
@@ -403,6 +404,9 @@ describe('runtimePlugin', () => {
403
404
await new Promise ( ( resolve ) => setTimeout ( resolve , 10 ) ) ;
404
405
405
406
expect ( callback ) . toHaveBeenCalledWith ( expect . any ( Error ) , null ) ;
407
+ expect ( callback . mock . calls [ 0 ] [ 0 ] . message . includes ( url . href ) ) . toEqual (
408
+ true ,
409
+ ) ;
406
410
} ) ;
407
411
} ) ;
408
412
@@ -746,6 +750,50 @@ describe('runtimePlugin', () => {
746
750
// The original promise should be reused
747
751
expect ( promises [ 0 ] ) . toBe ( originalPromise ) ;
748
752
} ) ;
753
+
754
+ it ( 'should delete chunks from the installedChunks when loadChunk fails' , async ( ) => {
755
+ // mock loadChunk to fail
756
+ jest
757
+ . spyOn ( runtimePluginModule , 'loadChunk' )
758
+ . mockImplementationOnce ( ( strategy , chunkId , root , callback , args ) => {
759
+ Promise . resolve ( ) . then ( ( ) => {
760
+ callback ( new Error ( 'failed to load' ) , undefined ) ;
761
+ } ) ;
762
+ } ) ;
763
+
764
+ jest
765
+ . spyOn ( runtimePluginModule , 'installChunk' )
766
+ . mockImplementationOnce ( ( chunk , installedChunks ) => {
767
+ // Mock implementation that doesn't rely on iterating chunk.ids
768
+ installedChunks [ 'test-chunk' ] = 0 ;
769
+ } ) ;
770
+
771
+ // Mock installedChunks
772
+ const installedChunks : Record < string , any > = { } ;
773
+
774
+ // Call the function under test - returns the handler function, doesn't set webpack_require.f.require
775
+ const handler = setupChunkHandler ( installedChunks , { } ) ;
776
+
777
+ const promises : Promise < any > [ ] = [ ] ;
778
+ let res , err ;
779
+
780
+ try {
781
+ // Call the handler with mock chunk ID and promises array
782
+ handler ( 'test-chunk' , promises ) ;
783
+ // Verify that installedChunks has test-chunk before the promise rejects
784
+ expect ( installedChunks [ 'test-chunk' ] ) . toBeDefined ( ) ;
785
+ res = await promises [ 0 ] ;
786
+ } catch ( e ) {
787
+ err = e ;
788
+ }
789
+
790
+ // Verify that an error was thrown, and the response is undefined
791
+ expect ( res ) . not . toBeDefined ( ) ;
792
+ expect ( err instanceof Error ) . toEqual ( true ) ;
793
+
794
+ // Verify the chunk data was properly removed
795
+ expect ( installedChunks [ 'test-chunk' ] ) . not . toBeDefined ( ) ;
796
+ } ) ;
749
797
} ) ;
750
798
751
799
describe ( 'setupWebpackRequirePatching' , ( ) => {
0 commit comments