-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathcache-option.test.js
341 lines (277 loc) · 11.2 KB
/
cache-option.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import cacache from 'cacache';
import findCacheDir from 'find-cache-dir';
import TerserPlugin from '../src/index';
import { createCompiler, compile, cleanErrorStack } from './helpers';
const cacheDir = findCacheDir({ name: 'terser-webpack-plugin' });
const otherCacheDir = findCacheDir({ name: 'other-cache-directory' });
describe('when applied with `cache` option', () => {
let compiler;
beforeEach(() => {
compiler = createCompiler({
entry: {
one: `${__dirname}/fixtures/cache.js`,
two: `${__dirname}/fixtures/cache-1.js`,
three: `${__dirname}/fixtures/cache-2.js`,
four: `${__dirname}/fixtures/cache-3.js`,
five: `${__dirname}/fixtures/cache-4.js`,
},
});
return Promise.all([
cacache.rm.all(cacheDir),
cacache.rm.all(otherCacheDir),
]);
});
afterEach(() =>
Promise.all([cacache.rm.all(cacheDir), cacache.rm.all(otherCacheDir)])
);
it('matches snapshot for `false` value', () => {
new TerserPlugin({ cache: false }).apply(compiler);
cacache.get = jest.fn(cacache.get);
cacache.put = jest.fn(cacache.put);
return compile(compiler).then((stats) => {
const errors = stats.compilation.errors.map(cleanErrorStack);
const warnings = stats.compilation.warnings.map(cleanErrorStack);
expect(errors).toMatchSnapshot('errors');
expect(warnings).toMatchSnapshot('warnings');
for (const file in stats.compilation.assets) {
if (
Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)
) {
expect(stats.compilation.assets[file].source()).toMatchSnapshot(file);
}
}
// Cache disabled so we don't run `get` or `put`
expect(cacache.get.mock.calls.length).toBe(0);
expect(cacache.put.mock.calls.length).toBe(0);
return Promise.resolve()
.then(() => cacache.ls(cacheDir))
.then((cacheEntriesList) => {
const cacheKeys = Object.keys(cacheEntriesList);
expect(cacheKeys.length).toBe(0);
});
});
});
it('matches snapshot for `true` value', () => {
new TerserPlugin({ cache: true }).apply(compiler);
cacache.get = jest.fn(cacache.get);
cacache.put = jest.fn(cacache.put);
return compile(compiler).then((stats) => {
const errors = stats.compilation.errors.map(cleanErrorStack);
const warnings = stats.compilation.warnings.map(cleanErrorStack);
expect(errors).toMatchSnapshot('errors');
expect(warnings).toMatchSnapshot('warnings');
for (const file in stats.compilation.assets) {
if (
Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)
) {
expect(stats.compilation.assets[file].source()).toMatchSnapshot(file);
}
}
const countAssets = Object.keys(stats.compilation.assets).length;
// Try to found cached files, but we don't have their in cache
expect(cacache.get.mock.calls.length).toBe(countAssets);
// Put files in cache
expect(cacache.put.mock.calls.length).toBe(countAssets);
return (
Promise.resolve()
.then(() => cacache.ls(cacheDir))
.then((cacheEntriesList) => {
const cacheKeys = Object.keys(cacheEntriesList);
// Make sure that we cached files
expect(cacheKeys.length).toBe(countAssets);
cacache.get.mockClear();
cacache.put.mockClear();
})
// Run second compilation to ensure cached files will be taken from cache
.then(() => compile(compiler))
.then((newStats) => {
const newErrors = newStats.compilation.errors.map(cleanErrorStack);
const newWarnings = newStats.compilation.warnings.map(
cleanErrorStack
);
expect(newErrors).toMatchSnapshot('errors');
expect(newWarnings).toMatchSnapshot('warnings');
for (const file in newStats.compilation.assets) {
if (
Object.prototype.hasOwnProperty.call(
newStats.compilation.assets,
file
)
) {
expect(
newStats.compilation.assets[file].source()
).toMatchSnapshot(file);
}
}
const newCountAssets = Object.keys(newStats.compilation.assets)
.length;
// Now we have cached files so we get their and don't put
expect(cacache.get.mock.calls.length).toBe(newCountAssets);
expect(cacache.put.mock.calls.length).toBe(0);
})
);
});
});
it('matches snapshot for `other-cache-directory` value (string)', () => {
new TerserPlugin({ cache: otherCacheDir }).apply(compiler);
cacache.get = jest.fn(cacache.get);
cacache.put = jest.fn(cacache.put);
return compile(compiler).then((stats) => {
const errors = stats.compilation.errors.map(cleanErrorStack);
const warnings = stats.compilation.warnings.map(cleanErrorStack);
expect(errors).toMatchSnapshot('errors');
expect(warnings).toMatchSnapshot('warnings');
for (const file in stats.compilation.assets) {
if (
Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)
) {
expect(stats.compilation.assets[file].source()).toMatchSnapshot(file);
}
}
const countAssets = Object.keys(stats.compilation.assets).length;
// Try to found cached files, but we don't have their in cache
expect(cacache.get.mock.calls.length).toBe(countAssets);
// Put files in cache
expect(cacache.put.mock.calls.length).toBe(countAssets);
return (
Promise.resolve()
.then(() => cacache.ls(otherCacheDir))
.then((cacheEntriesList) => {
const cacheKeys = Object.keys(cacheEntriesList);
// Make sure that we cached files
expect(cacheKeys.length).toBe(countAssets);
cacache.get.mockClear();
cacache.put.mockClear();
})
// Run second compilation to ensure cached files will be taken from cache
.then(() => compile(compiler))
.then((newStats) => {
const newErrors = newStats.compilation.errors.map(cleanErrorStack);
const newWarnings = newStats.compilation.warnings.map(
cleanErrorStack
);
expect(newErrors).toMatchSnapshot('errors');
expect(newWarnings).toMatchSnapshot('warnings');
for (const file in newStats.compilation.assets) {
if (
Object.prototype.hasOwnProperty.call(
newStats.compilation.assets,
file
)
) {
expect(
newStats.compilation.assets[file].source()
).toMatchSnapshot(file);
}
}
const newCountAssets = Object.keys(newStats.compilation.assets)
.length;
// Now we have cached files so we get their and don't put
expect(cacache.get.mock.calls.length).toBe(newCountAssets);
expect(cacache.put.mock.calls.length).toBe(0);
})
);
});
});
it('matches snapshot for `true` value and `cacheKey` is custom `function`', () => {
new TerserPlugin({
cache: true,
cacheKeys: (defaultCacheKeys, file) => {
// eslint-disable-next-line no-param-reassign
defaultCacheKeys.myCacheKey = 1;
// eslint-disable-next-line no-param-reassign
defaultCacheKeys.myCacheKeyBasedOnFile = `file-${file}`;
return defaultCacheKeys;
},
}).apply(compiler);
cacache.get = jest.fn(cacache.get);
cacache.put = jest.fn(cacache.put);
return compile(compiler).then((stats) => {
const errors = stats.compilation.errors.map(cleanErrorStack);
const warnings = stats.compilation.warnings.map(cleanErrorStack);
expect(errors).toMatchSnapshot('errors');
expect(warnings).toMatchSnapshot('warnings');
for (const file in stats.compilation.assets) {
if (
Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)
) {
expect(stats.compilation.assets[file].source()).toMatchSnapshot(file);
}
}
const countAssets = Object.keys(stats.compilation.assets).length;
// Try to found cached files, but we don't have their in cache
expect(cacache.get.mock.calls.length).toBe(countAssets);
// Put files in cache
expect(cacache.put.mock.calls.length).toBe(countAssets);
return (
Promise.resolve()
.then(() => cacache.ls(cacheDir))
.then((cacheEntriesList) => {
const cacheKeys = Object.keys(cacheEntriesList);
// Make sure that we cached files
expect(cacheKeys.length).toBe(countAssets);
cacheKeys.forEach((cacheEntry) => {
// eslint-disable-next-line no-new-func
const cacheEntryOptions = new Function(
`'use strict'\nreturn ${cacheEntry}`
)();
expect(cacheEntryOptions.myCacheKey).toBe(1);
expect(cacheEntryOptions.myCacheKeyBasedOnFile).toMatch(
/file-(.+)?\.js/
);
});
cacache.get.mockClear();
cacache.put.mockClear();
})
// Run second compilation to ensure cached files will be taken from cache
.then(() => compile(compiler))
.then((newStats) => {
const newErrors = newStats.compilation.errors.map(cleanErrorStack);
const newWarnings = newStats.compilation.warnings.map(
cleanErrorStack
);
expect(newErrors).toMatchSnapshot('errors');
expect(newWarnings).toMatchSnapshot('warnings');
for (const file in newStats.compilation.assets) {
if (
Object.prototype.hasOwnProperty.call(
newStats.compilation.assets,
file
)
) {
expect(
newStats.compilation.assets[file].source()
).toMatchSnapshot(file);
}
}
const newCountAssets = Object.keys(newStats.compilation.assets)
.length;
// Now we have cached files so we get their and don't put
expect(cacache.get.mock.calls.length).toBe(newCountAssets);
expect(cacache.put.mock.calls.length).toBe(0);
})
);
});
});
it('matches snapshot for errors into `cacheKeys` option', () => {
new TerserPlugin({
cache: true,
cacheKeys: () => {
throw new Error('message');
},
}).apply(compiler);
return compile(compiler).then((stats) => {
const errors = stats.compilation.errors.map(cleanErrorStack);
const warnings = stats.compilation.warnings.map(cleanErrorStack);
expect(errors).toMatchSnapshot('errors');
expect(warnings).toMatchSnapshot('warnings');
for (const file in stats.compilation.assets) {
if (
Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)
) {
expect(stats.compilation.assets[file].source()).toMatchSnapshot(file);
}
}
});
});
});