11import * as fs from 'fs'
22import * as path from 'path'
3+ import type { Compilation , Compiler } from '@rspack/core'
34import { describe , it , expect , vi , beforeEach } from 'vitest'
45import { AddAssetsToCompilation } from '../../steps/add-assets-to-compilation'
56import * as utils from '../../html-lib/utils'
@@ -38,58 +39,61 @@ vi.mock('@rspack/core', () => {
3839} )
3940
4041describe ( 'AddAssetsToCompilation' , ( ) => {
41- let compilation : any
42- let compiler : any
43- let emitAssetSpy : any
44- let getAssetSpy : any
45- let warningsPushSpy : any
46- let existsSyncSpy : any
47- let readFileSyncSpy : any
48- let processAssetsCallback : any
42+ let compilation : Partial < Compilation >
43+ let compiler : Partial < Compiler >
44+ let emitAssetSpy : ReturnType < typeof vi . fn >
45+ let getAssetSpy : ReturnType < typeof vi . fn >
46+ let warningsPushSpy : ReturnType < typeof vi . fn >
47+ let existsSyncSpy : ReturnType < typeof vi . spyOn >
48+ let readFileSyncSpy : ReturnType < typeof vi . spyOn >
49+ let processAssetsCallback : ( ) => void
4950
5051 beforeEach ( ( ) => {
5152 vi . clearAllMocks ( )
5253
5354 emitAssetSpy = vi . fn ( )
5455 getAssetSpy = vi . fn ( )
5556 warningsPushSpy = vi . fn ( )
56- existsSyncSpy = vi . spyOn ( fs , 'existsSync' ) . mockReturnValue ( true )
57+ existsSyncSpy = vi . spyOn ( fs , 'existsSync' ) . mockReturnValue ( true ) as any
5758 readFileSyncSpy = vi
5859 . spyOn ( fs , 'readFileSync' )
59- . mockReturnValue ( Buffer . from ( 'file-content' ) )
60+ . mockReturnValue ( Buffer . from ( 'file-content' ) ) as any
6061
6162 compilation = {
6263 hooks : {
6364 processAssets : {
64- tap : ( _opts : any , fn : any ) => {
65- processAssetsCallback = fn
65+ tap : ( _opts : unknown , fn : ( assets : Record < string , any > ) => void ) => {
66+ processAssetsCallback = ( ) => fn ( { } )
6667 }
67- }
68- } ,
68+ } as any
69+ } as any ,
6970 getAsset : getAssetSpy ,
7071 emitAsset : emitAssetSpy ,
71- warnings : { push : warningsPushSpy } ,
72+ warnings : { push : warningsPushSpy } as any ,
7273 errors : [ ]
7374 }
7475
7576 compiler = {
7677 hooks : {
7778 thisCompilation : {
78- tap : ( _name : string , fn : any ) => {
79- fn ( compilation )
79+ tap : (
80+ _name : string ,
81+ fn : ( compilation : Compilation , params : any ) => void
82+ ) => {
83+ fn ( compilation as Compilation , { } )
8084 }
81- }
82- }
85+ } as any
86+ } as any
8387 }
8488
8589 // Default path mocking for all tests
86- vi . spyOn ( path , 'join' ) . mockImplementation ( ( ...args ) => {
90+ vi . spyOn ( path , 'join' ) . mockImplementation ( ( ...args : string [ ] ) => {
8791 return args . filter ( Boolean ) . join ( '/' )
8892 } )
89- vi . spyOn ( path . posix , 'join' ) . mockImplementation ( ( ...args ) => {
93+ vi . spyOn ( path . posix , 'join' ) . mockImplementation ( ( ...args : string [ ] ) => {
9094 return args . filter ( Boolean ) . join ( '/' )
9195 } )
92- vi . spyOn ( path , 'basename' ) . mockImplementation ( ( p ) => {
96+ vi . spyOn ( path , 'basename' ) . mockImplementation ( ( p : string ) => {
9397 if ( p === 'resource.html' ) return 'resource.html'
9498 if ( p === 'nested/page.html' ) return 'page.html'
9599 return p . split ( '/' ) . pop ( ) || ''
@@ -107,7 +111,7 @@ describe('AddAssetsToCompilation', () => {
107111 if ( name === 'resource.html' ) {
108112 return {
109113 source : {
110- source : ( ) => '<html><img src="foo .png"></html>'
114+ source : ( ) => '<html><img src="extensionjs .png"></html>'
111115 }
112116 }
113117 }
@@ -117,18 +121,18 @@ describe('AddAssetsToCompilation', () => {
117121 vi . mocked ( utils . getAssetsFromHtml ) . mockReturnValue ( {
118122 css : [ ] ,
119123 js : [ ] ,
120- static : [ 'foo .png' ]
124+ static : [ 'extensionjs .png' ]
121125 } )
122126 vi . mocked ( utils . isFromIncludeList ) . mockReturnValue ( false )
123127 vi . mocked ( webpackUtils . shouldExclude ) . mockReturnValue ( false )
124128
125- plugin . apply ( compiler )
126- processAssetsCallback ( )
129+ plugin . apply ( compiler as Compiler )
130+ if ( processAssetsCallback ) processAssetsCallback ( )
127131
128- expect ( emitAssetSpy ) . toHaveBeenCalled ( )
129- expect ( readFileSyncSpy ) . toHaveBeenCalledWith ( 'foo .png' )
132+ expect ( emitAssetSpy ) . toHaveBeenCalledTimes ( 1 )
133+ expect ( readFileSyncSpy ) . toHaveBeenCalledWith ( 'extensionjs .png' )
130134 expect ( emitAssetSpy ) . toHaveBeenCalledWith (
131- 'assets/foo .png' ,
135+ 'assets/extensionjs .png' ,
132136 expect . any ( Object )
133137 )
134138 expect ( warningsPushSpy ) . not . toHaveBeenCalled ( )
@@ -160,7 +164,7 @@ describe('AddAssetsToCompilation', () => {
160164 vi . mocked ( utils . isFromIncludeList ) . mockReturnValue ( false )
161165 vi . mocked ( webpackUtils . shouldExclude ) . mockReturnValue ( false )
162166
163- plugin . apply ( compiler )
167+ plugin . apply ( compiler as Compiler )
164168 processAssetsCallback ( )
165169
166170 expect ( path . posix . join ) . toHaveBeenCalledWith (
@@ -203,8 +207,8 @@ describe('AddAssetsToCompilation', () => {
203207 vi . mocked ( webpackUtils . shouldExclude ) . mockReturnValue ( false )
204208 existsSyncSpy . mockReturnValue ( false )
205209
206- plugin . apply ( compiler )
207- processAssetsCallback ( )
210+ plugin . apply ( compiler as Compiler )
211+ if ( processAssetsCallback ) processAssetsCallback ( )
208212
209213 expect ( warningsPushSpy ) . toHaveBeenCalled ( )
210214 expect ( emitAssetSpy ) . not . toHaveBeenCalled ( )
@@ -238,7 +242,7 @@ describe('AddAssetsToCompilation', () => {
238242 } )
239243 vi . mocked ( webpackUtils . shouldExclude ) . mockReturnValue ( false )
240244
241- plugin . apply ( compiler )
245+ plugin . apply ( compiler as Compiler )
242246 processAssetsCallback ( )
243247
244248 expect ( warningsPushSpy ) . not . toHaveBeenCalled ( )
0 commit comments