11import cacache from 'cacache' ;
22import findCacheDir from 'find-cache-dir' ;
33
4+ import TaskRunner from '../src/TaskRunner' ;
45import TerserPlugin from '../src/index' ;
56
6- import { createCompiler , compile , cleanErrorStack , getAssets } from './helpers' ;
7-
8- const cacheDir = findCacheDir ( { name : 'terser-webpack-plugin' } ) ;
7+ import {
8+ createCompiler ,
9+ compile ,
10+ cleanErrorStack ,
11+ getAssets ,
12+ removeCache ,
13+ } from './helpers' ;
14+
15+ const uniqueCacheDirectory = findCacheDir ( { name : 'unique-cache-directory' } ) ;
16+ const uniqueOtherDirectory = findCacheDir ( {
17+ name : 'unique-other-cache-directory' ,
18+ } ) ;
919const otherCacheDir = findCacheDir ( { name : 'other-cache-directory' } ) ;
20+ const otherOtherCacheDir = findCacheDir ( {
21+ name : 'other-other-cache-directory' ,
22+ } ) ;
23+
24+ jest . setTimeout ( 30000 ) ;
1025
1126describe ( 'cache option' , ( ) => {
1227 let compiler ;
1328
1429 beforeEach ( ( ) => {
30+ jest . clearAllMocks ( ) ;
31+
1532 compiler = createCompiler ( {
1633 entry : {
1734 one : `${ __dirname } /fixtures/cache.js` ,
@@ -23,20 +40,33 @@ describe('cache option', () => {
2340 } ) ;
2441
2542 return Promise . all ( [
26- cacache . rm . all ( cacheDir ) ,
27- cacache . rm . all ( otherCacheDir ) ,
43+ removeCache ( ) ,
44+ removeCache ( uniqueCacheDirectory ) ,
45+ removeCache ( uniqueOtherDirectory ) ,
46+ removeCache ( otherCacheDir ) ,
47+ removeCache ( otherOtherCacheDir ) ,
2848 ] ) ;
2949 } ) ;
3050
31- afterEach ( ( ) =>
32- Promise . all ( [ cacache . rm . all ( cacheDir ) , cacache . rm . all ( otherCacheDir ) ] )
33- ) ;
51+ afterEach ( ( ) => {
52+ return Promise . all ( [
53+ removeCache ( ) ,
54+ removeCache ( uniqueCacheDirectory ) ,
55+ removeCache ( uniqueOtherDirectory ) ,
56+ removeCache ( otherCacheDir ) ,
57+ removeCache ( otherOtherCacheDir ) ,
58+ ] ) ;
59+ } ) ;
3460
35- it ( 'should match snapshot for the "false" value' , async ( ) => {
36- new TerserPlugin ( { cache : false } ) . apply ( compiler ) ;
61+ it ( 'should match snapshot when a value is not specify' , async ( ) => {
62+ const cacacheGetSpy = jest . spyOn ( cacache , 'get' ) ;
63+ const cacachePutSpy = jest . spyOn ( cacache , 'put' ) ;
3764
38- cacache . get = jest . fn ( cacache . get ) ;
39- cacache . put = jest . fn ( cacache . put ) ;
65+ jest . spyOn ( TaskRunner , 'getCacheDirectory' ) . mockImplementation ( ( ) => {
66+ return uniqueCacheDirectory ;
67+ } ) ;
68+
69+ new TerserPlugin ( { cache : true } ) . apply ( compiler ) ;
4070
4171 const stats = await compile ( compiler ) ;
4272
@@ -47,21 +77,62 @@ describe('cache option', () => {
4777 expect ( warnings ) . toMatchSnapshot ( 'warnings' ) ;
4878 expect ( getAssets ( stats , compiler ) ) . toMatchSnapshot ( 'assets' ) ;
4979
50- // Cache disabled so we don't run `get` or `put`
51- expect ( cacache . get . mock . calls . length ) . toBe ( 0 ) ;
52- expect ( cacache . put . mock . calls . length ) . toBe ( 0 ) ;
80+ const countAssets = Object . keys ( stats . compilation . assets ) . length ;
81+
82+ // Try to found cached files, but we don't have their in cache
83+ expect ( cacacheGetSpy ) . toHaveBeenCalledTimes ( countAssets ) ;
84+ // Put files in cache
85+ expect ( cacachePutSpy ) . toHaveBeenCalledTimes ( countAssets ) ;
86+
87+ cacache . get . mockClear ( ) ;
88+ cacache . put . mockClear ( ) ;
89+
90+ const newStats = await compile ( compiler ) ;
91+
92+ const newErrors = newStats . compilation . errors . map ( cleanErrorStack ) ;
93+ const newWarnings = newStats . compilation . warnings . map ( cleanErrorStack ) ;
94+
95+ expect ( newErrors ) . toMatchSnapshot ( 'errors' ) ;
96+ expect ( newWarnings ) . toMatchSnapshot ( 'warnings' ) ;
97+
98+ expect ( getAssets ( newStats , compiler ) ) . toMatchSnapshot ( 'assets' ) ;
99+
100+ const newCountAssets = Object . keys ( newStats . compilation . assets ) . length ;
101+
102+ // Now we have cached files so we get them and don't put new
103+ expect ( cacacheGetSpy ) . toHaveBeenCalledTimes ( newCountAssets ) ;
104+ expect ( cacachePutSpy ) . toHaveBeenCalledTimes ( 0 ) ;
105+ } ) ;
106+
107+ it ( 'should match snapshot for the "false" value' , async ( ) => {
108+ const cacacheGetSpy = jest . spyOn ( cacache , 'get' ) ;
109+ const cacachePutSpy = jest . spyOn ( cacache , 'put' ) ;
110+
111+ new TerserPlugin ( { cache : false } ) . apply ( compiler ) ;
112+
113+ const stats = await compile ( compiler ) ;
114+
115+ const errors = stats . compilation . errors . map ( cleanErrorStack ) ;
116+ const warnings = stats . compilation . warnings . map ( cleanErrorStack ) ;
53117
54- const cacheEntriesList = await cacache . ls ( cacheDir ) ;
55- const cacheKeys = Object . keys ( cacheEntriesList ) ;
118+ expect ( errors ) . toMatchSnapshot ( 'errors' ) ;
119+ expect ( warnings ) . toMatchSnapshot ( 'warnings' ) ;
120+ expect ( getAssets ( stats , compiler ) ) . toMatchSnapshot ( 'assets' ) ;
56121
57- expect ( cacheKeys . length ) . toBe ( 0 ) ;
122+ // Cache disabled so we don't run `get` or `put`
123+ expect ( cacacheGetSpy ) . toHaveBeenCalledTimes ( 0 ) ;
124+ expect ( cacachePutSpy ) . toHaveBeenCalledTimes ( 0 ) ;
58125 } ) ;
59126
60127 it ( 'should match snapshot for the "true" value' , async ( ) => {
61- new TerserPlugin ( { cache : true } ) . apply ( compiler ) ;
128+ const cacacheGetSpy = jest . spyOn ( cacache , 'get' ) ;
129+ const cacachePutSpy = jest . spyOn ( cacache , 'put' ) ;
130+
131+ jest . spyOn ( TaskRunner , 'getCacheDirectory' ) . mockImplementation ( ( ) => {
132+ return uniqueOtherDirectory ;
133+ } ) ;
62134
63- cacache . get = jest . fn ( cacache . get ) ;
64- cacache . put = jest . fn ( cacache . put ) ;
135+ new TerserPlugin ( { cache : true } ) . apply ( compiler ) ;
65136
66137 const stats = await compile ( compiler ) ;
67138
@@ -75,16 +146,9 @@ describe('cache option', () => {
75146 const countAssets = Object . keys ( stats . compilation . assets ) . length ;
76147
77148 // Try to found cached files, but we don't have their in cache
78- expect ( cacache . get . mock . calls . length ) . toBe ( countAssets ) ;
149+ expect ( cacacheGetSpy ) . toHaveBeenCalledTimes ( countAssets ) ;
79150 // Put files in cache
80- expect ( cacache . put . mock . calls . length ) . toBe ( countAssets ) ;
81-
82- const cacheEntriesList = await cacache . ls ( cacheDir ) ;
83-
84- const cacheKeys = Object . keys ( cacheEntriesList ) ;
85-
86- // Make sure that we cached files
87- expect ( cacheKeys . length ) . toBe ( countAssets ) ;
151+ expect ( cacachePutSpy ) . toHaveBeenCalledTimes ( countAssets ) ;
88152
89153 cacache . get . mockClear ( ) ;
90154 cacache . put . mockClear ( ) ;
@@ -102,15 +166,15 @@ describe('cache option', () => {
102166 const newCountAssets = Object . keys ( newStats . compilation . assets ) . length ;
103167
104168 // Now we have cached files so we get them and don't put new
105- expect ( cacache . get . mock . calls . length ) . toBe ( newCountAssets ) ;
106- expect ( cacache . put . mock . calls . length ) . toBe ( 0 ) ;
169+ expect ( cacacheGetSpy ) . toHaveBeenCalledTimes ( newCountAssets ) ;
170+ expect ( cacachePutSpy ) . toHaveBeenCalledTimes ( 0 ) ;
107171 } ) ;
108172
109173 it ( 'should match snapshot for the "other-cache-directory" value' , async ( ) => {
110- new TerserPlugin ( { cache : otherCacheDir } ) . apply ( compiler ) ;
174+ const cacacheGetSpy = jest . spyOn ( cacache , 'get' ) ;
175+ const cacachePutSpy = jest . spyOn ( cacache , 'put' ) ;
111176
112- cacache . get = jest . fn ( cacache . get ) ;
113- cacache . put = jest . fn ( cacache . put ) ;
177+ new TerserPlugin ( { cache : otherCacheDir } ) . apply ( compiler ) ;
114178
115179 const stats = await compile ( compiler ) ;
116180
@@ -124,15 +188,9 @@ describe('cache option', () => {
124188 const countAssets = Object . keys ( stats . compilation . assets ) . length ;
125189
126190 // Try to found cached files, but we don't have their in cache
127- expect ( cacache . get . mock . calls . length ) . toBe ( countAssets ) ;
191+ expect ( cacacheGetSpy ) . toHaveBeenCalledTimes ( countAssets ) ;
128192 // Put files in cache
129- expect ( cacache . put . mock . calls . length ) . toBe ( countAssets ) ;
130-
131- const cacheEntriesList = await cacache . ls ( otherCacheDir ) ;
132- const cacheKeys = Object . keys ( cacheEntriesList ) ;
133-
134- // Make sure that we cached files
135- expect ( cacheKeys . length ) . toBe ( countAssets ) ;
193+ expect ( cacachePutSpy ) . toHaveBeenCalledTimes ( countAssets ) ;
136194
137195 cacache . get . mockClear ( ) ;
138196 cacache . put . mockClear ( ) ;
@@ -149,14 +207,17 @@ describe('cache option', () => {
149207
150208 const newCountAssets = Object . keys ( newStats . compilation . assets ) . length ;
151209
152- // Now we have cached files so we get their and don't put
153- expect ( cacache . get . mock . calls . length ) . toBe ( newCountAssets ) ;
154- expect ( cacache . put . mock . calls . length ) . toBe ( 0 ) ;
210+ // Now we have cached files so we get them and don't put new
211+ expect ( cacacheGetSpy ) . toHaveBeenCalledTimes ( newCountAssets ) ;
212+ expect ( cacachePutSpy ) . toHaveBeenCalledTimes ( 0 ) ;
155213 } ) ;
156214
157- it ( 'should match snapshot for the "true" value when "cacheKey" is custom "function"' , async ( ) => {
215+ it ( 'should match snapshot when "cacheKey" is custom "function"' , async ( ) => {
216+ const cacacheGetSpy = jest . spyOn ( cacache , 'get' ) ;
217+ const cacachePutSpy = jest . spyOn ( cacache , 'put' ) ;
218+
158219 new TerserPlugin ( {
159- cache : true ,
220+ cache : otherOtherCacheDir ,
160221 cacheKeys : ( defaultCacheKeys , file ) => {
161222 // eslint-disable-next-line no-param-reassign
162223 defaultCacheKeys . myCacheKey = 1 ;
@@ -167,9 +228,6 @@ describe('cache option', () => {
167228 } ,
168229 } ) . apply ( compiler ) ;
169230
170- cacache . get = jest . fn ( cacache . get ) ;
171- cacache . put = jest . fn ( cacache . put ) ;
172-
173231 const stats = await compile ( compiler ) ;
174232
175233 const errors = stats . compilation . errors . map ( cleanErrorStack ) ;
@@ -182,25 +240,9 @@ describe('cache option', () => {
182240 const countAssets = Object . keys ( stats . compilation . assets ) . length ;
183241
184242 // Try to found cached files, but we don't have their in cache
185- expect ( cacache . get . mock . calls . length ) . toBe ( countAssets ) ;
243+ expect ( cacacheGetSpy ) . toHaveBeenCalledTimes ( countAssets ) ;
186244 // Put files in cache
187- expect ( cacache . put . mock . calls . length ) . toBe ( countAssets ) ;
188-
189- const cacheEntriesList = await cacache . ls ( cacheDir ) ;
190- const cacheKeys = Object . keys ( cacheEntriesList ) ;
191-
192- // Make sure that we cached files
193- expect ( cacheKeys . length ) . toBe ( countAssets ) ;
194-
195- cacheKeys . forEach ( ( cacheEntry ) => {
196- // eslint-disable-next-line no-new-func
197- const cacheEntryOptions = new Function (
198- `'use strict'\nreturn ${ cacheEntry } `
199- ) ( ) ;
200-
201- expect ( cacheEntryOptions . myCacheKey ) . toBe ( 1 ) ;
202- expect ( cacheEntryOptions . myCacheKeyBasedOnFile ) . toMatch ( / f i l e - ( .+ ) ? \. j s / ) ;
203- } ) ;
245+ expect ( cacachePutSpy ) . toHaveBeenCalledTimes ( countAssets ) ;
204246
205247 cacache . get . mockClear ( ) ;
206248 cacache . put . mockClear ( ) ;
@@ -216,9 +258,9 @@ describe('cache option', () => {
216258
217259 const newCountAssets = Object . keys ( newStats . compilation . assets ) . length ;
218260
219- // Now we have cached files so we get their and don't put
220- expect ( cacache . get . mock . calls . length ) . toBe ( newCountAssets ) ;
221- expect ( cacache . put . mock . calls . length ) . toBe ( 0 ) ;
261+ // Now we have cached files so we get them and don't put new
262+ expect ( cacacheGetSpy ) . toHaveBeenCalledTimes ( newCountAssets ) ;
263+ expect ( cacachePutSpy ) . toHaveBeenCalledTimes ( 0 ) ;
222264 } ) ;
223265
224266 it ( 'should match snapshot for errors into the "cacheKeys" option' , async ( ) => {
0 commit comments