1
1
import cacache from 'cacache' ;
2
2
import findCacheDir from 'find-cache-dir' ;
3
3
4
+ import TaskRunner from '../src/TaskRunner' ;
4
5
import TerserPlugin from '../src/index' ;
5
6
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
+ } ) ;
9
19
const otherCacheDir = findCacheDir ( { name : 'other-cache-directory' } ) ;
20
+ const otherOtherCacheDir = findCacheDir ( {
21
+ name : 'other-other-cache-directory' ,
22
+ } ) ;
23
+
24
+ jest . setTimeout ( 30000 ) ;
10
25
11
26
describe ( 'cache option' , ( ) => {
12
27
let compiler ;
13
28
14
29
beforeEach ( ( ) => {
30
+ jest . clearAllMocks ( ) ;
31
+
15
32
compiler = createCompiler ( {
16
33
entry : {
17
34
one : `${ __dirname } /fixtures/cache.js` ,
@@ -23,20 +40,33 @@ describe('cache option', () => {
23
40
} ) ;
24
41
25
42
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 ) ,
28
48
] ) ;
29
49
} ) ;
30
50
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
+ } ) ;
34
60
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' ) ;
37
64
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 ) ;
40
70
41
71
const stats = await compile ( compiler ) ;
42
72
@@ -47,21 +77,62 @@ describe('cache option', () => {
47
77
expect ( warnings ) . toMatchSnapshot ( 'warnings' ) ;
48
78
expect ( getAssets ( stats , compiler ) ) . toMatchSnapshot ( 'assets' ) ;
49
79
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 ) ;
53
117
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' ) ;
56
121
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 ) ;
58
125
} ) ;
59
126
60
127
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
+ } ) ;
62
134
63
- cacache . get = jest . fn ( cacache . get ) ;
64
- cacache . put = jest . fn ( cacache . put ) ;
135
+ new TerserPlugin ( { cache : true } ) . apply ( compiler ) ;
65
136
66
137
const stats = await compile ( compiler ) ;
67
138
@@ -75,16 +146,9 @@ describe('cache option', () => {
75
146
const countAssets = Object . keys ( stats . compilation . assets ) . length ;
76
147
77
148
// 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 ) ;
79
150
// 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 ) ;
88
152
89
153
cacache . get . mockClear ( ) ;
90
154
cacache . put . mockClear ( ) ;
@@ -102,15 +166,15 @@ describe('cache option', () => {
102
166
const newCountAssets = Object . keys ( newStats . compilation . assets ) . length ;
103
167
104
168
// 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 ) ;
107
171
} ) ;
108
172
109
173
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' ) ;
111
176
112
- cacache . get = jest . fn ( cacache . get ) ;
113
- cacache . put = jest . fn ( cacache . put ) ;
177
+ new TerserPlugin ( { cache : otherCacheDir } ) . apply ( compiler ) ;
114
178
115
179
const stats = await compile ( compiler ) ;
116
180
@@ -124,15 +188,9 @@ describe('cache option', () => {
124
188
const countAssets = Object . keys ( stats . compilation . assets ) . length ;
125
189
126
190
// 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 ) ;
128
192
// 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 ) ;
136
194
137
195
cacache . get . mockClear ( ) ;
138
196
cacache . put . mockClear ( ) ;
@@ -149,14 +207,17 @@ describe('cache option', () => {
149
207
150
208
const newCountAssets = Object . keys ( newStats . compilation . assets ) . length ;
151
209
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 ) ;
155
213
} ) ;
156
214
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
+
158
219
new TerserPlugin ( {
159
- cache : true ,
220
+ cache : otherOtherCacheDir ,
160
221
cacheKeys : ( defaultCacheKeys , file ) => {
161
222
// eslint-disable-next-line no-param-reassign
162
223
defaultCacheKeys . myCacheKey = 1 ;
@@ -167,9 +228,6 @@ describe('cache option', () => {
167
228
} ,
168
229
} ) . apply ( compiler ) ;
169
230
170
- cacache . get = jest . fn ( cacache . get ) ;
171
- cacache . put = jest . fn ( cacache . put ) ;
172
-
173
231
const stats = await compile ( compiler ) ;
174
232
175
233
const errors = stats . compilation . errors . map ( cleanErrorStack ) ;
@@ -182,25 +240,9 @@ describe('cache option', () => {
182
240
const countAssets = Object . keys ( stats . compilation . assets ) . length ;
183
241
184
242
// 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 ) ;
186
244
// 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 ) ;
204
246
205
247
cacache . get . mockClear ( ) ;
206
248
cacache . put . mockClear ( ) ;
@@ -216,9 +258,9 @@ describe('cache option', () => {
216
258
217
259
const newCountAssets = Object . keys ( newStats . compilation . assets ) . length ;
218
260
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 ) ;
222
264
} ) ;
223
265
224
266
it ( 'should match snapshot for errors into the "cacheKeys" option' , async ( ) => {
0 commit comments