11import path from 'path' ;
2+ import slash from 'slash' ;
23import AddAssetHtmlPlugin from './src/index' ;
34import addAllAssetsToCompilation from './src/addAllAssetsToCompilation' ;
45import handleUrl from './src/handleUrl' ;
56
7+ const testFile = slash ( require . resolve ( './fixture/some-file' ) ) ;
8+
69const pluginMock = {
710 plugin : {
811 addFileToAssets : filename => Promise . resolve ( path . basename ( filename ) ) ,
@@ -20,7 +23,7 @@ test('assets should should be reversed', () => {
2023 expect ( new AddAssetHtmlPlugin ( [ 'a' , 'b' ] ) . assets ) . toEqual ( [ 'b' , 'a' ] ) ;
2124} ) ;
2225
23- test ( 'should not reject success' , async ( ) => {
26+ test ( 'should not reject on success' , async ( ) => {
2427 expect ( await addAllAssetsToCompilation ( [ ] , { } , pluginMock ) ) . toEqual (
2528 pluginMock ,
2629 ) ;
@@ -90,7 +93,7 @@ test('should add file missing "/" to public path', async () => {
9093 expect ( pluginData . assets ) . toMatchSnapshot ( ) ;
9194} ) ;
9295
93- test ( 'should add sourcemap to compilation' , async ( ) => {
96+ test ( 'should add sourcemap and gzipped files to compilation' , async ( ) => {
9497 const addFileToAssetsStub = jest . fn ( ) ;
9598 const compilation = { options : { output : { } } } ;
9699 const pluginData = {
@@ -100,25 +103,26 @@ test('should add sourcemap to compilation', async () => {
100103 addFileToAssetsStub . mockReturnValue ( Promise . resolve ( 'my-file.js' ) ) ;
101104
102105 await addAllAssetsToCompilation (
103- [ { filepath : 'my-file.js' } ] ,
106+ [ { filepath : testFile } ] ,
104107 compilation ,
105108 pluginData ,
106109 ) ;
107110
108111 expect ( pluginData . assets ) . toMatchSnapshot ( ) ;
109112
110- expect ( addFileToAssetsStub ) . toHaveBeenCalledTimes ( 2 ) ;
111- expect ( addFileToAssetsStub . mock . calls [ 0 ] ) . toEqual ( [
112- 'my-file.js' ,
113+ expect ( addFileToAssetsStub ) . toHaveBeenCalledTimes ( 3 ) ;
114+ expect ( addFileToAssetsStub . mock . calls [ 0 ] ) . toEqual ( [ testFile , compilation ] ) ;
115+ expect ( addFileToAssetsStub . mock . calls [ 1 ] ) . toEqual ( [
116+ `${ testFile } .gz` ,
113117 compilation ,
114118 ] ) ;
115- expect ( addFileToAssetsStub . mock . calls [ 1 ] ) . toEqual ( [
116- 'my-file.js. map' ,
119+ expect ( addFileToAssetsStub . mock . calls [ 2 ] ) . toEqual ( [
120+ ` ${ testFile } . map` ,
117121 compilation ,
118122 ] ) ;
119123} ) ;
120124
121- test ( 'should skip adding sourcemap to compilation if set to false' , async ( ) => {
125+ test ( 'should skip adding sourcemap and gzipped files to compilation if set to false' , async ( ) => {
122126 const addFileToAssetsStub = jest . fn ( ) ;
123127 const compilation = { options : { output : { } } } ;
124128 const pluginData = {
@@ -128,7 +132,7 @@ test('should skip adding sourcemap to compilation if set to false', async () =>
128132 addFileToAssetsStub . mockReturnValue ( Promise . resolve ( 'my-file.js' ) ) ;
129133
130134 await addAllAssetsToCompilation (
131- [ { filepath : 'my-file.js' , includeSourcemap : false } ] ,
135+ [ { filepath : 'my-file.js' , includeRelatedFiles : false } ] ,
132136 compilation ,
133137 pluginData ,
134138 ) ;
@@ -192,17 +196,18 @@ test('should replace compilation assets key if `outputPath` is set', async () =>
192196 } ;
193197
194198 await addAllAssetsToCompilation (
195- [ { filepath : 'my-file.js' , outputPath : 'assets' } ] ,
199+ [ { filepath : testFile , outputPath : 'assets' } ] ,
196200 compilation ,
197201 pluginData ,
198202 ) ;
199203
200204 expect ( pluginData . assets ) . toMatchSnapshot ( ) ;
201205
202- expect ( compilation . assets [ 'my-file.js' ] ) . toBeUndefined ( ) ;
203- expect ( compilation . assets [ 'assets/my-file.js' ] ) . toEqual ( source ) ;
204- expect ( compilation . assets [ 'my-file.js.map' ] ) . toBeUndefined ( ) ;
205- expect ( compilation . assets [ 'assets/my-file.js.map' ] ) . toEqual ( source ) ;
206+ expect ( compilation . assets ) . toEqual ( {
207+ 'assets/some-file.js' : source ,
208+ 'assets/some-file.js.gz' : source ,
209+ 'assets/some-file.js.map' : source ,
210+ } ) ;
206211} ) ;
207212
208213test ( 'filter option should exclude some files' , async ( ) => {
0 commit comments