diff --git a/src/index.js b/src/index.js index d4176ca..d292fdf 100644 --- a/src/index.js +++ b/src/index.js @@ -26,7 +26,7 @@ class CompressionPlugin { test, include, exclude, - cache = false, + cache = true, algorithm = 'gzip', compressionOptions = {}, filename = '[path].gz[query]', diff --git a/test/CompressionPlugin.test.js b/test/CompressionPlugin.test.js index fc6881f..8f5207a 100644 --- a/test/CompressionPlugin.test.js +++ b/test/CompressionPlugin.test.js @@ -6,9 +6,14 @@ import { getCompiler, getErrors, getWarnings, + removeCache, } from './helpers/index'; describe('CompressionPlugin', () => { + beforeEach(() => { + return removeCache(); + }); + it('should work', async () => { const compiler = getCompiler( './entry.js', diff --git a/test/algorithm.test.js b/test/algorithm.test.js index 719c79f..9ae8d6e 100644 --- a/test/algorithm.test.js +++ b/test/algorithm.test.js @@ -6,6 +6,7 @@ import { getCompiler, getErrors, getWarnings, + removeCache, } from './helpers/index'; describe('"algorithm" option', () => { @@ -13,6 +14,8 @@ describe('"algorithm" option', () => { beforeEach(() => { compiler = getCompiler('./entry.js'); + + return removeCache(); }); it('matches snapshot for `unknown` value ({String})', () => { diff --git a/test/cache-option.test.js b/test/cache-option.test.js index 362f6d9..bf75edc 100644 --- a/test/cache-option.test.js +++ b/test/cache-option.test.js @@ -6,6 +6,7 @@ import findCacheDir from 'find-cache-dir'; import del from 'del'; import CompressionPlugin from '../src/index'; +import Webpack4Cache from '../src/Webpack4Cache'; import { compile, @@ -17,6 +18,7 @@ import { const cacheDir = findCacheDir({ name: 'compression-webpack-plugin' }); const otherCacheDir = findCacheDir({ name: 'other-cache-directory' }); +const uniqueDirectory = findCacheDir({ name: 'unique-cache-directory' }); if (getCompiler.isWebpack4()) { describe('"cache" option', () => { @@ -24,6 +26,7 @@ if (getCompiler.isWebpack4()) { return Promise.all([ cacache.rm.all(cacheDir), cacache.rm.all(otherCacheDir), + cacache.rm.all(uniqueDirectory), ]); }); @@ -65,6 +68,12 @@ if (getCompiler.isWebpack4()) { cacache.get = jest.fn(cacache.get); cacache.put = jest.fn(cacache.put); + const getCacheDirectorySpy = jest + .spyOn(Webpack4Cache, 'getCacheDirectory') + .mockImplementation(() => { + return uniqueDirectory; + }); + const stats = await compile(beforeCacheCompiler); expect(getAssetsNameAndSize(stats)).toMatchSnapshot('assets'); @@ -78,7 +87,7 @@ if (getCompiler.isWebpack4()) { // Put files in cache expect(cacache.put.mock.calls.length).toBe(countAssets / 2); - const cacheEntriesList = await cacache.ls(cacheDir); + const cacheEntriesList = await cacache.ls(uniqueDirectory); const cacheKeys = Object.keys(cacheEntriesList); @@ -117,6 +126,8 @@ if (getCompiler.isWebpack4()) { // Now we have cached files so we get their and don't put expect(cacache.get.mock.calls.length).toBe(newCountAssets / 2); expect(cacache.put.mock.calls.length).toBe(0); + + getCacheDirectorySpy.mockRestore(); }); it('matches snapshot for `other-cache-directory` value ({String})', async () => { diff --git a/test/compressionOptions-option.test.js b/test/compressionOptions-option.test.js index 25684e9..f3939c5 100644 --- a/test/compressionOptions-option.test.js +++ b/test/compressionOptions-option.test.js @@ -6,6 +6,7 @@ import { getCompiler, getErrors, getWarnings, + removeCache, } from './helpers/index'; describe('"compressionOptions" option', () => { @@ -13,6 +14,8 @@ describe('"compressionOptions" option', () => { beforeEach(() => { compiler = getCompiler('./entry.js'); + + return removeCache(); }); it('matches snapshot without values', async () => { diff --git a/test/deleteOriginalAssets.test.js b/test/deleteOriginalAssets.test.js index 59d304d..77afd73 100644 --- a/test/deleteOriginalAssets.test.js +++ b/test/deleteOriginalAssets.test.js @@ -6,6 +6,7 @@ import { getCompiler, getErrors, getWarnings, + removeCache, } from './helpers/index'; describe('"deleteOriginalAssets" option', () => { @@ -13,6 +14,8 @@ describe('"deleteOriginalAssets" option', () => { beforeEach(() => { compiler = getCompiler('./entry.js'); + + return removeCache(); }); it('matches snapshot for `true` value ({Boolean})', async () => { diff --git a/test/exclude-option.test.js b/test/exclude-option.test.js index 3733e58..b4474c0 100644 --- a/test/exclude-option.test.js +++ b/test/exclude-option.test.js @@ -6,6 +6,7 @@ import { getCompiler, getErrors, getWarnings, + removeCache, } from './helpers/index'; describe('"exclude" option', () => { @@ -23,6 +24,8 @@ describe('"exclude" option', () => { }, } ); + + return removeCache(); }); it('matches snapshot for a single `exclude` value ({RegExp})', async () => { diff --git a/test/filename-option.test.js b/test/filename-option.test.js index 38ea5ee..f1d72f1 100644 --- a/test/filename-option.test.js +++ b/test/filename-option.test.js @@ -6,6 +6,7 @@ import { getCompiler, getErrors, getWarnings, + removeCache, } from './helpers/index'; describe('"filename" option', () => { @@ -23,6 +24,8 @@ describe('"filename" option', () => { }, } ); + + return removeCache(); }); it('matches snapshot for `[path].super-compressed.gz[query]` value ({String})', async () => { diff --git a/test/helpers/getCacheDirectory.js b/test/helpers/getCacheDirectory.js new file mode 100644 index 0000000..b8394fa --- /dev/null +++ b/test/helpers/getCacheDirectory.js @@ -0,0 +1,9 @@ +import os from 'os'; + +import findCacheDir from 'find-cache-dir'; + +function getCacheDirectory() { + return findCacheDir({ name: 'compression-webpack-plugin' }) || os.tmpdir(); +} + +export default getCacheDirectory; diff --git a/test/helpers/index.js b/test/helpers/index.js index 16d05e3..a1b33a8 100644 --- a/test/helpers/index.js +++ b/test/helpers/index.js @@ -7,6 +7,7 @@ import getWarnings from './getWarnings'; import normalizeErrors from './normalizeErrors'; import readAsset from './readAsset'; import readsAssets from './readAssets'; +import removeCache from './removeCache'; export { compile, @@ -18,4 +19,5 @@ export { normalizeErrors, readAsset, readsAssets, + removeCache, }; diff --git a/test/helpers/removeCache.js b/test/helpers/removeCache.js new file mode 100644 index 0000000..05c2f81 --- /dev/null +++ b/test/helpers/removeCache.js @@ -0,0 +1,11 @@ +import cacache from 'cacache'; + +import getCacheDirectory from './getCacheDirectory'; + +async function removeCache(cacheDirectory) { + const cacheDir = cacheDirectory || getCacheDirectory(); + + return cacache.rm.all(cacheDir); +} + +export default removeCache; diff --git a/test/include-option.test.js b/test/include-option.test.js index 9d06b96..30f21f3 100644 --- a/test/include-option.test.js +++ b/test/include-option.test.js @@ -6,6 +6,7 @@ import { getCompiler, getErrors, getWarnings, + removeCache, } from './helpers/index'; describe('"include" option', () => { @@ -23,6 +24,8 @@ describe('"include" option', () => { }, } ); + + return removeCache(); }); it('matches snapshot for a single `include` value ({RegExp})', async () => { diff --git a/test/minRatio-option.test.js b/test/minRatio-option.test.js index 28f5611..bbb6b41 100644 --- a/test/minRatio-option.test.js +++ b/test/minRatio-option.test.js @@ -6,6 +6,7 @@ import { getCompiler, getErrors, getWarnings, + removeCache, } from './helpers/index'; describe('"minRatio" option', () => { @@ -13,6 +14,8 @@ describe('"minRatio" option', () => { beforeEach(() => { compiler = getCompiler('./entry.js'); + + return removeCache(); }); it('matches snapshot for `0` value ({Number})', async () => { diff --git a/test/test-option.test.js b/test/test-option.test.js index a9c1629..d349384 100644 --- a/test/test-option.test.js +++ b/test/test-option.test.js @@ -6,6 +6,7 @@ import { getCompiler, getErrors, getWarnings, + removeCache, } from './helpers/index'; describe('"test" option', () => { @@ -23,6 +24,8 @@ describe('"test" option', () => { }, } ); + + return removeCache(); }); it('matches snapshot with empty `test` value', async () => { diff --git a/test/threshold-option.test.js b/test/threshold-option.test.js index 67590bd..6f677e9 100644 --- a/test/threshold-option.test.js +++ b/test/threshold-option.test.js @@ -6,6 +6,7 @@ import { getCompiler, getErrors, getWarnings, + removeCache, } from './helpers/index'; describe('"threshold" option', () => { @@ -13,6 +14,8 @@ describe('"threshold" option', () => { beforeEach(() => { compiler = getCompiler('./entry.js'); + + return removeCache(); }); it('matches snapshot for `0` value ({Number})', async () => { diff --git a/test/validate-options.test.js b/test/validate-options.test.js index 2bcc6f0..07cb76e 100644 --- a/test/validate-options.test.js +++ b/test/validate-options.test.js @@ -1,6 +1,12 @@ import CompressionPlugin from '../src'; +import { removeCache } from './helpers'; + describe('validate options', () => { + beforeEach(() => { + return removeCache(); + }); + const tests = { test: { success: [