@@ -286,6 +286,14 @@ describe('ChunksWebpackPlugin', () => {
286
286
'<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />' ,
287
287
filename : 'templates/home-styles.html'
288
288
} ) ;
289
+ expect ( compilationWebpack . emitAsset ) . toHaveBeenNthCalledWith (
290
+ 1 ,
291
+ 'templates/home-styles.html' ,
292
+ {
293
+ source :
294
+ '<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />'
295
+ }
296
+ ) ;
289
297
290
298
// JS
291
299
expect ( compilationWebpack . getCache ( ) . getLazyHashedEtag ) . toHaveBeenNthCalledWith (
@@ -335,6 +343,14 @@ describe('ChunksWebpackPlugin', () => {
335
343
'<script defer src="https://cdn.domain.com/dist/abc.js"></script><script defer src="https://cdn.domain.com/dist/def.js"></script>' ,
336
344
filename : 'templates/home-scripts.html'
337
345
} ) ;
346
+ expect ( compilationWebpack . emitAsset ) . toHaveBeenNthCalledWith (
347
+ 2 ,
348
+ 'templates/home-scripts.html' ,
349
+ {
350
+ source :
351
+ '<script defer src="https://cdn.domain.com/dist/abc.js"></script><script defer src="https://cdn.domain.com/dist/def.js"></script>'
352
+ }
353
+ ) ;
338
354
339
355
expect ( compilationWebpack . getCache ( ) . getLazyHashedEtag ) . toHaveBeenNthCalledWith ( 7 , {
340
356
source :
@@ -363,128 +379,153 @@ describe('ChunksWebpackPlugin', () => {
363
379
} ) ;
364
380
} ) ;
365
381
366
- it ( 'Should call the addAssets function with dependencies and without cache with CSS only' , async ( ) => {
367
- chunksWebpackPlugin . getFilesDependenciesByEntrypoint = jest . fn ( ) . mockReturnValue ( {
368
- css : [
369
- { name : 'a.css' , source : 'module css a' } ,
370
- { name : 'b.css' , source : 'module css b' } ,
371
- { name : 'c.css' , source : 'module css c' }
372
- ] ,
373
- js : [ ]
374
- } ) ;
375
- chunksWebpackPlugin . getPublicPath = jest . fn ( ) . mockReturnValue ( 'dist/' ) ;
376
- chunksWebpackPlugin . getAssetData = jest
377
- . fn ( )
378
- . mockReturnValueOnce ( {
379
- filePath : [ 'dist/abc.css' , 'dist/def.css' ] ,
380
- htmlTags :
382
+ describe ( 'Should call the addAssets function with dependencies and without cache with CSS only' , ( ) => {
383
+ beforeEach ( ( ) => {
384
+ chunksWebpackPlugin . getFilesDependenciesByEntrypoint = jest . fn ( ) . mockReturnValue ( {
385
+ css : [
386
+ { name : 'a.css' , source : 'module css a' } ,
387
+ { name : 'b.css' , source : 'module css b' } ,
388
+ { name : 'c.css' , source : 'module css c' }
389
+ ] ,
390
+ js : [ ]
391
+ } ) ;
392
+ chunksWebpackPlugin . getPublicPath = jest . fn ( ) . mockReturnValue ( 'dist/' ) ;
393
+ chunksWebpackPlugin . getAssetData = jest
394
+ . fn ( )
395
+ . mockReturnValueOnce ( {
396
+ filePath : [ 'dist/abc.css' , 'dist/def.css' ] ,
397
+ htmlTags :
398
+ '<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />'
399
+ } )
400
+ . mockReturnValueOnce ( {
401
+ filePath : [ ] ,
402
+ htmlTags : ''
403
+ } ) ;
404
+ chunksWebpackPlugin . createChunksManifestFile = jest . fn ( ) ;
405
+ compilationWebpack . compiler . webpack . sources . RawSource . mockReturnValueOnce ( {
406
+ source :
381
407
'<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />'
382
- } )
383
- . mockReturnValueOnce ( {
384
- filePath : [ ] ,
385
- htmlTags : ''
386
408
} ) ;
387
- chunksWebpackPlugin . createChunksManifestFile = jest . fn ( ) ;
388
- compilationWebpack . compiler . webpack . sources . RawSource . mockReturnValueOnce ( {
389
- source :
390
- '<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />'
391
- } ) ;
392
409
393
- compilationWebpack . entrypoints . keys . mockReturnValue ( [ 'home' ] ) ;
394
- compilationWebpack . getCache . mockReturnValue ( {
395
- getLazyHashedEtag : jest
396
- . fn ( )
397
- . mockReturnValueOnce ( 'module css a' )
398
- . mockReturnValueOnce ( 'module css b' )
399
- . mockReturnValueOnce ( 'module css c' )
400
- . mockReturnValueOnce ( 'css home' ) ,
401
- mergeEtags : jest
402
- . fn ( )
403
- . mockReturnValueOnce ( '123456789123' )
404
- . mockReturnValueOnce ( '123456789123' ) ,
405
- getItemCache : jest . fn ( ) . mockReturnValue ( {
406
- getPromise : jest . fn ( ) ,
407
- storePromise : jest . fn ( )
408
- } )
410
+ compilationWebpack . entrypoints . keys . mockReturnValue ( [ 'home' ] ) ;
411
+ compilationWebpack . getCache . mockReturnValue ( {
412
+ getLazyHashedEtag : jest
413
+ . fn ( )
414
+ . mockReturnValueOnce ( 'module css a' )
415
+ . mockReturnValueOnce ( 'module css b' )
416
+ . mockReturnValueOnce ( 'module css c' )
417
+ . mockReturnValueOnce ( 'css home' ) ,
418
+ mergeEtags : jest
419
+ . fn ( )
420
+ . mockReturnValueOnce ( '123456789123' )
421
+ . mockReturnValueOnce ( '123456789123' ) ,
422
+ getItemCache : jest . fn ( ) . mockReturnValue ( {
423
+ getPromise : jest . fn ( ) ,
424
+ storePromise : jest . fn ( )
425
+ } )
426
+ } ) ;
409
427
} ) ;
410
428
411
- await chunksWebpackPlugin . addAssets ( compilationWebpack ) ;
412
-
413
- expect ( chunksWebpackPlugin . getFilesDependenciesByEntrypoint ) . toHaveBeenCalledWith ( {
414
- compilation : compilationWebpack ,
415
- entryName : 'home'
416
- } ) ;
417
- expect ( chunksWebpackPlugin . getPublicPath ) . toHaveBeenCalledWith ( compilationWebpack , 'home' ) ;
429
+ afterEach ( ( ) => {
430
+ expect ( chunksWebpackPlugin . getFilesDependenciesByEntrypoint ) . toHaveBeenCalledWith ( {
431
+ compilation : compilationWebpack ,
432
+ entryName : 'home'
433
+ } ) ;
434
+ expect ( chunksWebpackPlugin . getPublicPath ) . toHaveBeenCalledWith ( compilationWebpack , 'home' ) ;
418
435
419
- // CSS
420
- expect ( compilationWebpack . getCache ( ) . getLazyHashedEtag ) . toHaveBeenNthCalledWith (
421
- 1 ,
422
- 'module css a'
423
- ) ;
424
- expect ( compilationWebpack . getCache ( ) . getLazyHashedEtag ) . toHaveBeenNthCalledWith (
425
- 2 ,
426
- 'module css b'
427
- ) ;
428
- expect ( compilationWebpack . getCache ( ) . getLazyHashedEtag ) . toHaveBeenNthCalledWith (
429
- 3 ,
430
- 'module css c'
431
- ) ;
432
- expect ( compilationWebpack . getCache ( ) . getItemCache ) . toHaveBeenNthCalledWith (
433
- 1 ,
434
- 'css|home' ,
435
- '123456789123'
436
- ) ;
437
- expect ( compilationWebpack . getCache ( ) . getItemCache ( ) . getPromise ) . toHaveBeenNthCalledWith ( 1 ) ;
438
- expect ( chunksWebpackPlugin . getAssetData ) . toHaveBeenNthCalledWith ( 1 , {
439
- templateFunction : chunksWebpackPlugin . options . templateStyle ,
440
- assets : [
441
- {
442
- name : 'a.css' ,
443
- source : 'module css a'
444
- } ,
445
- {
446
- name : 'b.css' ,
447
- source : 'module css b'
448
- } ,
436
+ // CSS
437
+ expect ( compilationWebpack . getCache ( ) . getLazyHashedEtag ) . toHaveBeenNthCalledWith (
438
+ 1 ,
439
+ 'module css a'
440
+ ) ;
441
+ expect ( compilationWebpack . getCache ( ) . getLazyHashedEtag ) . toHaveBeenNthCalledWith (
442
+ 2 ,
443
+ 'module css b'
444
+ ) ;
445
+ expect ( compilationWebpack . getCache ( ) . getLazyHashedEtag ) . toHaveBeenNthCalledWith (
446
+ 3 ,
447
+ 'module css c'
448
+ ) ;
449
+ expect ( compilationWebpack . getCache ( ) . getItemCache ) . toHaveBeenNthCalledWith (
450
+ 1 ,
451
+ 'css|home' ,
452
+ '123456789123'
453
+ ) ;
454
+ expect ( compilationWebpack . getCache ( ) . getItemCache ( ) . getPromise ) . toHaveBeenNthCalledWith ( 1 ) ;
455
+ expect ( chunksWebpackPlugin . getAssetData ) . toHaveBeenNthCalledWith ( 1 , {
456
+ templateFunction : chunksWebpackPlugin . options . templateStyle ,
457
+ assets : [
458
+ {
459
+ name : 'a.css' ,
460
+ source : 'module css a'
461
+ } ,
462
+ {
463
+ name : 'b.css' ,
464
+ source : 'module css b'
465
+ } ,
466
+ {
467
+ name : 'c.css' ,
468
+ source : 'module css c'
469
+ }
470
+ ] ,
471
+ entryName : 'home' ,
472
+ publicPath : 'dist/'
473
+ } ) ;
474
+ expect ( compilationWebpack . getCache ( ) . getItemCache ( ) . storePromise ) . toHaveBeenNthCalledWith (
475
+ 1 ,
449
476
{
450
- name : 'c.css' ,
451
- source : 'module css c'
477
+ source : {
478
+ source :
479
+ '<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />'
480
+ } ,
481
+ filePath : [ 'dist/abc.css' , 'dist/def.css' ] ,
482
+ htmlTags :
483
+ '<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />' ,
484
+ filename : 'templates/home-styles.html'
452
485
}
453
- ] ,
454
- entryName : 'home' ,
455
- publicPath : 'dist/'
456
- } ) ;
457
- expect ( compilationWebpack . getCache ( ) . getItemCache ( ) . storePromise ) . toHaveBeenNthCalledWith ( 1 , {
458
- source : {
486
+ ) ;
487
+
488
+ expect ( compilationWebpack . getCache ( ) . getLazyHashedEtag ) . toHaveBeenNthCalledWith ( 4 , {
459
489
source :
460
490
'<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />'
461
- } ,
462
- filePath : [ 'dist/abc.css' , 'dist/def.css' ] ,
463
- htmlTags :
464
- '<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />' ,
465
- filename : 'templates/home-styles.html'
491
+ } ) ;
492
+ expect ( compilationWebpack . getCache ( ) . getLazyHashedEtag ) . toHaveBeenCalledTimes ( 4 ) ;
493
+ expect ( compilationWebpack . getCache ( ) . mergeEtags ) . toHaveBeenCalledTimes ( 2 ) ;
494
+ expect ( chunksWebpackPlugin . createChunksManifestFile ) . toHaveBeenCalledWith ( {
495
+ compilation : compilationWebpack ,
496
+ cache : {
497
+ getLazyHashedEtag : expect . any ( Function ) ,
498
+ mergeEtags : expect . any ( Function ) ,
499
+ getItemCache : expect . any ( Function )
500
+ } ,
501
+ eTag : 'css home' , // Because reduce is not executed when array contains only one item
502
+ manifest : {
503
+ home : {
504
+ scripts : [ ] ,
505
+ styles : [ 'dist/abc.css' , 'dist/def.css' ]
506
+ }
507
+ }
508
+ } ) ;
466
509
} ) ;
467
510
468
- expect ( compilationWebpack . getCache ( ) . getLazyHashedEtag ) . toHaveBeenNthCalledWith ( 4 , {
469
- source :
470
- '<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />'
471
- } ) ;
472
- expect ( compilationWebpack . getCache ( ) . getLazyHashedEtag ) . toHaveBeenCalledTimes ( 4 ) ;
473
- expect ( compilationWebpack . getCache ( ) . mergeEtags ) . toHaveBeenCalledTimes ( 2 ) ;
474
- expect ( chunksWebpackPlugin . createChunksManifestFile ) . toHaveBeenCalledWith ( {
475
- compilation : compilationWebpack ,
476
- cache : {
477
- getLazyHashedEtag : expect . any ( Function ) ,
478
- mergeEtags : expect . any ( Function ) ,
479
- getItemCache : expect . any ( Function )
480
- } ,
481
- eTag : 'css home' , // Because reduce is not executed when array contains only one item
482
- manifest : {
483
- home : {
484
- scripts : [ ] ,
485
- styles : [ 'dist/abc.css' , 'dist/def.css' ]
511
+ it ( 'With generateChunksFiles true' , async ( ) => {
512
+ await chunksWebpackPlugin . addAssets ( compilationWebpack ) ;
513
+
514
+ expect ( compilationWebpack . emitAsset ) . toHaveBeenNthCalledWith (
515
+ 1 ,
516
+ 'templates/home-styles.html' ,
517
+ {
518
+ source :
519
+ '<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />'
486
520
}
487
- }
521
+ ) ;
522
+ } ) ;
523
+
524
+ it ( 'With generateChunksFiles false' , async ( ) => {
525
+ chunksWebpackPlugin . options . generateChunksFiles = false ;
526
+ await chunksWebpackPlugin . addAssets ( compilationWebpack ) ;
527
+
528
+ expect ( compilationWebpack . emitAsset ) . not . toHaveBeenCalled ( ) ;
488
529
} ) ;
489
530
} ) ;
490
531
} ) ;
0 commit comments