From 1e12a84450e6a66faeb1f55cc43932f0794cb2a8 Mon Sep 17 00:00:00 2001 From: Evilebot Tnawi Date: Fri, 6 Dec 2019 15:39:13 +0300 Subject: [PATCH] test: refactor (#192) --- .editorconfig | 3 +- azure-pipelines.yml | 70 ++-- lint-staged.config.js | 2 +- package-lock.json | 56 ++- package.json | 30 +- test/TerserPlugin.test.js | 84 ++--- ....js.snap => validate-options.test.js.snap} | 0 test/cache-option.test.js | 121 +++--- test/chunkFilter-option.test.js | 18 +- test/cjs.test.js | 8 +- test/exclude-option.test.js | 45 +-- test/extractComments-option.test.js | 343 ++++++------------ test/helpers/cleanErrorStack.js | 10 - test/helpers/compile.js | 11 +- test/helpers/execute.js | 22 ++ test/helpers/getAssets.js | 25 -- .../{createCompiler.js => getCompiler.js} | 10 +- test/helpers/getErrors.js | 5 + test/helpers/getWarnings.js | 5 + test/helpers/index.js | 20 +- test/helpers/normalizeErrors.js | 25 ++ test/helpers/readAsset.js | 23 ++ test/helpers/readAssets.js | 11 + test/include-option.test.js | 45 +-- test/minify-option.test.js | 80 ++-- test/parallel-option-failure.test.js | 21 +- test/parallel-option.test.js | 67 ++-- test/sourceMap-option.test.js | 209 ++++------- test/terserOptions-option.test.js | 324 ++++++----------- test/test-option.test.js | 65 ++-- ...ation.test.js => validate-options.test.js} | 0 test/warningsFilter-option.test.js | 54 +-- 32 files changed, 762 insertions(+), 1050 deletions(-) rename test/__snapshots__/{validation.test.js.snap => validate-options.test.js.snap} (100%) delete mode 100644 test/helpers/cleanErrorStack.js create mode 100644 test/helpers/execute.js delete mode 100644 test/helpers/getAssets.js rename test/helpers/{createCompiler.js => getCompiler.js} (66%) create mode 100644 test/helpers/getErrors.js create mode 100644 test/helpers/getWarnings.js create mode 100644 test/helpers/normalizeErrors.js create mode 100644 test/helpers/readAsset.js create mode 100644 test/helpers/readAssets.js rename test/{validation.test.js => validate-options.test.js} (100%) diff --git a/.editorconfig b/.editorconfig index 9f89f364..ab524583 100644 --- a/.editorconfig +++ b/.editorconfig @@ -9,5 +9,4 @@ insert_final_newline = true trim_trailing_whitespace = true [*.md] -insert_final_newline = true -trim_trailing_whitespace = false +trim_trailing_whitespace = false \ No newline at end of file diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 6aa59122..192d686e 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -2,10 +2,13 @@ trigger: - master - next +variables: + npm_config_cache: $(Pipeline.Workspace)/.npm + jobs: - job: Lint pool: - vmImage: ubuntu-16.04 + vmImage: ubuntu-latest steps: - task: NodeTool@0 inputs: @@ -20,10 +23,12 @@ jobs: node -v npm -v displayName: 'Print versions' - - task: Npm@1 + - task: CacheBeta@1 inputs: - command: custom - customCommand: ci + key: npm | $(Agent.OS) | package-lock.json + path: $(npm_config_cache) + displayName: 'Cache npm' + - script: npm ci displayName: 'Install dependencies' - script: npm run lint displayName: 'Run lint' @@ -34,10 +39,13 @@ jobs: - job: Linux pool: - vmImage: ubuntu-16.04 + vmImage: ubuntu-latest strategy: - maxParallel: 4 + maxParallel: 5 matrix: + node-13: + node_version: ^13.0.0 + webpack_version: latest node-12: node_version: ^12.0.0 webpack_version: latest @@ -47,8 +55,8 @@ jobs: node-8: node_version: ^8.9.0 webpack_version: latest - node-8-canary: - node_version: ^8.9.0 + node-10-canary: + node_version: ^10.13.0 webpack_version: next continue_on_error: true steps: @@ -65,10 +73,12 @@ jobs: node -v npm -v displayName: 'Print versions' - - task: Npm@1 + - task: CacheBeta@1 inputs: - command: custom - customCommand: ci + key: npm | $(Agent.OS) | package-lock.json + path: $(npm_config_cache) + displayName: 'Cache npm' + - script: npm ci displayName: 'Install dependencies' - script: npm i webpack@$(webpack_version) displayName: 'Install "webpack@$(webpack_version)"' @@ -86,10 +96,13 @@ jobs: - job: macOS pool: - vmImage: macOS-10.14 + vmImage: macOS-latest strategy: - maxParallel: 4 + maxParallel: 5 matrix: + node-13: + node_version: ^13.0.0 + webpack_version: latest node-12: node_version: ^12.0.0 webpack_version: latest @@ -99,8 +112,8 @@ jobs: node-8: node_version: ^8.9.0 webpack_version: latest - node-8-canary: - node_version: ^8.9.0 + node-10-canary: + node_version: ^10.13.0 webpack_version: next continue_on_error: true steps: @@ -117,10 +130,12 @@ jobs: node -v npm -v displayName: 'Print versions' - - task: Npm@1 + - task: CacheBeta@1 inputs: - command: custom - customCommand: ci + key: npm | $(Agent.OS) | package-lock.json + path: $(npm_config_cache) + displayName: 'Cache npm' + - script: npm ci displayName: 'Install dependencies' - script: npm i webpack@$(webpack_version) displayName: 'Install "webpack@$(webpack_version)"' @@ -138,10 +153,13 @@ jobs: - job: Windows pool: - vmImage: windows-2019 + vmImage: windows-latest strategy: - maxParallel: 4 + maxParallel: 5 matrix: + node-13: + node_version: ^13.0.0 + webpack_version: latest node-12: node_version: ^12.0.0 webpack_version: latest @@ -151,8 +169,8 @@ jobs: node-8: node_version: ^8.9.0 webpack_version: latest - node-8-canary: - node_version: ^8.9.0 + node-10-canary: + node_version: ^10.13.0 webpack_version: next continue_on_error: true steps: @@ -172,10 +190,12 @@ jobs: node -v npm -v displayName: 'Print versions' - - task: Npm@1 + - task: CacheBeta@1 inputs: - command: custom - customCommand: ci + key: npm | $(Agent.OS) | package-lock.json + path: $(npm_config_cache) + displayName: 'Cache npm' + - script: npm ci displayName: 'Install dependencies' - script: npm i webpack@$(webpack_version) displayName: 'Install "webpack@$(webpack_version)"' diff --git a/lint-staged.config.js b/lint-staged.config.js index 77a86a71..618a5ec4 100644 --- a/lint-staged.config.js +++ b/lint-staged.config.js @@ -1,4 +1,4 @@ module.exports = { '*.js': ['prettier --write', 'eslint --fix', 'git add'], - '*.{json,md,yml,css}': ['prettier --write', 'git add'], + '*.{json,md,yml,css,ts}': ['prettier --write', 'git add'], }; diff --git a/package-lock.json b/package-lock.json index c1075359..94e63e30 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1669,9 +1669,9 @@ } }, "@webpack-contrib/defaults": { - "version": "5.1.1", - "resolved": "https://registry.npmjs.org/@webpack-contrib/defaults/-/defaults-5.1.1.tgz", - "integrity": "sha512-UvWJSpCBVzDpCASLhBuiSw/S5i57Z3FqO5X2cRqkvDR6TlhaPp0cXLr7R83xyRs2+eZ5pyl9UnP8HtSJx1s7Fw==", + "version": "6.2.0", + "resolved": "https://registry.npmjs.org/@webpack-contrib/defaults/-/defaults-6.2.0.tgz", + "integrity": "sha512-MvEbQWTU/ZPq7acBTm2a/CWBZwt7MQqTIrTxpj7X6Hwwt4pgW+6bvYnAdtgpGewFSuwAoiurseyxXvOk4x02dg==", "dev": true, "requires": { "chalk": "^2.3.0", @@ -4055,6 +4055,18 @@ "graceful-fs": "^4.1.2", "memory-fs": "^0.5.0", "tapable": "^1.0.0" + }, + "dependencies": { + "memory-fs": { + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/memory-fs/-/memory-fs-0.5.0.tgz", + "integrity": "sha512-jA0rdU5KoQMC0e6ppoNRtpp6vjFq6+NY7r8hywnC7V+1Xj/MtHwGIbB1QaK/dunyjWteJzmkpd7ooeWg10T7GA==", + "dev": true, + "requires": { + "errno": "^0.1.3", + "readable-stream": "^2.0.1" + } + } } }, "errno": { @@ -4750,6 +4762,12 @@ "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-2.0.1.tgz", "integrity": "sha1-ewUhjd+WZ79/Nwv3/bLLFf3Qqkk=" }, + "fast-extend": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/fast-extend/-/fast-extend-1.0.2.tgz", + "integrity": "sha512-XXA9RmlPatkFKUzqVZAFth18R4Wo+Xug/S+C7YlYA3xrXwfPlW3dqNwOb4hvQo7wZJ2cNDYhrYuPzVOfHy5/uQ==", + "dev": true + }, "fast-glob": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.1.1.tgz", @@ -5025,6 +5043,12 @@ "minipass": "^3.0.0" } }, + "fs-monkey": { + "version": "0.3.3", + "resolved": "https://registry.npmjs.org/fs-monkey/-/fs-monkey-0.3.3.tgz", + "integrity": "sha512-FNUvuTAJ3CqCQb5ELn+qCbGR/Zllhf2HtwsdAtBi59s1WeCjKMT81fHcSu7dwIskqGVK+MmOrb7VOBlq3/SItw==", + "dev": true + }, "fs-readdir-recursive": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/fs-readdir-recursive/-/fs-readdir-recursive-1.1.0.tgz", @@ -8148,10 +8172,20 @@ "safe-buffer": "^5.1.2" } }, + "memfs": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/memfs/-/memfs-3.0.1.tgz", + "integrity": "sha512-v8MPoino1vfmwl2LDbFAg5tgPd7EPvKLvcSkx/CDJGB3JCGyHZSasRtC63wmM+iHFXh4cooYCu+cWCd1dqh2+A==", + "dev": true, + "requires": { + "fast-extend": "1.0.2", + "fs-monkey": "0.3.3" + } + }, "memory-fs": { - "version": "0.5.0", - "resolved": "https://registry.npmjs.org/memory-fs/-/memory-fs-0.5.0.tgz", - "integrity": "sha512-jA0rdU5KoQMC0e6ppoNRtpp6vjFq6+NY7r8hywnC7V+1Xj/MtHwGIbB1QaK/dunyjWteJzmkpd7ooeWg10T7GA==", + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/memory-fs/-/memory-fs-0.4.1.tgz", + "integrity": "sha1-OpoguEYlI+RHz7x+i7gO1me/xVI=", "dev": true, "requires": { "errno": "^0.1.3", @@ -11448,16 +11482,6 @@ "estraverse": "^4.1.1" } }, - "memory-fs": { - "version": "0.4.1", - "resolved": "https://registry.npmjs.org/memory-fs/-/memory-fs-0.4.1.tgz", - "integrity": "sha1-OpoguEYlI+RHz7x+i7gO1me/xVI=", - "dev": true, - "requires": { - "errno": "^0.1.3", - "readable-stream": "^2.0.1" - } - }, "schema-utils": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/schema-utils/-/schema-utils-1.0.0.tgz", diff --git a/package.json b/package.json index 7c85ab15..b3c1e190 100644 --- a/package.json +++ b/package.json @@ -7,34 +7,38 @@ "author": "webpack Contrib Team", "homepage": "https://github.com/webpack-contrib/terser-webpack-plugin", "bugs": "https://github.com/webpack-contrib/terser-webpack-plugin/issues", + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/webpack" + }, "main": "dist/cjs.js", "engines": { "node": ">= 8.9.0" }, "scripts": { "start": "npm run build -- -w", - "prebuild": "npm run clean", - "build": "cross-env NODE_ENV=production babel src -d dist --ignore \"src/**/*.test.js\" --copy-files", "clean": "del-cli dist", + "prebuild": "npm run clean", + "build": "cross-env NODE_ENV=production babel src -d dist --copy-files", "commitlint": "commitlint --from=master", - "lint:prettier": "prettier \"{**/*,*}.{js,json,md,yml,css}\" --list-different", - "lint:js": "eslint --cache src test", - "lint": "npm-run-all -l -p \"lint:**\"", - "prepare": "npm run build", - "release": "standard-version", "security": "npm audit", + "lint:prettier": "prettier \"{**/*,*}.{js,json,md,yml,css,ts}\" --list-different", + "lint:js": "eslint --cache .", + "lint": "npm-run-all -l -p \"lint:**\"", "test:only": "cross-env NODE_ENV=test jest", - "test:watch": "cross-env NODE_ENV=test jest --watch", - "test:coverage": "cross-env NODE_ENV=test jest --collectCoverageFrom=\"src/**/*.js\" --coverage", + "test:watch": "npm run test:only -- --watch", + "test:coverage": "npm run test:only -- --collectCoverageFrom=\"src/**/*.js\" --coverage", "pretest": "npm run lint", - "test": "cross-env NODE_ENV=test npm run test:coverage", + "test": "npm run test:coverage", + "prepare": "npm run build", + "release": "standard-version", "defaults": "webpack-defaults" }, "files": [ "dist" ], "peerDependencies": { - "webpack": "^4.0.0" + "webpack": "^4.0.0 || ^5.0.0" }, "dependencies": { "cacache": "^13.0.1", @@ -52,7 +56,7 @@ "@babel/preset-env": "^7.7.4", "@commitlint/cli": "^8.2.0", "@commitlint/config-conventional": "^8.2.0", - "@webpack-contrib/defaults": "^5.0.2", + "@webpack-contrib/defaults": "^6.2.0", "@webpack-contrib/eslint-config-webpack": "^3.0.0", "babel-jest": "^24.9.0", "commitlint-azure-pipelines-cli": "^1.0.2", @@ -66,7 +70,7 @@ "jest": "^24.9.0", "jest-junit": "^10.0.0", "lint-staged": "^9.5.0", - "memory-fs": "^0.5.0", + "memfs": "^3.0.1", "npm-run-all": "^4.1.5", "prettier": "^1.19.1", "standard-version": "^7.0.1", diff --git a/test/TerserPlugin.test.js b/test/TerserPlugin.test.js index fca51a28..e353554f 100644 --- a/test/TerserPlugin.test.js +++ b/test/TerserPlugin.test.js @@ -5,11 +5,12 @@ import ChunkTemplate from 'webpack/lib/ChunkTemplate'; import TerserPlugin from '../src/index'; import { - cleanErrorStack, compile, countPlugins, - createCompiler, - getAssets, + getCompiler, + getErrors, + getWarnings, + readsAssets, removeCache, } from './helpers'; @@ -34,22 +35,19 @@ describe('TerserPlugin', () => { afterEach(() => Promise.all([removeCache()])); it('should work (without options)', async () => { - const compiler = createCompiler(); + const compiler = getCompiler(); new TerserPlugin().apply(compiler); const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should work in multi compiler mode', async () => { - const multiCompiler = createCompiler([ + const multiCompiler = getCompiler([ { mode: 'production', bail: true, @@ -112,34 +110,28 @@ describe('TerserPlugin', () => { const multiStats = await compile(multiCompiler); multiStats.stats.forEach((stats, index) => { - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, multiCompiler.compilers[index])).toMatchSnapshot( - 'assets' - ); + expect( + readsAssets(multiCompiler.compilers[index], stats) + ).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); }); it('should work as a plugin', async () => { - const compiler = createCompiler({ + const compiler = getCompiler({ plugins: [new TerserPlugin()], }); const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should work as a minimizer', async () => { - const compiler = createCompiler({ + const compiler = getCompiler({ optimization: { minimize: true, minimizer: [new TerserPlugin()], @@ -148,16 +140,13 @@ describe('TerserPlugin', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should work and respect "terser" errors (the "parallel" option is "true")', async () => { - const compiler = createCompiler(); + const compiler = getCompiler(); new TerserPlugin({ parallel: true, @@ -169,18 +158,15 @@ describe('TerserPlugin', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); expect(/node_modules(\/|\\)terser/.test(stats.compilation.errors[0])).toBe( true ); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should work and respect "terser" errors (the "parallel" option is "false")', async () => { - const compiler = createCompiler(); + const compiler = getCompiler(); new TerserPlugin({ parallel: false, @@ -192,14 +178,11 @@ describe('TerserPlugin', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); expect(/node_modules(\/|\\)terser/.test(stats.compilation.errors[0])).toBe( true ); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should regenerate hash', async () => { @@ -213,7 +196,7 @@ describe('TerserPlugin', () => { MainTemplate.prototype.updateHashForChunk = mockMainTemplateUpdateHashForChunk; ChunkTemplate.prototype.updateHashForChunk = mockChunkTemplateUpdateHashFocChunk; - const compiler = createCompiler({ + const compiler = getCompiler({ entry: { js: `${__dirname}/fixtures/entry.js`, mjs: `${__dirname}/fixtures/entry.mjs`, @@ -231,11 +214,8 @@ describe('TerserPlugin', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); // On each chunk we have 2 calls (we have 1 async chunk and 4 initial). // First call do `webpack`. @@ -246,7 +226,7 @@ describe('TerserPlugin', () => { // We have 4 initial chunks (4 * 2 = 8 calls for MainTemplate) expect(mockChunkTemplateUpdateHashFocChunk).toHaveBeenCalledTimes(2); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); MainTemplate.prototype.updateHashForChunk = originalMainTemplateUpdateHashForChunk; ChunkTemplate.prototype.updateHashForChunk = originalChunkTemplateUpdateHashForChunk; diff --git a/test/__snapshots__/validation.test.js.snap b/test/__snapshots__/validate-options.test.js.snap similarity index 100% rename from test/__snapshots__/validation.test.js.snap rename to test/__snapshots__/validate-options.test.js.snap diff --git a/test/cache-option.test.js b/test/cache-option.test.js index 9da00247..e4a40f27 100644 --- a/test/cache-option.test.js +++ b/test/cache-option.test.js @@ -5,10 +5,11 @@ import TaskRunner from '../src/TaskRunner'; import TerserPlugin from '../src/index'; import { - createCompiler, compile, - cleanErrorStack, - getAssets, + getCompiler, + getErrors, + getWarnings, + readsAssets, removeCache, } from './helpers'; @@ -30,7 +31,7 @@ describe('cache option', () => { let compiler; beforeEach(() => { - compiler = createCompiler({ + compiler = getCompiler({ entry: { one: `${__dirname}/fixtures/cache.js`, two: `${__dirname}/fixtures/cache-1.js`, @@ -73,12 +74,9 @@ describe('cache option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); const countAssets = Object.keys(stats.compilation.assets).length; @@ -92,13 +90,9 @@ describe('cache option', () => { const newStats = await compile(compiler); - const newErrors = newStats.compilation.errors.map(cleanErrorStack); - const newWarnings = newStats.compilation.warnings.map(cleanErrorStack); - - expect(newErrors).toMatchSnapshot('errors'); - expect(newWarnings).toMatchSnapshot('warnings'); - - expect(getAssets(newStats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, newStats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); const newCountAssets = Object.keys(newStats.compilation.assets).length; @@ -119,12 +113,9 @@ describe('cache option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); // Cache disabled so we don't run `get` or `put` expect(cacacheGetSpy).toHaveBeenCalledTimes(0); @@ -148,12 +139,9 @@ describe('cache option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); const countAssets = Object.keys(stats.compilation.assets).length; @@ -167,13 +155,9 @@ describe('cache option', () => { const newStats = await compile(compiler); - const newErrors = newStats.compilation.errors.map(cleanErrorStack); - const newWarnings = newStats.compilation.warnings.map(cleanErrorStack); - - expect(newErrors).toMatchSnapshot('errors'); - expect(newWarnings).toMatchSnapshot('warnings'); - - expect(getAssets(newStats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, newStats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); const newCountAssets = Object.keys(newStats.compilation.assets).length; @@ -194,12 +178,9 @@ describe('cache option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); const countAssets = Object.keys(stats.compilation.assets).length; @@ -213,13 +194,9 @@ describe('cache option', () => { const newStats = await compile(compiler); - const newErrors = newStats.compilation.errors.map(cleanErrorStack); - const newWarnings = newStats.compilation.warnings.map(cleanErrorStack); - - expect(newErrors).toMatchSnapshot('errors'); - expect(newWarnings).toMatchSnapshot('warnings'); - - expect(getAssets(newStats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, newStats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); const newCountAssets = Object.keys(newStats.compilation.assets).length; @@ -249,12 +226,9 @@ describe('cache option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); const countAssets = Object.keys(stats.compilation.assets).length; @@ -268,12 +242,9 @@ describe('cache option', () => { const newStats = await compile(compiler); - const newErrors = newStats.compilation.errors.map(cleanErrorStack); - const newWarnings = newStats.compilation.warnings.map(cleanErrorStack); - - expect(newErrors).toMatchSnapshot('errors'); - expect(newWarnings).toMatchSnapshot('warnings'); - expect(getAssets(newStats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, newStats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); const newCountAssets = Object.keys(newStats.compilation.assets).length; @@ -295,11 +266,8 @@ describe('cache option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot and invalid cache when entry point was renamed', async () => { @@ -316,12 +284,9 @@ describe('cache option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); const countAssets = Object.keys(stats.compilation.assets).length; @@ -333,7 +298,7 @@ describe('cache option', () => { cacache.get.mockClear(); cacache.put.mockClear(); - compiler = createCompiler({ + compiler = getCompiler({ entry: { onne: `${__dirname}/fixtures/cache.js`, two: `${__dirname}/fixtures/cache-1.js`, @@ -347,13 +312,9 @@ describe('cache option', () => { const newStats = await compile(compiler); - const newErrors = newStats.compilation.errors.map(cleanErrorStack); - const newWarnings = newStats.compilation.warnings.map(cleanErrorStack); - - expect(newErrors).toMatchSnapshot('errors'); - expect(newWarnings).toMatchSnapshot('warnings'); - - expect(getAssets(newStats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, newStats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); const newCountAssets = Object.keys(newStats.compilation.assets).length; diff --git a/test/chunkFilter-option.test.js b/test/chunkFilter-option.test.js index b9902482..cb4c7031 100644 --- a/test/chunkFilter-option.test.js +++ b/test/chunkFilter-option.test.js @@ -1,10 +1,11 @@ import TerserPlugin from '../src/index'; import { - cleanErrorStack, - createCompiler, compile, - getAssets, + getCompiler, + getErrors, + getWarnings, + readsAssets, removeCache, } from './helpers'; @@ -12,7 +13,7 @@ describe('chunkFilter option', () => { let compiler; beforeEach(() => { - compiler = createCompiler({ + compiler = getCompiler({ entry: { included: `${__dirname}/fixtures/included1.js`, entry: `${__dirname}/fixtures/entry.js`, @@ -37,11 +38,8 @@ describe('chunkFilter option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getWarnings(stats)).toMatchSnapshot('errors'); + expect(getErrors(stats)).toMatchSnapshot('warnings'); }); }); diff --git a/test/cjs.test.js b/test/cjs.test.js index ba223f26..3893b518 100644 --- a/test/cjs.test.js +++ b/test/cjs.test.js @@ -1,8 +1,8 @@ -import TerserPlugin from '../src'; -import CJSTerserPlugin from '../src/cjs'; +import src from '../src'; +import cjs from '../src/cjs'; describe('CJS', () => { - it('should exported plugin', () => { - expect(CJSTerserPlugin).toEqual(TerserPlugin); + it('should export loader', () => { + expect(cjs).toEqual(src); }); }); diff --git a/test/exclude-option.test.js b/test/exclude-option.test.js index 5d3120d0..3ea148fe 100644 --- a/test/exclude-option.test.js +++ b/test/exclude-option.test.js @@ -1,10 +1,11 @@ import TerserPlugin from '../src/index'; import { - cleanErrorStack, - createCompiler, compile, - getAssets, + getCompiler, + getErrors, + getWarnings, + readsAssets, removeCache, } from './helpers'; @@ -12,7 +13,7 @@ describe('exclude option', () => { let compiler; beforeEach(() => { - compiler = createCompiler({ + compiler = getCompiler({ entry: { excluded1: `${__dirname}/fixtures/excluded1.js`, excluded2: `${__dirname}/fixtures/excluded2.js`, @@ -32,12 +33,9 @@ describe('exclude option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for a single String value', async () => { @@ -47,12 +45,9 @@ describe('exclude option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for multiple RegExp values', async () => { @@ -62,12 +57,9 @@ describe('exclude option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for multiple String values', async () => { @@ -77,11 +69,8 @@ describe('exclude option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); }); diff --git a/test/extractComments-option.test.js b/test/extractComments-option.test.js index b47a8f1f..b488dc35 100644 --- a/test/extractComments-option.test.js +++ b/test/extractComments-option.test.js @@ -1,10 +1,11 @@ import TerserPlugin from '../src/index'; import { - cleanErrorStack, compile, - createCompiler, - getAssets, + getCompiler, + getErrors, + getWarnings, + readsAssets, removeCache, } from './helpers'; @@ -12,7 +13,7 @@ describe('extractComments option', () => { let compiler; beforeEach(() => { - compiler = createCompiler({ + compiler = getCompiler({ entry: { one: `${__dirname}/fixtures/comments.js`, two: `${__dirname}/fixtures/comments-2.js`, @@ -35,12 +36,9 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "false" value', async () => { @@ -48,12 +46,9 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "true" value', async () => { @@ -61,12 +56,9 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "/Foo/" value', async () => { @@ -74,12 +66,9 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "all" value', async () => { @@ -87,12 +76,9 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "some" value', async () => { @@ -100,12 +86,9 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "Foo" value', async () => { @@ -113,12 +96,9 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for a "function" value', async () => { @@ -126,12 +106,9 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "extractComments.condition" with the "true" value', async () => { @@ -143,12 +120,9 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot when extracts comments to multiple files', async () => { @@ -171,12 +145,9 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot when extracts comments to a single file', async () => { @@ -192,12 +163,9 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot when extracts without condition', async () => { @@ -213,12 +181,9 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the `true` value and preserve "@license" comments', async () => { @@ -233,12 +198,9 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot when no condition, preserve only `/@license/i` comments and extract "some" comments', async () => { @@ -265,12 +227,9 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the `true` value and dedupe duplicate comments', async () => { @@ -278,12 +237,9 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot when extracts comments to a single file and dedupe duplicate comments', async () => { @@ -299,16 +255,13 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot when extracts comments to files with query string', async () => { - compiler = createCompiler({ + compiler = getCompiler({ entry: { one: `${__dirname}/fixtures/comments.js`, two: `${__dirname}/fixtures/comments-2.js`, @@ -325,16 +278,13 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot when extracts comments to files with query string and with placeholders', async () => { - compiler = createCompiler({ + compiler = getCompiler({ entry: { one: `${__dirname}/fixtures/comments.js`, two: `${__dirname}/fixtures/comments-2.js`, @@ -359,18 +309,15 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot when extracts comments to files with query string and when filename is a function', async () => { expect.assertions(13); - compiler = createCompiler({ + compiler = getCompiler({ entry: { one: `${__dirname}/fixtures/comments.js`, two: `${__dirname}/fixtures/comments-2.js`, @@ -399,16 +346,13 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot and throw error when comment file exists in assets', async () => { - compiler = createCompiler({ + compiler = getCompiler({ entry: { one: `${__dirname}/fixtures/comments.js`, }, @@ -423,15 +367,12 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot and throw error when comment file exists in assets', async () => { - compiler = createCompiler({ + compiler = getCompiler({ entry: { one: `${__dirname}/fixtures/comments.js`, }, @@ -449,15 +390,12 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for nested comment file', async () => { - compiler = createCompiler({ + compiler = getCompiler({ entry: { one: `${__dirname}/fixtures/comments.js`, }, @@ -472,16 +410,13 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for comment file when filename is nested', async () => { - compiler = createCompiler({ + compiler = getCompiler({ entry: { one: `${__dirname}/fixtures/comments.js`, }, @@ -499,12 +434,9 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot and extract "some" comments', async () => { @@ -514,12 +446,9 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot and preserve "all" and extract "some" comments', async () => { @@ -534,12 +463,9 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot and do not preserve and extract "all" comments', async () => { @@ -549,12 +475,9 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot and preserve "all" and extract "all" comments', async () => { @@ -569,12 +492,9 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot and do not preserve and extract "all" comments', async () => { @@ -584,12 +504,9 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot and preserve "all" and extract "all" comments', async () => { @@ -604,12 +521,9 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot and do not preserve and extract "some" comments', async () => { @@ -619,12 +533,9 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot and preserve "all" and extract "some" comments', async () => { @@ -639,12 +550,9 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot and preserve "all" and extract "some" comments', async () => { @@ -661,12 +569,9 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot and preserve "all" and do not extract comments', async () => { @@ -683,12 +588,9 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot and preserve "some" do not extract comments', async () => { @@ -698,12 +600,9 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot and preserve "all" do not extract comments', async () => { @@ -718,12 +617,9 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot and do not preserve or extract comments', async () => { @@ -738,11 +634,8 @@ describe('extractComments option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); }); diff --git a/test/helpers/cleanErrorStack.js b/test/helpers/cleanErrorStack.js deleted file mode 100644 index 31cfd27b..00000000 --- a/test/helpers/cleanErrorStack.js +++ /dev/null @@ -1,10 +0,0 @@ -function removeCWD(str) { - return str.split(`${process.cwd()}/`).join(''); -} - -export default function cleanErrorStack(error) { - return removeCWD(error.toString()) - .split('\n') - .slice(0, 2) - .join('\n'); -} diff --git a/test/helpers/compile.js b/test/helpers/compile.js index 1e22f7e2..066873ab 100644 --- a/test/helpers/compile.js +++ b/test/helpers/compile.js @@ -1,12 +1,11 @@ -export default function compile(compiler) { +export default (compiler) => { return new Promise((resolve, reject) => { - // eslint-disable-line consistent-return - compiler.run((err, stats) => { - if (err) { - return reject(err); + compiler.run((error, stats) => { + if (error) { + return reject(error); } return resolve(stats); }); }); -} +}; diff --git a/test/helpers/execute.js b/test/helpers/execute.js new file mode 100644 index 00000000..866001ae --- /dev/null +++ b/test/helpers/execute.js @@ -0,0 +1,22 @@ +import Module from 'module'; +import path from 'path'; + +const parentModule = module; + +export default (code) => { + const resource = 'test.js'; + const module = new Module(resource, parentModule); + // eslint-disable-next-line no-underscore-dangle + module.paths = Module._nodeModulePaths( + path.resolve(__dirname, '../fixtures') + ); + module.filename = resource; + + // eslint-disable-next-line no-underscore-dangle + module._compile( + `let __export__;${code};module.exports = __export__;`, + resource + ); + + return module.exports; +}; diff --git a/test/helpers/getAssets.js b/test/helpers/getAssets.js deleted file mode 100644 index 9a633b43..00000000 --- a/test/helpers/getAssets.js +++ /dev/null @@ -1,25 +0,0 @@ -import path from 'path'; - -export default function getAssets(stats, compiler) { - const usedFs = compiler.outputFileSystem; - const outputPath = stats.compilation.outputOptions.path; - const assets = {}; - - for (const file in stats.compilation.assets) { - if (Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)) { - let targetFile = file; - - const queryStringIdx = targetFile.indexOf('?'); - - if (queryStringIdx >= 0) { - targetFile = targetFile.substr(0, queryStringIdx); - } - - assets[file] = usedFs - .readFileSync(path.join(outputPath, targetFile)) - .toString(); - } - } - - return assets; -} diff --git a/test/helpers/createCompiler.js b/test/helpers/getCompiler.js similarity index 66% rename from test/helpers/createCompiler.js rename to test/helpers/getCompiler.js index 8498c05b..174065d7 100644 --- a/test/helpers/createCompiler.js +++ b/test/helpers/getCompiler.js @@ -1,9 +1,9 @@ import path from 'path'; import webpack from 'webpack'; -import MemoryFileSystem from 'memory-fs'; +import { createFsFromVolume, Volume } from 'memfs'; -export default function createCompiler(options = {}) { +export default function getCompiler(options = {}) { const compiler = webpack( Array.isArray(options) ? options @@ -26,7 +26,11 @@ export default function createCompiler(options = {}) { } ); - compiler.outputFileSystem = new MemoryFileSystem(); + const outputFileSystem = createFsFromVolume(new Volume()); + // Todo remove when we drop webpack@4 support + outputFileSystem.join = path.join.bind(path); + + compiler.outputFileSystem = outputFileSystem; return compiler; } diff --git a/test/helpers/getErrors.js b/test/helpers/getErrors.js new file mode 100644 index 00000000..716fbbb4 --- /dev/null +++ b/test/helpers/getErrors.js @@ -0,0 +1,5 @@ +import normalizeErrors from './normalizeErrors'; + +export default (stats) => { + return normalizeErrors(stats.compilation.errors); +}; diff --git a/test/helpers/getWarnings.js b/test/helpers/getWarnings.js new file mode 100644 index 00000000..c8a09d6d --- /dev/null +++ b/test/helpers/getWarnings.js @@ -0,0 +1,5 @@ +import normalizeErrors from './normalizeErrors'; + +export default (stats) => { + return normalizeErrors(stats.compilation.warnings); +}; diff --git a/test/helpers/index.js b/test/helpers/index.js index 09460eeb..e7c7bdd3 100644 --- a/test/helpers/index.js +++ b/test/helpers/index.js @@ -1,17 +1,25 @@ -import cleanErrorStack from './cleanErrorStack'; import compile from './compile'; import countPlugins from './countPlugins'; -import createCompiler from './createCompiler'; -import getAssets from './getAssets'; +import execute from './execute'; import getCacheDirectory from './getCacheDirectory'; +import getCompiler from './getCompiler'; +import getErrors from './getErrors'; +import getWarnings from './getWarnings'; +import readAsset from './readAsset'; +import readsAssets from './readAssets'; +import normalizeErrors from './normalizeErrors'; import removeCache from './removeCache'; export { - cleanErrorStack, compile, countPlugins, - createCompiler, - getAssets, + execute, getCacheDirectory, + getCompiler, + getErrors, + getWarnings, + normalizeErrors, + readAsset, + readsAssets, removeCache, }; diff --git a/test/helpers/normalizeErrors.js b/test/helpers/normalizeErrors.js new file mode 100644 index 00000000..d540ed76 --- /dev/null +++ b/test/helpers/normalizeErrors.js @@ -0,0 +1,25 @@ +function removeCWD(str) { + const isWin = process.platform === 'win32'; + let cwd = process.cwd(); + + if (isWin) { + // eslint-disable-next-line no-param-reassign + str = str.replace(/\\/g, '/'); + // eslint-disable-next-line no-param-reassign + cwd = cwd.replace(/\\/g, '/'); + } + + return str.replace(new RegExp(cwd, 'g'), ''); +} + +export default (errors) => { + return errors.map((error) => + removeCWD( + error + .toString() + .split('\n') + .slice(0, 2) + .join('\n') + ) + ); +}; diff --git a/test/helpers/readAsset.js b/test/helpers/readAsset.js new file mode 100644 index 00000000..8f4699f0 --- /dev/null +++ b/test/helpers/readAsset.js @@ -0,0 +1,23 @@ +import path from 'path'; + +export default (asset, compiler, stats) => { + const usedFs = compiler.outputFileSystem; + const outputPath = stats.compilation.outputOptions.path; + + let data = ''; + let targetFile = asset; + + const queryStringIdx = targetFile.indexOf('?'); + + if (queryStringIdx >= 0) { + targetFile = targetFile.substr(0, queryStringIdx); + } + + try { + data = usedFs.readFileSync(path.join(outputPath, targetFile)).toString(); + } catch (error) { + data = error.toString(); + } + + return data; +}; diff --git a/test/helpers/readAssets.js b/test/helpers/readAssets.js new file mode 100644 index 00000000..a2fb7837 --- /dev/null +++ b/test/helpers/readAssets.js @@ -0,0 +1,11 @@ +import readAsset from './readAsset'; + +export default function readAssets(compiler, stats) { + const assets = {}; + + Object.keys(stats.compilation.assets).forEach((asset) => { + assets[asset] = readAsset(asset, compiler, stats); + }); + + return assets; +} diff --git a/test/include-option.test.js b/test/include-option.test.js index 4bca3562..63652d2c 100644 --- a/test/include-option.test.js +++ b/test/include-option.test.js @@ -1,10 +1,11 @@ import TerserPlugin from '../src/index'; import { - cleanErrorStack, - createCompiler, compile, - getAssets, + getCompiler, + getErrors, + getWarnings, + readsAssets, removeCache, } from './helpers'; @@ -12,7 +13,7 @@ describe('include option', () => { let compiler; beforeEach(() => { - compiler = createCompiler({ + compiler = getCompiler({ entry: { included1: `${__dirname}/fixtures/included1.js`, included2: `${__dirname}/fixtures/included2.js`, @@ -32,12 +33,9 @@ describe('include option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for a single String value', async () => { @@ -47,12 +45,9 @@ describe('include option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for multiple RegExp values', async () => { @@ -62,12 +57,9 @@ describe('include option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for multiple String values', async () => { @@ -77,11 +69,8 @@ describe('include option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); }); diff --git a/test/minify-option.test.js b/test/minify-option.test.js index 033bb2a1..336781eb 100644 --- a/test/minify-option.test.js +++ b/test/minify-option.test.js @@ -1,10 +1,11 @@ import TerserPlugin from '../src'; import { - cleanErrorStack, compile, - createCompiler, - getAssets, + getCompiler, + getErrors, + getWarnings, + readsAssets, removeCache, } from './helpers'; @@ -14,7 +15,7 @@ describe('minify option', () => { afterEach(() => Promise.all([removeCache()])); it('should snapshot for the "uglify-js" minifier', async () => { - const compiler = createCompiler({ + const compiler = getCompiler({ entry: `${__dirname}/fixtures/minify/es5.js`, output: { path: `${__dirname}/dist-uglify-js`, @@ -36,16 +37,13 @@ describe('minify option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should snapshot snapshot for the "uglify-js" minifier with extracting comments', async () => { - const compiler = createCompiler({ + const compiler = getCompiler({ entry: `${__dirname}/fixtures/minify/es5.js`, output: { path: `${__dirname}/dist-uglify-js`, @@ -68,16 +66,13 @@ describe('minify option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should snapshot for the "terser" minifier', async () => { - const compiler = createCompiler({ + const compiler = getCompiler({ entry: `${__dirname}/fixtures/minify/es6.js`, output: { path: `${__dirname}/dist-terser`, @@ -99,16 +94,13 @@ describe('minify option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should snapshot snapshot for "terser" minifier when the "sourceMap" option is "true"', async () => { - const compiler = createCompiler({ + const compiler = getCompiler({ devtool: 'source-map', entry: `${__dirname}/fixtures/minify/es6.js`, output: { @@ -140,16 +132,13 @@ describe('minify option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should snapshot for the "terser" minifier when the "parallel" option is "true"', async () => { - const compiler = createCompiler({ + const compiler = getCompiler({ entry: `${__dirname}/fixtures/minify/es6.js`, output: { path: `${__dirname}/dist-terser`, @@ -172,16 +161,13 @@ describe('minify option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should snapshot for errors into the "minify" option', async () => { - const compiler = createCompiler({ + const compiler = getCompiler({ entry: `${__dirname}/fixtures/minify/es6.js`, output: { path: `${__dirname}/dist-terser`, @@ -198,15 +184,12 @@ describe('minify option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should snapshot for errors into the "minify" option when the "parallel" option is "true"', async () => { - const compiler = createCompiler({ + const compiler = getCompiler({ entry: `${__dirname}/fixtures/minify/es6.js`, output: { path: `${__dirname}/dist-terser`, @@ -224,10 +207,7 @@ describe('minify option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); }); diff --git a/test/parallel-option-failure.test.js b/test/parallel-option-failure.test.js index d366227b..ebe15297 100644 --- a/test/parallel-option-failure.test.js +++ b/test/parallel-option-failure.test.js @@ -5,9 +5,10 @@ import Worker from 'jest-worker'; import TerserPlugin from '../src/index'; import { - createCompiler, compile, - cleanErrorStack, + getCompiler, + getErrors, + getWarnings, removeCache, } from './helpers'; @@ -45,7 +46,7 @@ describe('parallel option', () => { beforeEach(() => { jest.clearAllMocks(); - compiler = createCompiler({ + compiler = getCompiler({ entry: { one: `${__dirname}/fixtures/entry.js`, two: `${__dirname}/fixtures/entry.js`, @@ -62,9 +63,6 @@ describe('parallel option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - expect(Worker).toHaveBeenCalledTimes(1); expect(Worker).toHaveBeenLastCalledWith(workerPath, { numWorkers: os.cpus().length - 1, @@ -74,8 +72,8 @@ describe('parallel option', () => { ); expect(workerEnd).toHaveBeenCalledTimes(1); - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for errors into the "jest-worker" package whe the "cache" option is "true"', async () => { @@ -83,9 +81,6 @@ describe('parallel option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - expect(Worker).toHaveBeenCalledTimes(1); expect(Worker).toHaveBeenLastCalledWith(workerPath, { numWorkers: os.cpus().length - 1, @@ -95,7 +90,7 @@ describe('parallel option', () => { ); expect(workerEnd).toHaveBeenCalledTimes(1); - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); }); diff --git a/test/parallel-option.test.js b/test/parallel-option.test.js index 9088a47e..6705124c 100644 --- a/test/parallel-option.test.js +++ b/test/parallel-option.test.js @@ -5,10 +5,11 @@ import Worker from 'jest-worker'; import TerserPlugin from '../src/index'; import { - createCompiler, compile, - cleanErrorStack, - getAssets, + getCompiler, + getErrors, + getWarnings, + readsAssets, removeCache, } from './helpers'; @@ -47,7 +48,7 @@ describe('parallel option', () => { beforeEach(() => { jest.clearAllMocks(); - compiler = createCompiler({ + compiler = getCompiler({ entry: { one: `${__dirname}/fixtures/entry.js`, two: `${__dirname}/fixtures/entry.js`, @@ -66,9 +67,6 @@ describe('parallel option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - expect(Worker).toHaveBeenCalledTimes(1); expect(Worker).toHaveBeenLastCalledWith(workerPath, { numWorkers: os.cpus().length - 1, @@ -78,9 +76,9 @@ describe('parallel option', () => { ); expect(workerEnd).toHaveBeenCalledTimes(1); - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "false" value', async () => { @@ -88,14 +86,11 @@ describe('parallel option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - expect(Worker).toHaveBeenCalledTimes(0); - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "true" value', async () => { @@ -103,9 +98,6 @@ describe('parallel option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - expect(Worker).toHaveBeenCalledTimes(1); expect(Worker).toHaveBeenLastCalledWith(workerPath, { numWorkers: os.cpus().length - 1, @@ -115,9 +107,9 @@ describe('parallel option', () => { ); expect(workerEnd).toHaveBeenCalledTimes(1); - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "2" value', async () => { @@ -125,9 +117,6 @@ describe('parallel option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - expect(Worker).toHaveBeenCalledTimes(1); expect(Worker).toHaveBeenLastCalledWith(workerPath, { numWorkers: 2, @@ -137,13 +126,13 @@ describe('parallel option', () => { ); expect(workerEnd).toHaveBeenCalledTimes(1); - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "true" value and only one file passed', async () => { - compiler = createCompiler({ + compiler = getCompiler({ entry: `${__dirname}/fixtures/entry.js`, }); @@ -151,9 +140,6 @@ describe('parallel option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - expect(Worker).toHaveBeenCalledTimes(1); expect(Worker).toHaveBeenLastCalledWith(workerPath, { numWorkers: os.cpus().length - 1, @@ -163,13 +149,13 @@ describe('parallel option', () => { ); expect(workerEnd).toHaveBeenCalledTimes(1); - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "true" value and two files passed', async () => { - compiler = createCompiler({ + compiler = getCompiler({ entry: { one: `${__dirname}/fixtures/entry.js`, two: `${__dirname}/fixtures/entry.js`, @@ -180,9 +166,6 @@ describe('parallel option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - expect(Worker).toHaveBeenCalledTimes(1); expect(Worker).toHaveBeenLastCalledWith(workerPath, { numWorkers: os.cpus().length - 1, @@ -192,8 +175,8 @@ describe('parallel option', () => { ); expect(workerEnd).toHaveBeenCalledTimes(1); - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); }); diff --git a/test/sourceMap-option.test.js b/test/sourceMap-option.test.js index 13fd05f8..e3123fec 100644 --- a/test/sourceMap-option.test.js +++ b/test/sourceMap-option.test.js @@ -3,10 +3,11 @@ import { SourceMapDevToolPlugin } from 'webpack'; import TerserPlugin from '../src/index'; import { - createCompiler, compile, - cleanErrorStack, - getAssets, + getCompiler, + getErrors, + getWarnings, + readsAssets, removeCache, } from './helpers'; @@ -32,7 +33,7 @@ describe('sourceMap', () => { afterEach(() => Promise.all([removeCache()])); it('should match snapshot for a "false" value (the "devtool" option has the "source-map" value)', async () => { - const compiler = createCompiler({ + const compiler = getCompiler({ entry: `${__dirname}/fixtures/entry.js`, devtool: 'source-map', }); @@ -41,16 +42,13 @@ describe('sourceMap', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for a "false" value (the "devtool" option has the "false" value)', async () => { - const compiler = createCompiler({ + const compiler = getCompiler({ entry: `${__dirname}/fixtures/entry.js`, devtool: false, }); @@ -59,16 +57,13 @@ describe('sourceMap', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for a "true" value (the "devtool" option has the "source-map" value)', async () => { - const compiler = createCompiler({ + const compiler = getCompiler({ entry: `${__dirname}/fixtures/entry.js`, devtool: 'source-map', }); @@ -77,16 +72,13 @@ describe('sourceMap', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for a "true" value (the "devtool" option has the "inline-source-map" value)', async () => { - const compiler = createCompiler({ + const compiler = getCompiler({ entry: `${__dirname}/fixtures/entry.js`, devtool: 'inline-source-map', }); @@ -95,16 +87,13 @@ describe('sourceMap', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for a "true" value (the "devtool" option has the "hidden-source-map" value)', async () => { - const compiler = createCompiler({ + const compiler = getCompiler({ entry: `${__dirname}/fixtures/entry.js`, devtool: 'hidden-source-map', }); @@ -113,16 +102,13 @@ describe('sourceMap', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for a "true" value (the "devtool" option has the "nosources-source-map" value)', async () => { - const compiler = createCompiler({ + const compiler = getCompiler({ entry: `${__dirname}/fixtures/entry.js`, devtool: 'nosources-source-map', }); @@ -131,16 +117,13 @@ describe('sourceMap', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for a "true" value (the "devtool" option has the "false" value)', async () => { - const compiler = createCompiler({ + const compiler = getCompiler({ entry: `${__dirname}/fixtures/entry.js`, devtool: false, }); @@ -149,12 +132,9 @@ describe('sourceMap', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for a "true" value (the "devtool" option has the "source-map" value) and source map invalid', async () => { @@ -191,7 +171,7 @@ describe('sourceMap', () => { ); } })(); - const compiler = createCompiler({ + const compiler = getCompiler({ entry: `${__dirname}/fixtures/entry.js`, devtool: 'source-map', plugins: [emitBrokenSourceMapPlugin], @@ -201,16 +181,13 @@ describe('sourceMap', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot when the "devtool" option has the "source-map" value', async () => { - const compiler = createCompiler({ + const compiler = getCompiler({ entry: `${__dirname}/fixtures/entry.js`, devtool: 'source-map', }); @@ -219,16 +196,13 @@ describe('sourceMap', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot when the "devtool" option has the "sourcemap" value', async () => { - const compiler = createCompiler({ + const compiler = getCompiler({ entry: `${__dirname}/fixtures/entry.js`, devtool: 'sourcemap', }); @@ -237,16 +211,13 @@ describe('sourceMap', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot when the "devtool" option has the "source-map" value', async () => { - const compiler = createCompiler({ + const compiler = getCompiler({ entry: `${__dirname}/fixtures/entry.js`, devtool: 'inline-source-map', }); @@ -255,16 +226,13 @@ describe('sourceMap', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot when the "devtool" option has the "source-map" value', async () => { - const compiler = createCompiler({ + const compiler = getCompiler({ entry: `${__dirname}/fixtures/entry.js`, devtool: 'hidden-source-map', }); @@ -273,16 +241,13 @@ describe('sourceMap', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot when the "devtool" option has the "source-map" value', async () => { - const compiler = createCompiler({ + const compiler = getCompiler({ entry: `${__dirname}/fixtures/entry.js`, devtool: 'nosources-source-map', }); @@ -291,16 +256,13 @@ describe('sourceMap', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for a "true" value (the "devtool" option has the "eval" value)', async () => { - const compiler = createCompiler({ + const compiler = getCompiler({ entry: `${__dirname}/fixtures/entry.js`, devtool: 'eval', }); @@ -309,16 +271,13 @@ describe('sourceMap', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for a "true" value (the "devtool" option has the "cheap-source-map" value)', async () => { - const compiler = createCompiler({ + const compiler = getCompiler({ entry: `${__dirname}/fixtures/entry.js`, devtool: 'cheap-source-map', }); @@ -327,16 +286,13 @@ describe('sourceMap', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the `SourceMapDevToolPlugin` plugin (like `source-map`)', async () => { - const compiler = createCompiler({ + const compiler = getCompiler({ entry: `${__dirname}/fixtures/entry.js`, devtool: false, plugins: [ @@ -352,16 +308,13 @@ describe('sourceMap', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the `SourceMapDevToolPlugin` plugin (like `cheap-source-map`)', async () => { - const compiler = createCompiler({ + const compiler = getCompiler({ entry: `${__dirname}/fixtures/entry.js`, devtool: false, plugins: [ @@ -377,16 +330,13 @@ describe('sourceMap', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for multi compiler mode with source maps', async () => { - const multiCompiler = createCompiler([ + const multiCompiler = getCompiler([ { mode: 'production', devtool: 'eval', @@ -470,14 +420,11 @@ describe('sourceMap', () => { const multiStats = await compile(multiCompiler); multiStats.stats.forEach((stats, index) => { - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, multiCompiler.compilers[index])).toMatchSnapshot( - 'assets' - ); + expect( + readsAssets(multiCompiler.compilers[index], stats) + ).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); }); }); diff --git a/test/terserOptions-option.test.js b/test/terserOptions-option.test.js index 78ee0d1f..2261a68a 100644 --- a/test/terserOptions-option.test.js +++ b/test/terserOptions-option.test.js @@ -1,10 +1,11 @@ import TerserPlugin from '../src/index'; import { - cleanErrorStack, - createCompiler, compile, - getAssets, + getCompiler, + getErrors, + getWarnings, + readsAssets, removeCache, } from './helpers'; @@ -14,7 +15,7 @@ describe('terserOptions option', () => { afterEach(() => Promise.all([removeCache()])); it('should match snapshot for the "ecma" option with the "5" value', async () => { - const compiler = createCompiler({ + const compiler = getCompiler({ entry: `${__dirname}/fixtures/ecma-5/entry.js`, }); @@ -31,16 +32,13 @@ describe('terserOptions option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "ecma" option with the "6" value', async () => { - const compiler = createCompiler({ + const compiler = getCompiler({ entry: `${__dirname}/fixtures/ecma-6/entry.js`, }); @@ -57,16 +55,13 @@ describe('terserOptions option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "ecma" option with the "7" value', async () => { - const compiler = createCompiler({ + const compiler = getCompiler({ entry: `${__dirname}/fixtures/ecma-7/entry.js`, }); @@ -83,16 +78,13 @@ describe('terserOptions option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "ecma" option with the "8" value', async () => { - const compiler = createCompiler({ + const compiler = getCompiler({ entry: `${__dirname}/fixtures/ecma-8/entry.js`, }); @@ -109,16 +101,13 @@ describe('terserOptions option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "warnings" option with the "false" value', async () => { - const compiler = createCompiler({ + const compiler = getCompiler({ entry: `${__dirname}/fixtures/unreachable-code.js`, }); @@ -130,16 +119,13 @@ describe('terserOptions option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "warnings" option with the "true" value', async () => { - const compiler = createCompiler({ + const compiler = getCompiler({ entry: `${__dirname}/fixtures/unreachable-code.js`, }); @@ -151,16 +137,13 @@ describe('terserOptions option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "parse.ecma" option with the "8" value', async () => { - const compiler = createCompiler(); + const compiler = getCompiler(); new TerserPlugin({ terserOptions: { @@ -172,16 +155,13 @@ describe('terserOptions option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "compress" option with the "false" value', async () => { - const compiler = createCompiler(); + const compiler = getCompiler(); new TerserPlugin({ terserOptions: { @@ -191,16 +171,13 @@ describe('terserOptions option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "compress" option with the "true" value', async () => { - const compiler = createCompiler(); + const compiler = getCompiler(); new TerserPlugin({ terserOptions: { @@ -210,16 +187,13 @@ describe('terserOptions option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "compress" option with an object value', async () => { - const compiler = createCompiler(); + const compiler = getCompiler(); new TerserPlugin({ terserOptions: { @@ -231,16 +205,13 @@ describe('terserOptions option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "mangle" option with the "false" value', async () => { - const compiler = createCompiler(); + const compiler = getCompiler(); new TerserPlugin({ terserOptions: { @@ -250,16 +221,13 @@ describe('terserOptions option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "mangle" option with the "true" value', async () => { - const compiler = createCompiler(); + const compiler = getCompiler(); new TerserPlugin({ terserOptions: { @@ -269,16 +237,13 @@ describe('terserOptions option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "mangle" option with object values', async () => { - const compiler = createCompiler(); + const compiler = getCompiler(); new TerserPlugin({ terserOptions: { @@ -290,16 +255,13 @@ describe('terserOptions option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "module" option with the "false" value', async () => { - const compiler = createCompiler(); + const compiler = getCompiler(); new TerserPlugin({ terserOptions: { @@ -309,16 +271,13 @@ describe('terserOptions option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "mangle" option with the "true" value', async () => { - const compiler = createCompiler(); + const compiler = getCompiler(); new TerserPlugin({ terserOptions: { @@ -328,16 +287,13 @@ describe('terserOptions option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "output.beautify" option with "true" value', async () => { - const compiler = createCompiler(); + const compiler = getCompiler(); new TerserPlugin({ terserOptions: { @@ -349,16 +305,13 @@ describe('terserOptions option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "output.comments" option with the "true"', async () => { - const compiler = createCompiler(); + const compiler = getCompiler(); new TerserPlugin({ terserOptions: { @@ -370,16 +323,13 @@ describe('terserOptions option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "toplevel" option with the "false" value', async () => { - const compiler = createCompiler(); + const compiler = getCompiler(); new TerserPlugin({ terserOptions: { @@ -389,16 +339,13 @@ describe('terserOptions option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "toplevel" option with the "true" value', async () => { - const compiler = createCompiler(); + const compiler = getCompiler(); new TerserPlugin({ terserOptions: { @@ -408,16 +355,13 @@ describe('terserOptions option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "nameCache" option with a empty object value', async () => { - const compiler = createCompiler(); + const compiler = getCompiler(); new TerserPlugin({ terserOptions: { @@ -427,16 +371,13 @@ describe('terserOptions option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "ie8" option with the "false" value', async () => { - const compiler = createCompiler(); + const compiler = getCompiler(); new TerserPlugin({ terserOptions: { @@ -446,16 +387,13 @@ describe('terserOptions option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "ie8" option with the "true" value', async () => { - const compiler = createCompiler(); + const compiler = getCompiler(); new TerserPlugin({ terserOptions: { @@ -465,16 +403,13 @@ describe('terserOptions option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "keep_classnames" option with the "false" value', async () => { - const compiler = createCompiler(); + const compiler = getCompiler(); new TerserPlugin({ terserOptions: { @@ -484,16 +419,13 @@ describe('terserOptions option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "keep_classnames" option with the "true" value', async () => { - const compiler = createCompiler(); + const compiler = getCompiler(); new TerserPlugin({ terserOptions: { @@ -503,16 +435,13 @@ describe('terserOptions option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "keep_fnames" option with the "false" value', async () => { - const compiler = createCompiler(); + const compiler = getCompiler(); new TerserPlugin({ terserOptions: { @@ -522,16 +451,13 @@ describe('terserOptions option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "keep_fnames" option with the "true" value', async () => { - const compiler = createCompiler(); + const compiler = getCompiler(); new TerserPlugin({ terserOptions: { @@ -541,16 +467,13 @@ describe('terserOptions option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "safari10" option with the "false" value', async () => { - const compiler = createCompiler(); + const compiler = getCompiler(); new TerserPlugin({ terserOptions: { @@ -560,16 +483,13 @@ describe('terserOptions option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "safari10" option with the "true" value', async () => { - const compiler = createCompiler(); + const compiler = getCompiler(); new TerserPlugin({ terserOptions: { @@ -579,16 +499,13 @@ describe('terserOptions option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for the "unknown" option', async () => { - const compiler = createCompiler(); + const compiler = getCompiler(); new TerserPlugin({ parallel: false, @@ -601,10 +518,7 @@ describe('terserOptions option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); }); diff --git a/test/test-option.test.js b/test/test-option.test.js index f098648f..6731dd50 100644 --- a/test/test-option.test.js +++ b/test/test-option.test.js @@ -1,10 +1,11 @@ import TerserPlugin from '../src/index'; import { - cleanErrorStack, - createCompiler, compile, - getAssets, + getCompiler, + getErrors, + getWarnings, + readsAssets, removeCache, } from './helpers'; @@ -12,7 +13,7 @@ describe('test option', () => { let compiler; beforeEach(() => { - compiler = createCompiler({ + compiler = getCompiler({ entry: { js: `${__dirname}/fixtures/entry.js`, mjs: `${__dirname}/fixtures/entry.mjs`, @@ -36,12 +37,9 @@ describe('test option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for a single `test` value ({RegExp})', async () => { @@ -51,12 +49,9 @@ describe('test option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for a single "test" value ({String})', async () => { @@ -66,12 +61,9 @@ describe('test option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for multiple "test" values ({RegExp})', async () => { @@ -81,12 +73,9 @@ describe('test option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for multiple "test" values ({String})', async () => { @@ -96,16 +85,13 @@ describe('test option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot and uglify "mjs"', async () => { - compiler = createCompiler({ + compiler = getCompiler({ entry: { js: `${__dirname}/fixtures/entry.js`, mjs: `${__dirname}/fixtures/entry.mjs`, @@ -123,11 +109,8 @@ describe('test option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); }); diff --git a/test/validation.test.js b/test/validate-options.test.js similarity index 100% rename from test/validation.test.js rename to test/validate-options.test.js diff --git a/test/warningsFilter-option.test.js b/test/warningsFilter-option.test.js index cfb33c01..a88e50da 100644 --- a/test/warningsFilter-option.test.js +++ b/test/warningsFilter-option.test.js @@ -1,10 +1,11 @@ import TerserPlugin from '../src/index'; import { - cleanErrorStack, - createCompiler, compile, - getAssets, + getCompiler, + getErrors, + getWarnings, + readsAssets, removeCache, } from './helpers'; @@ -12,7 +13,7 @@ describe('warningsFilter option', () => { let compiler; beforeEach(() => { - compiler = createCompiler({ + compiler = getCompiler({ entry: { one: `${__dirname}/fixtures/unreachable-code.js`, two: `${__dirname}/fixtures/unreachable-code-2.js`, @@ -41,12 +42,9 @@ describe('warningsFilter option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for a "function" value and the "sourceMap" option is "true" (filter by message)', async () => { @@ -66,12 +64,9 @@ describe('warningsFilter option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for a "function" value and the "sourceMap" value is "true" (filter by source)', async () => { @@ -91,12 +86,9 @@ describe('warningsFilter option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for a "function" value and the "sourceMap" option is "true" (filter by file)', async () => { @@ -116,12 +108,9 @@ describe('warningsFilter option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); it('should match snapshot for an empty "function" value', async () => { @@ -135,11 +124,8 @@ describe('warningsFilter option', () => { const stats = await compile(compiler); - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); + expect(readsAssets(compiler, stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); });