@@ -30,7 +30,7 @@ describe('TestCache', () => {
30
30
} ) ;
31
31
32
32
const casesDirectory = path . resolve ( __dirname , 'cases' ) ;
33
- const directoryForCase = path . resolve ( casesDirectory , 'simple ' ) ;
33
+ const directoryForCase = path . resolve ( casesDirectory , 'asset-modules ' ) ;
34
34
// eslint-disable-next-line import/no-dynamic-require, global-require
35
35
const webpackConfig = require ( path . resolve (
36
36
directoryForCase ,
@@ -58,7 +58,12 @@ describe('TestCache', () => {
58
58
return ;
59
59
}
60
60
61
- expect ( stats . compilation . emittedAssets . size ) . toBe ( 2 ) ;
61
+ expect ( Object . keys ( stats . compilation . assets ) . sort ( ) ) . toMatchSnapshot (
62
+ 'first'
63
+ ) ;
64
+ expect (
65
+ Array . from ( stats . compilation . emittedAssets ) . sort ( )
66
+ ) . toMatchSnapshot ( 'first emitted' ) ;
62
67
expect ( stats . compilation . warnings ) . toHaveLength ( 0 ) ;
63
68
expect ( stats . compilation . errors ) . toHaveLength ( 0 ) ;
64
69
@@ -86,8 +91,12 @@ describe('TestCache', () => {
86
91
return ;
87
92
}
88
93
89
- // Because webpack compare the source content before emitting
90
- expect ( stats . compilation . emittedAssets . size ) . toBe ( 0 ) ;
94
+ expect ( Object . keys ( stats . compilation . assets ) . sort ( ) ) . toMatchSnapshot (
95
+ 'second'
96
+ ) ;
97
+ expect (
98
+ Array . from ( stats . compilation . emittedAssets ) . sort ( )
99
+ ) . toMatchSnapshot ( 'second emitted' ) ;
91
100
expect ( stats . compilation . warnings ) . toHaveLength ( 0 ) ;
92
101
expect ( stats . compilation . errors ) . toHaveLength ( 0 ) ;
93
102
@@ -119,7 +128,7 @@ describe('TestCache', () => {
119
128
} ) ;
120
129
121
130
const casesDirectory = path . resolve ( __dirname , 'cases' ) ;
122
- const directoryForCase = path . resolve ( casesDirectory , 'simple ' ) ;
131
+ const directoryForCase = path . resolve ( casesDirectory , 'asset-modules ' ) ;
123
132
// eslint-disable-next-line import/no-dynamic-require, global-require
124
133
const webpackConfig = require ( path . resolve (
125
134
directoryForCase ,
@@ -149,7 +158,12 @@ describe('TestCache', () => {
149
158
return ;
150
159
}
151
160
152
- expect ( stats . compilation . emittedAssets . size ) . toBe ( 2 ) ;
161
+ expect ( Object . keys ( stats . compilation . assets ) . sort ( ) ) . toMatchSnapshot (
162
+ 'first'
163
+ ) ;
164
+ expect (
165
+ Array . from ( stats . compilation . emittedAssets ) . sort ( )
166
+ ) . toMatchSnapshot ( 'first emitted' ) ;
153
167
expect ( stats . compilation . warnings ) . toHaveLength ( 0 ) ;
154
168
expect ( stats . compilation . errors ) . toHaveLength ( 0 ) ;
155
169
@@ -179,7 +193,12 @@ describe('TestCache', () => {
179
193
return ;
180
194
}
181
195
182
- expect ( stats . compilation . emittedAssets . size ) . toBe ( 0 ) ;
196
+ expect ( Object . keys ( stats . compilation . assets ) . sort ( ) ) . toMatchSnapshot (
197
+ 'second'
198
+ ) ;
199
+ expect (
200
+ Array . from ( stats . compilation . emittedAssets ) . sort ( )
201
+ ) . toMatchSnapshot ( 'second emitted' ) ;
183
202
expect ( stats . compilation . warnings ) . toHaveLength ( 0 ) ;
184
203
expect ( stats . compilation . errors ) . toHaveLength ( 0 ) ;
185
204
@@ -248,7 +267,236 @@ describe('TestCache', () => {
248
267
return ;
249
268
}
250
269
251
- expect ( stats . compilation . emittedAssets . size ) . toBe ( 2 ) ;
270
+ expect ( Object . keys ( stats . compilation . assets ) . sort ( ) ) . toMatchSnapshot (
271
+ 'first'
272
+ ) ;
273
+ expect (
274
+ Array . from ( stats . compilation . emittedAssets ) . sort ( )
275
+ ) . toMatchSnapshot ( 'first emitted' ) ;
276
+ expect ( stats . compilation . warnings ) . toHaveLength ( 0 ) ;
277
+ expect ( stats . compilation . errors ) . toHaveLength ( 0 ) ;
278
+
279
+ compiler1 . close ( ( ) => {
280
+ resolve ( ) ;
281
+ } ) ;
282
+ } ) ;
283
+ } ) ;
284
+
285
+ const compiler2 = webpack ( {
286
+ ...webpackConfig ,
287
+ mode : 'development' ,
288
+ context : directoryForCase ,
289
+ cache : {
290
+ type : 'filesystem' ,
291
+ cacheDirectory : fileSystemCacheDirectory ,
292
+ idleTimeout : 0 ,
293
+ idleTimeoutForInitialStore : 0 ,
294
+ } ,
295
+ output : {
296
+ path : outputPath ,
297
+ } ,
298
+ } ) ;
299
+
300
+ await new Promise ( ( resolve , reject ) => {
301
+ compiler2 . run ( ( error , stats ) => {
302
+ if ( error ) {
303
+ reject ( error ) ;
304
+
305
+ return ;
306
+ }
307
+
308
+ expect ( Object . keys ( stats . compilation . assets ) . sort ( ) ) . toMatchSnapshot (
309
+ 'second'
310
+ ) ;
311
+ expect (
312
+ Array . from ( stats . compilation . emittedAssets ) . sort ( )
313
+ ) . toMatchSnapshot ( 'second emitted' ) ;
314
+ expect ( stats . compilation . warnings ) . toHaveLength ( 0 ) ;
315
+ expect ( stats . compilation . errors ) . toHaveLength ( 0 ) ;
316
+
317
+ compiler2 . close ( ( ) => {
318
+ resolve ( ) ;
319
+ } ) ;
320
+ } ) ;
321
+ } ) ;
322
+ } else {
323
+ expect ( true ) . toBe ( true ) ;
324
+ }
325
+ } ) ;
326
+
327
+ it ( 'should work with the "filesystem" cache and asset modules' , async ( ) => {
328
+ if ( webpack . version [ 0 ] !== '4' ) {
329
+ const originalRegister = webpack . util . serialization . register ;
330
+
331
+ webpack . util . serialization . register = jest
332
+ . fn ( )
333
+ . mockImplementation ( ( ...args ) => {
334
+ if ( args [ 1 ] . startsWith ( 'mini-css-extract-plugin' ) ) {
335
+ // eslint-disable-next-line no-param-reassign
336
+ args [ 1 ] = args [ 1 ] . replace ( / d i s t / , 'src' ) ;
337
+
338
+ return originalRegister ( ...args ) ;
339
+ }
340
+
341
+ return originalRegister ( ...args ) ;
342
+ } ) ;
343
+
344
+ const casesDirectory = path . resolve ( __dirname , 'cases' ) ;
345
+ const directoryForCase = path . resolve ( casesDirectory , 'asset-modules' ) ;
346
+ // eslint-disable-next-line import/no-dynamic-require, global-require
347
+ const webpackConfig = require ( path . resolve (
348
+ directoryForCase ,
349
+ 'webpack.config.js'
350
+ ) ) ;
351
+ const outputPath = path . resolve ( __dirname , 'js/cache-filesystem' ) ;
352
+ const fileSystemCacheDirectory = path . resolve (
353
+ __dirname ,
354
+ './js/.cache/type-filesystem'
355
+ ) ;
356
+
357
+ await del ( [ outputPath , fileSystemCacheDirectory ] ) ;
358
+
359
+ const compiler1 = webpack ( {
360
+ ...webpackConfig ,
361
+ mode : 'development' ,
362
+ context : directoryForCase ,
363
+ cache : {
364
+ type : 'filesystem' ,
365
+ cacheDirectory : fileSystemCacheDirectory ,
366
+ idleTimeout : 0 ,
367
+ idleTimeoutForInitialStore : 0 ,
368
+ } ,
369
+ output : {
370
+ path : outputPath ,
371
+ } ,
372
+ } ) ;
373
+
374
+ await new Promise ( ( resolve , reject ) => {
375
+ compiler1 . run ( ( error , stats ) => {
376
+ if ( error ) {
377
+ reject ( error ) ;
378
+
379
+ return ;
380
+ }
381
+
382
+ expect ( Object . keys ( stats . compilation . assets ) . sort ( ) ) . toMatchSnapshot (
383
+ 'first'
384
+ ) ;
385
+ expect (
386
+ Array . from ( stats . compilation . emittedAssets ) . sort ( )
387
+ ) . toMatchSnapshot ( 'first emitted' ) ;
388
+ expect ( stats . compilation . warnings ) . toHaveLength ( 0 ) ;
389
+ expect ( stats . compilation . errors ) . toHaveLength ( 0 ) ;
390
+
391
+ compiler1 . close ( ( ) => {
392
+ resolve ( ) ;
393
+ } ) ;
394
+ } ) ;
395
+ } ) ;
396
+
397
+ const compiler2 = webpack ( {
398
+ ...webpackConfig ,
399
+ mode : 'development' ,
400
+ context : directoryForCase ,
401
+ cache : {
402
+ type : 'filesystem' ,
403
+ cacheDirectory : fileSystemCacheDirectory ,
404
+ idleTimeout : 0 ,
405
+ idleTimeoutForInitialStore : 0 ,
406
+ } ,
407
+ output : {
408
+ path : outputPath ,
409
+ } ,
410
+ } ) ;
411
+
412
+ await new Promise ( ( resolve , reject ) => {
413
+ compiler2 . run ( ( error , stats ) => {
414
+ if ( error ) {
415
+ reject ( error ) ;
416
+
417
+ return ;
418
+ }
419
+
420
+ expect ( Object . keys ( stats . compilation . assets ) . sort ( ) ) . toMatchSnapshot (
421
+ 'second'
422
+ ) ;
423
+ expect (
424
+ Array . from ( stats . compilation . emittedAssets ) . sort ( )
425
+ ) . toMatchSnapshot ( 'second emitted' ) ;
426
+ expect ( stats . compilation . warnings ) . toHaveLength ( 0 ) ;
427
+ expect ( stats . compilation . errors ) . toHaveLength ( 0 ) ;
428
+
429
+ compiler2 . close ( ( ) => {
430
+ resolve ( ) ;
431
+ } ) ;
432
+ } ) ;
433
+ } ) ;
434
+ } else {
435
+ expect ( true ) . toBe ( true ) ;
436
+ }
437
+ } ) ;
438
+
439
+ it ( 'should work with the "filesystem" cache and file-loader' , async ( ) => {
440
+ if ( webpack . version [ 0 ] !== '4' ) {
441
+ const originalRegister = webpack . util . serialization . register ;
442
+
443
+ webpack . util . serialization . register = jest
444
+ . fn ( )
445
+ . mockImplementation ( ( ...args ) => {
446
+ if ( args [ 1 ] . startsWith ( 'mini-css-extract-plugin' ) ) {
447
+ // eslint-disable-next-line no-param-reassign
448
+ args [ 1 ] = args [ 1 ] . replace ( / d i s t / , 'src' ) ;
449
+
450
+ return originalRegister ( ...args ) ;
451
+ }
452
+
453
+ return originalRegister ( ...args ) ;
454
+ } ) ;
455
+
456
+ const casesDirectory = path . resolve ( __dirname , 'cases' ) ;
457
+ const directoryForCase = path . resolve ( casesDirectory , 'file-loader' ) ;
458
+ // eslint-disable-next-line import/no-dynamic-require, global-require
459
+ const webpackConfig = require ( path . resolve (
460
+ directoryForCase ,
461
+ 'webpack.config.js'
462
+ ) ) ;
463
+ const outputPath = path . resolve ( __dirname , 'js/cache-filesystem' ) ;
464
+ const fileSystemCacheDirectory = path . resolve (
465
+ __dirname ,
466
+ './js/.cache/type-filesystem'
467
+ ) ;
468
+
469
+ await del ( [ outputPath , fileSystemCacheDirectory ] ) ;
470
+
471
+ const compiler1 = webpack ( {
472
+ ...webpackConfig ,
473
+ mode : 'development' ,
474
+ context : directoryForCase ,
475
+ cache : {
476
+ type : 'filesystem' ,
477
+ cacheDirectory : fileSystemCacheDirectory ,
478
+ idleTimeout : 0 ,
479
+ idleTimeoutForInitialStore : 0 ,
480
+ } ,
481
+ output : {
482
+ path : outputPath ,
483
+ } ,
484
+ } ) ;
485
+
486
+ await new Promise ( ( resolve , reject ) => {
487
+ compiler1 . run ( ( error , stats ) => {
488
+ if ( error ) {
489
+ reject ( error ) ;
490
+
491
+ return ;
492
+ }
493
+
494
+ expect ( Object . keys ( stats . compilation . assets ) . sort ( ) ) . toMatchSnapshot (
495
+ 'first'
496
+ ) ;
497
+ expect (
498
+ Array . from ( stats . compilation . emittedAssets ) . sort ( )
499
+ ) . toMatchSnapshot ( 'first emitted' ) ;
252
500
expect ( stats . compilation . warnings ) . toHaveLength ( 0 ) ;
253
501
expect ( stats . compilation . errors ) . toHaveLength ( 0 ) ;
254
502
@@ -281,7 +529,12 @@ describe('TestCache', () => {
281
529
return ;
282
530
}
283
531
284
- expect ( stats . compilation . emittedAssets . size ) . toBe ( 0 ) ;
532
+ expect ( Object . keys ( stats . compilation . assets ) . sort ( ) ) . toMatchSnapshot (
533
+ 'second'
534
+ ) ;
535
+ expect (
536
+ Array . from ( stats . compilation . emittedAssets ) . sort ( )
537
+ ) . toMatchSnapshot ( 'second emitted' ) ;
285
538
expect ( stats . compilation . warnings ) . toHaveLength ( 0 ) ;
286
539
expect ( stats . compilation . errors ) . toHaveLength ( 0 ) ;
287
540
0 commit comments