From 4bf4b36a32ae549054f7f3e687aaa161ce81746d Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Mon, 30 Jul 2018 15:42:46 +0300 Subject: [PATCH 01/11] refactor: helper --- test/helpers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/helpers.js b/test/helpers.js index 6083654d..55627958 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -65,6 +65,6 @@ export function removeCWD(str) { } export function cleanErrorStack(error) { - return exports.removeCWD(error.toString()).split('\n').slice(0, 2).join('\n'); + return removeCWD(error.toString()).split('\n').slice(0, 2).join('\n'); } From 3d3c2a76d485402659ecbc296c6c37c11563d1dc Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Mon, 30 Jul 2018 15:52:37 +0300 Subject: [PATCH 02/11] refactor: move `empty-options` test to `UglifyJsPlugin` test --- test/UglifyJsPlugin.test.js | 187 +++++++++++++++++- .../__snapshots__/UglifyJsPlugin.test.js.snap | 40 ++-- test/__snapshots__/empty-options.test.js.snap | 9 - test/empty-options.test.js | 187 ------------------ 4 files changed, 207 insertions(+), 216 deletions(-) delete mode 100644 test/__snapshots__/empty-options.test.js.snap delete mode 100644 test/empty-options.test.js diff --git a/test/UglifyJsPlugin.test.js b/test/UglifyJsPlugin.test.js index bae86b0c..48a5aa21 100644 --- a/test/UglifyJsPlugin.test.js +++ b/test/UglifyJsPlugin.test.js @@ -1,12 +1,191 @@ +import { RawSource } from 'webpack-sources'; import UglifyJsPlugin from '../src/index'; -import { cleanErrorStack, compile, createCompiler } from './helpers'; +import { cleanErrorStack, compile, createCompiler, PluginEnvironment } from './helpers'; describe('UglifyJsPlugin', () => { - it('export as function', () => { + it('should exported as function', () => { expect(typeof new UglifyJsPlugin().apply).toBe('function'); }); - it('validation errors', () => { + describe('when applied with no options', () => { + let eventBindings; + let eventBinding; + + beforeEach(() => { + const pluginEnvironment = new PluginEnvironment(); + const compilerEnv = pluginEnvironment.getEnvironmentStub(); + compilerEnv.context = ''; + + const plugin = new UglifyJsPlugin(); + plugin.apply(compilerEnv); + eventBindings = pluginEnvironment.getEventBindings(); + }); + + it('binds one event handler', () => { + expect(eventBindings.length).toBe(1); + }); + + describe('compilation handler', () => { + beforeEach(() => { + [eventBinding] = eventBindings; + }); + + it('binds to compilation event', () => { + expect(eventBinding.name).toBe('compilation'); + }); + + describe('when called', () => { + let chunkPluginEnvironment; + let compilationEventBindings; + let compilationEventBinding; + let compilation; + let callback; + + beforeEach(() => { + chunkPluginEnvironment = new PluginEnvironment(); + compilation = chunkPluginEnvironment.getEnvironmentStub(); + compilation.assets = { + 'test.js': {}, + 'test1.js': '', + 'test2.js': { + source: () => 'invalid javascript', + }, + 'test3.js': { + source: () => '/** @preserve Foo Bar */ function foo(longVariableName) { longVariableName = 1; }', + }, + }; + compilation.errors = []; + + eventBinding.handler(compilation); + compilationEventBindings = chunkPluginEnvironment.getEventBindings(); + }); + + it('binds one event handler', () => { + expect(compilationEventBindings.length).toBe(1); + }); + + describe('optimize-chunk-assets handler', () => { + beforeEach(() => { + [compilationEventBinding] = compilationEventBindings; + }); + + it('binds to optimize-chunk-assets event', () => { + expect(compilationEventBinding.name).toEqual('optimize-chunk-assets'); + }); + + it('only calls callback once', () => { + callback = jest.fn(); + compilationEventBinding.handler([''], () => { + callback(); + expect(callback.mock.calls.length).toBe(1); + }); + }); + + it('default only parses filenames ending with .js', () => { + compilationEventBinding.handler([{ + files: ['test', 'test.js'], + }], () => { + expect(Object.keys(compilation.assets).length).toBe(4); + }); + }); + + it('early returns if private property is already set', () => { + compilationEventBinding.handler([{ + files: ['test.js'], + }], () => { + expect(compilation.assets['test.js']).toEqual({}); + }); + }); + + it('outputs stack trace errors for invalid asset', () => { + compilationEventBinding.handler([{ + files: ['test1.js'], + }], () => { + expect(compilation.errors.length).toBe(1); + expect(compilation.errors[0]).toBeInstanceOf(Error); + expect(compilation.errors[0].message).toEqual(expect.stringContaining('asset.source is not a function')); + }); + }); + + it('outputs parsing errors for invalid javascript', () => { + compilationEventBinding.handler([{ + files: ['test2.js'], + }], () => { + expect(compilation.errors.length).toBe(1); + expect(compilation.errors[0]).toBeInstanceOf(Error); + expect(compilation.errors[0].message).toEqual(expect.stringContaining('Unexpected token')); + expect(compilation.errors[0].message).toEqual(expect.stringContaining('[test2.js:1,8]')); + }); + }); + + it('outputs no errors for valid javascript', () => { + compilationEventBinding.handler([{ + files: ['test3.js'], + }], () => { + expect(compilation.errors.length).toBe(0); + }); + }); + + it('outputs RawSource for valid javascript', () => { + compilationEventBinding.handler([{ + files: ['test3.js'], + }], () => { + expect(compilation.assets['test3.js']).toBeInstanceOf(RawSource); + }); + }); + + it('outputs mangled javascript', () => { + compilationEventBinding.handler([{ + files: ['test3.js'], + }], () => { + // eslint-disable-next-line no-underscore-dangle + expect(compilation.assets['test3.js']._value) + .not.toEqual(expect.stringContaining('longVariableName')); + }); + }); + + it('compresses and does not output beautified javascript', () => { + compilationEventBinding.handler([{ + files: ['test3.js'], + }], () => { + // eslint-disable-next-line no-underscore-dangle + expect(compilation.assets['test3.js']._value).not.toEqual(expect.stringContaining('\n')); + }); + }); + + it('preserves comments', () => { + compilationEventBinding.handler([{ + files: ['test3.js'], + }], () => { + // eslint-disable-next-line no-underscore-dangle + expect(compilation.assets['test3.js']._value).toEqual(expect.stringContaining('/**')); + }); + }); + }); + }); + }); + + it('matches snapshot', () => { + const compiler = createCompiler(); + new UglifyJsPlugin().apply(compiler); + + return compile(compiler).then((stats) => { + const errors = stats.compilation.errors.map(cleanErrorStack); + const warnings = stats.compilation.warnings.map(cleanErrorStack); + + expect(errors).toMatchSnapshot('errors'); + expect(warnings).toMatchSnapshot('warnings'); + + for (const file in stats.compilation.assets) { + if (Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)) { + expect(stats.compilation.assets[file].source()).toMatchSnapshot(file); + } + } + }); + }); + }); + + it('should handle validation errors', () => { /* eslint-disable no-new */ expect(() => { new UglifyJsPlugin({ test: /foo/ }); @@ -180,7 +359,7 @@ describe('UglifyJsPlugin', () => { }).not.toThrow('Validation Error'); }); - it('contain errors when uglify has unknown option', () => { + it('should contains error when uglify has unknown option', () => { const compiler = createCompiler(); new UglifyJsPlugin({ uglifyOptions: { diff --git a/test/__snapshots__/UglifyJsPlugin.test.js.snap b/test/__snapshots__/UglifyJsPlugin.test.js.snap index 7f05267c..0acf5757 100644 --- a/test/__snapshots__/UglifyJsPlugin.test.js.snap +++ b/test/__snapshots__/UglifyJsPlugin.test.js.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`UglifyJsPlugin contain errors when uglify has unknown option: errors 1`] = ` +exports[`UglifyJsPlugin should contains error when uglify has unknown option: errors 1`] = ` Array [ "Error: main.0c220ec66316af2c1b24.js from UglifyJs DefaultsError: \`unknown\` is not a supported option", @@ -9,7 +9,7 @@ DefaultsError: \`unknown\` is not a supported option", ] `; -exports[`UglifyJsPlugin contain errors when uglify has unknown option: main.0c220ec66316af2c1b24.js 1`] = ` +exports[`UglifyJsPlugin should contains error when uglify has unknown option: main.0c220ec66316af2c1b24.js 1`] = ` "webpackJsonp([0],[ /* 0 */ /***/ (function(module, exports) { @@ -29,7 +29,7 @@ module.exports = function Foo() { ],[0]);" `; -exports[`UglifyJsPlugin contain errors when uglify has unknown option: manifest.d6857f782c13a99b5917.js 1`] = ` +exports[`UglifyJsPlugin should contains error when uglify has unknown option: manifest.d6857f782c13a99b5917.js 1`] = ` "/******/ (function(modules) { // webpackBootstrap /******/ // install a JSONP callback for chunk loading /******/ var parentJsonpFunction = window[\\"webpackJsonp\\"]; @@ -133,16 +133,16 @@ exports[`UglifyJsPlugin contain errors when uglify has unknown option: manifest. /******/ ([]);" `; -exports[`UglifyJsPlugin contain errors when uglify has unknown option: warnings 1`] = `Array []`; +exports[`UglifyJsPlugin should contains error when uglify has unknown option: warnings 1`] = `Array []`; -exports[`UglifyJsPlugin validation errors 1`] = ` +exports[`UglifyJsPlugin should handle validation errors 1`] = ` "UglifyJs Plugin Invalid Options options['doesntExist'] is an invalid additional property " `; -exports[`UglifyJsPlugin validation errors 2`] = ` +exports[`UglifyJsPlugin should handle validation errors 2`] = ` "UglifyJs Plugin Invalid Options options.cache should be boolean @@ -151,7 +151,7 @@ options.cache should match exactly one schema in oneOf " `; -exports[`UglifyJsPlugin validation errors 3`] = ` +exports[`UglifyJsPlugin should handle validation errors 3`] = ` "UglifyJs Plugin Invalid Options options.parallel should be boolean @@ -160,7 +160,7 @@ options.parallel should match exactly one schema in oneOf " `; -exports[`UglifyJsPlugin validation errors 4`] = ` +exports[`UglifyJsPlugin should handle validation errors 4`] = ` "UglifyJs Plugin Invalid Options options.parallel should be boolean @@ -169,58 +169,66 @@ options.parallel should match exactly one schema in oneOf " `; -exports[`UglifyJsPlugin validation errors 5`] = ` +exports[`UglifyJsPlugin should handle validation errors 5`] = ` "UglifyJs Plugin Invalid Options options.sourceMap should be boolean " `; -exports[`UglifyJsPlugin validation errors 6`] = ` +exports[`UglifyJsPlugin should handle validation errors 6`] = ` "UglifyJs Plugin Invalid Options options.uglifyOptions should be object " `; -exports[`UglifyJsPlugin validation errors 7`] = ` +exports[`UglifyJsPlugin should handle validation errors 7`] = ` "UglifyJs Plugin Invalid Options options.uglifyOptions.ie8 should be boolean " `; -exports[`UglifyJsPlugin validation errors 8`] = ` +exports[`UglifyJsPlugin should handle validation errors 8`] = ` "UglifyJs Plugin Invalid Options options.uglifyOptions.ecma should be integer " `; -exports[`UglifyJsPlugin validation errors 9`] = ` +exports[`UglifyJsPlugin should handle validation errors 9`] = ` "UglifyJs Plugin Invalid Options options.uglifyOptions.ecma should be integer " `; -exports[`UglifyJsPlugin validation errors 10`] = ` +exports[`UglifyJsPlugin should handle validation errors 10`] = ` "UglifyJs Plugin Invalid Options options.uglifyOptions.ecma should be integer " `; -exports[`UglifyJsPlugin validation errors 11`] = ` +exports[`UglifyJsPlugin should handle validation errors 11`] = ` "UglifyJs Plugin Invalid Options options.uglifyOptions.ecma should be >= 5 " `; -exports[`UglifyJsPlugin validation errors 12`] = ` +exports[`UglifyJsPlugin should handle validation errors 12`] = ` "UglifyJs Plugin Invalid Options options.uglifyOptions.ecma should be <= 8 " `; + +exports[`UglifyJsPlugin when applied with no options matches snapshot: errors 1`] = `Array []`; + +exports[`UglifyJsPlugin when applied with no options matches snapshot: main.0c220ec66316af2c1b24.js 1`] = `"webpackJsonp([0],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`; + +exports[`UglifyJsPlugin when applied with no options matches snapshot: manifest.d6857f782c13a99b5917.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a { - let eventBindings; - let eventBinding; - - beforeEach(() => { - const pluginEnvironment = new PluginEnvironment(); - const compilerEnv = pluginEnvironment.getEnvironmentStub(); - compilerEnv.context = ''; - - const plugin = new UglifyJsPlugin(); - plugin.apply(compilerEnv); - eventBindings = pluginEnvironment.getEventBindings(); - }); - - it('matches snapshot', () => { - const compiler = createCompiler(); - new UglifyJsPlugin().apply(compiler); - - - return compile(compiler).then((stats) => { - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - - for (const file in stats.compilation.assets) { - if (Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)) { - expect(stats.compilation.assets[file].source()).toMatchSnapshot(file); - } - } - }); - }); - - it('binds one event handler', () => { - expect(eventBindings.length).toBe(1); - }); - - describe('compilation handler', () => { - beforeEach(() => { - [eventBinding] = eventBindings; - }); - - it('binds to compilation event', () => { - expect(eventBinding.name).toBe('compilation'); - }); - - describe('when called', () => { - let chunkPluginEnvironment; - let compilationEventBindings; - let compilationEventBinding; - let compilation; - let callback; - - beforeEach(() => { - chunkPluginEnvironment = new PluginEnvironment(); - compilation = chunkPluginEnvironment.getEnvironmentStub(); - compilation.assets = { - 'test.js': {}, - 'test1.js': '', - 'test2.js': { - source: () => 'invalid javascript', - }, - 'test3.js': { - source: () => '/** @preserve Foo Bar */ function foo(longVariableName) { longVariableName = 1; }', - }, - }; - compilation.errors = []; - - eventBinding.handler(compilation); - compilationEventBindings = chunkPluginEnvironment.getEventBindings(); - }); - - it('binds one event handler', () => { - expect(compilationEventBindings.length).toBe(1); - }); - - describe('optimize-chunk-assets handler', () => { - beforeEach(() => { - [compilationEventBinding] = compilationEventBindings; - }); - - it('binds to optimize-chunk-assets event', () => { - expect(compilationEventBinding.name).toEqual('optimize-chunk-assets'); - }); - - it('only calls callback once', () => { - callback = jest.fn(); - compilationEventBinding.handler([''], () => { - callback(); - expect(callback.mock.calls.length).toBe(1); - }); - }); - - it('default only parses filenames ending with .js', () => { - compilationEventBinding.handler([{ - files: ['test', 'test.js'], - }], () => { - expect(Object.keys(compilation.assets).length).toBe(4); - }); - }); - - it('early returns if private property is already set', () => { - compilationEventBinding.handler([{ - files: ['test.js'], - }], () => { - expect(compilation.assets['test.js']).toEqual({}); - }); - }); - - it('outputs stack trace errors for invalid asset', () => { - compilationEventBinding.handler([{ - files: ['test1.js'], - }], () => { - expect(compilation.errors.length).toBe(1); - expect(compilation.errors[0]).toBeInstanceOf(Error); - expect(compilation.errors[0].message).toEqual(expect.stringContaining('asset.source is not a function')); - }); - }); - - it('outputs parsing errors for invalid javascript', () => { - compilationEventBinding.handler([{ - files: ['test2.js'], - }], () => { - expect(compilation.errors.length).toBe(1); - expect(compilation.errors[0]).toBeInstanceOf(Error); - expect(compilation.errors[0].message).toEqual(expect.stringContaining('Unexpected token')); - expect(compilation.errors[0].message).toEqual(expect.stringContaining('[test2.js:1,8]')); - }); - }); - - it('outputs no errors for valid javascript', () => { - compilationEventBinding.handler([{ - files: ['test3.js'], - }], () => { - expect(compilation.errors.length).toBe(0); - }); - }); - - it('outputs RawSource for valid javascript', () => { - compilationEventBinding.handler([{ - files: ['test3.js'], - }], () => { - expect(compilation.assets['test3.js']).toBeInstanceOf(RawSource); - }); - }); - - it('outputs mangled javascript', () => { - compilationEventBinding.handler([{ - files: ['test3.js'], - }], () => { - // eslint-disable-next-line no-underscore-dangle - expect(compilation.assets['test3.js']._value) - .not.toEqual(expect.stringContaining('longVariableName')); - }); - }); - - it('compresses and does not output beautified javascript', () => { - compilationEventBinding.handler([{ - files: ['test3.js'], - }], () => { - // eslint-disable-next-line no-underscore-dangle - expect(compilation.assets['test3.js']._value).not.toEqual(expect.stringContaining('\n')); - }); - }); - - it('preserves comments', () => { - compilationEventBinding.handler([{ - files: ['test3.js'], - }], () => { - // eslint-disable-next-line no-underscore-dangle - expect(compilation.assets['test3.js']._value).toEqual(expect.stringContaining('/**')); - }); - }); - }); - }); - }); -}); From 412cf57076e4c0df3ae929b22f3f69d752e98a05 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Mon, 30 Jul 2018 16:02:53 +0300 Subject: [PATCH 03/11] refactor: better name test --- .../supports-multicompiler.test.js.snap | 20 +++++++++---------- test/supports-multicompiler.test.js | 4 ++-- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/test/__snapshots__/supports-multicompiler.test.js.snap b/test/__snapshots__/supports-multicompiler.test.js.snap index fb4afc6c..ac52a55f 100644 --- a/test/__snapshots__/supports-multicompiler.test.js.snap +++ b/test/__snapshots__/supports-multicompiler.test.js.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`when using MultiCompiler with empty options matches snapshot: compiler plugin count 1`] = ` +exports[`when using MultiCompiler matches snapshot with empty options: compiler plugin count 1`] = ` Object { "after-emit": 1, "after-resolvers": 2, @@ -14,13 +14,13 @@ Object { } `; -exports[`when using MultiCompiler with empty options matches snapshot: errors 1`] = `Array []`; +exports[`when using MultiCompiler matches snapshot with empty options: errors 1`] = `Array []`; -exports[`when using MultiCompiler with empty options matches snapshot: errors 2`] = `Array []`; +exports[`when using MultiCompiler matches snapshot with empty options: errors 2`] = `Array []`; -exports[`when using MultiCompiler with empty options matches snapshot: errors 3`] = `Array []`; +exports[`when using MultiCompiler matches snapshot with empty options: errors 3`] = `Array []`; -exports[`when using MultiCompiler with empty options matches snapshot: main.4dff64fd1192e906e901.js 1`] = ` +exports[`when using MultiCompiler matches snapshot with empty options: main.4dff64fd1192e906e901.js 1`] = ` "/******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; @@ -105,12 +105,12 @@ module.exports = function Foo() { /******/ ]);" `; -exports[`when using MultiCompiler with empty options matches snapshot: main.4dff64fd1192e906e901.js 2`] = `"!function(n){var t={};function e(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return n[r].call(o.exports,o,o.exports,e),o.l=!0,o.exports}e.m=n,e.c=t,e.d=function(n,t,r){e.o(n,t)||Object.defineProperty(n,t,{configurable:!1,enumerable:!0,get:r})},e.n=function(n){var t=n&&n.__esModule?function(){return n.default}:function(){return n};return e.d(t,\\"a\\",t),t},e.o=function(n,t){return Object.prototype.hasOwnProperty.call(n,t)},e.p=\\"\\",e(e.s=0)}([function(n,t){n.exports=function(){console.log(7)}}]);"`; +exports[`when using MultiCompiler matches snapshot with empty options: main.4dff64fd1192e906e901.js 2`] = `"!function(n){var t={};function e(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return n[r].call(o.exports,o,o.exports,e),o.l=!0,o.exports}e.m=n,e.c=t,e.d=function(n,t,r){e.o(n,t)||Object.defineProperty(n,t,{configurable:!1,enumerable:!0,get:r})},e.n=function(n){var t=n&&n.__esModule?function(){return n.default}:function(){return n};return e.d(t,\\"a\\",t),t},e.o=function(n,t){return Object.prototype.hasOwnProperty.call(n,t)},e.p=\\"\\",e(e.s=0)}([function(n,t){n.exports=function(){console.log(7)}}]);"`; -exports[`when using MultiCompiler with empty options matches snapshot: main.286f3599c95e4eaa6950.js 1`] = `"!function(t){var e={};function r(n){if(e[n])return e[n].exports;var o=e[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}r.m=t,r.c=e,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{configurable:!1,enumerable:!0,get:n})},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,\\"a\\",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p=\\"\\",r(r.s=0)}([function(t,e,r){\\"use strict\\";Object.defineProperty(e,\\"__esModule\\",{value:!0});var n=r(1);e.default=function(){const t=n.b,e=\`baz\${Math.random()}\`;return()=>({a:t+n.a+e,b:t,baz:e})}},function(t,e,r){\\"use strict\\";e.a=\\"bar\\",e.b=\\"foo\\"}]);"`; +exports[`when using MultiCompiler matches snapshot with empty options: main.286f3599c95e4eaa6950.js 1`] = `"!function(t){var e={};function r(n){if(e[n])return e[n].exports;var o=e[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}r.m=t,r.c=e,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{configurable:!1,enumerable:!0,get:n})},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,\\"a\\",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p=\\"\\",r(r.s=0)}([function(t,e,r){\\"use strict\\";Object.defineProperty(e,\\"__esModule\\",{value:!0});var n=r(1);e.default=function(){const t=n.b,e=\`baz\${Math.random()}\`;return()=>({a:t+n.a+e,b:t,baz:e})}},function(t,e,r){\\"use strict\\";e.a=\\"bar\\",e.b=\\"foo\\"}]);"`; -exports[`when using MultiCompiler with empty options matches snapshot: warnings 1`] = `Array []`; +exports[`when using MultiCompiler matches snapshot with empty options: warnings 1`] = `Array []`; -exports[`when using MultiCompiler with empty options matches snapshot: warnings 2`] = `Array []`; +exports[`when using MultiCompiler matches snapshot with empty options: warnings 2`] = `Array []`; -exports[`when using MultiCompiler with empty options matches snapshot: warnings 3`] = `Array []`; +exports[`when using MultiCompiler matches snapshot with empty options: warnings 3`] = `Array []`; diff --git a/test/supports-multicompiler.test.js b/test/supports-multicompiler.test.js index d673c039..463910f6 100644 --- a/test/supports-multicompiler.test.js +++ b/test/supports-multicompiler.test.js @@ -8,8 +8,8 @@ import { compile, } from './helpers'; -describe('when using MultiCompiler with empty options', () => { - it('matches snapshot', () => { +describe('when using MultiCompiler', () => { + it('matches snapshot with empty options', () => { const multiCompiler = createCompiler([ { bail: true, From af576ba42fb11a1820351a9c27e6acd02ddc3fde Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Mon, 30 Jul 2018 16:17:57 +0300 Subject: [PATCH 04/11] refactor: `uglifyOptions` options tests --- .../uglifyOptions-option.test.js.snap | 463 +++++++++--------- test/fixtures/unreachable-code.js | 1 + test/uglifyOptions-option.test.js | 72 ++- 3 files changed, 310 insertions(+), 226 deletions(-) create mode 100644 test/fixtures/unreachable-code.js diff --git a/test/__snapshots__/uglifyOptions-option.test.js.snap b/test/__snapshots__/uglifyOptions-option.test.js.snap index 095d5739..b3c3647a 100644 --- a/test/__snapshots__/uglifyOptions-option.test.js.snap +++ b/test/__snapshots__/uglifyOptions-option.test.js.snap @@ -1,189 +1,40 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`when applied with uglify-es options disable inline optimization by default (have a lot of problems): errors 1`] = `Array []`; +exports[`when applied with uglifyOptions options disable inline optimization by default (have a lot of problems): errors 1`] = `Array []`; -exports[`when applied with uglify-es options disable inline optimization by default (have a lot of problems): main.js 1`] = `"webpackJsonp([0],[function(o,c,n){\\"use strict\\";console.log(42)}],[0]);"`; +exports[`when applied with uglifyOptions options disable inline optimization by default (have a lot of problems): main.js 1`] = `"webpackJsonp([0],[function(o,c,n){\\"use strict\\";console.log(42)}],[0]);"`; -exports[`when applied with uglify-es options disable inline optimization by default (have a lot of problems): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a{return{a:t+a[\\"a\\"]+e,b:t,baz:e}}}e[\\"default\\"]=o},function(t,e,n){\\"use strict\\";const a=\\"bar\\";e[\\"a\\"]=a;e[\\"b\\"]=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`compress\` option (boolean false): main.js 1`] = `"webpackJsonp([0],[function(t,e,n){\\"use strict\\";Object.defineProperty(e,\\"__esModule\\",{value:true});var a=n(1);function o(){const t=a[\\"b\\"];const e=\`baz\${Math.random()}\`;return()=>{return{a:t+a[\\"a\\"]+e,b:t,baz:e}}}e[\\"default\\"]=o},function(t,e,n){\\"use strict\\";const a=\\"bar\\";e[\\"a\\"]=a;e[\\"b\\"]=\\"foo\\"}],[0]);"`; -exports[`when applied with uglify-es options matches snapshot for \`compress\` option (boolean false): manifest.js 1`] = `"(function(r){var e=window[\\"webpackJsonp\\"];window[\\"webpackJsonp\\"]=function n(f,i,u){var c,a,l=0,p=[],s;for(;l({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`compress\` option (boolean true): main.js 1`] = `"webpackJsonp([0],[function(t,a,e){\\"use strict\\";Object.defineProperty(a,\\"__esModule\\",{value:!0});var n=e(1);a.default=function(){const t=n.b,a=\`baz\${Math.random()}\`;return()=>({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; -exports[`when applied with uglify-es options matches snapshot for \`compress\` option (boolean true): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`compress\` option (object): main.js 1`] = `"webpackJsonp([0],[function(t,a,e){\\"use strict\\";Object.defineProperty(a,\\"__esModule\\",{value:!0});var n=e(1);a.default=function(){const t=n.b;const a=\`baz\${Math.random()}\`;return()=>({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; -exports[`when applied with uglify-es options matches snapshot for \`compress\` option (object): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){var f,i,p,a=0,l=[];for(;a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; - -exports[`when applied with uglify-es options matches snapshot for \`ie8\` option: manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; - -exports[`when applied with uglify-es options matches snapshot for \`keep_classnames\` option: manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+o.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; - -exports[`when applied with uglify-es options matches snapshot for \`keep_fnames\` option: manifest.js 1`] = `"!function(e){var r=window.webpackJsonp;window.webpackJsonp=function webpackJsonpCallback(_,n,o){for(var u,c,p,a=0,i=[];a<_.length;a++)c=_[a],t[c]&&i.push(t[c][0]),t[c]=0;for(u in n)Object.prototype.hasOwnProperty.call(n,u)&&(e[u]=n[u]);for(r&&r(_,n,o);i.length;)i.shift()();if(o)for(a=0;a({a:b+__WEBPACK_IMPORTED_MODULE_0__dep__.a+baz,b:b,baz:baz})}},function(module,__webpack_exports__,__webpack_require__){\\"use strict\\";__webpack_exports__.a=\\"bar\\",__webpack_exports__.b=\\"foo\\"}],[0]);"`; - -exports[`when applied with uglify-es options matches snapshot for \`mangle\` option (false): manifest.js 1`] = `"!function(modules){var parentJsonpFunction=window.webpackJsonp;window.webpackJsonp=function(chunkIds,moreModules,executeModules){for(var moduleId,chunkId,result,i=0,resolves=[];i({a:t+n.a+baz,b:t,baz:baz})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; - -exports[`when applied with uglify-es options matches snapshot for \`mangle\` option (object): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; - -exports[`when applied with uglify-es options matches snapshot for \`mangle\` option (true): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; - -exports[`when applied with uglify-es options matches snapshot for \`nameCache\` option: manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a ({ - a: t + n.a + a, - b: t, - baz: a - }); - }; -}, function(t, a, e) { - \\"use strict\\"; - a.a = \\"bar\\", a.b = \\"foo\\"; -} ], [ 0 ]);" -`; - -exports[`when applied with uglify-es options matches snapshot for \`output\` option: manifest.js 1`] = ` -"!function(r) { - var n = window.webpackJsonp; - window.webpackJsonp = function(e, u, c) { - for (var f, i, p, a = 0, l = []; a < e.length; a++) i = e[a], o[i] && l.push(o[i][0]), - o[i] = 0; - for (f in u) Object.prototype.hasOwnProperty.call(u, f) && (r[f] = u[f]); - for (n && n(e, u, c); l.length; ) l.shift()(); - if (c) for (a = 0; a < c.length; a++) p = t(t.s = c[a]); - return p; - }; - var e = {}, o = { - 1: 0 - }; - function t(n) { - if (e[n]) return e[n].exports; - var o = e[n] = { - i: n, - l: !1, - exports: {} - }; - return r[n].call(o.exports, o, o.exports, t), o.l = !0, o.exports; - } - t.m = r, t.c = e, t.d = function(r, n, e) { - t.o(r, n) || Object.defineProperty(r, n, { - configurable: !1, - enumerable: !0, - get: e - }); - }, t.n = function(r) { - var n = r && r.__esModule ? function() { - return r.default; - } : function() { - return r; - }; - return t.d(n, \\"a\\", n), n; - }, t.o = function(r, n) { - return Object.prototype.hasOwnProperty.call(r, n); - }, t.p = \\"\\", t.oe = function(r) { - throw console.error(r), r; - }; -}([]);" -`; - -exports[`when applied with uglify-es options matches snapshot for \`output\` option: warnings 1`] = `Array []`; - -exports[`when applied with uglify-es options matches snapshot for \`parse\` options: errors 1`] = `Array []`; - -exports[`when applied with uglify-es options matches snapshot for \`parse\` options: main.js 1`] = `"webpackJsonp([0],[function(t,a,e){\\"use strict\\";Object.defineProperty(a,\\"__esModule\\",{value:!0});var n=e(1);a.default=function(){const t=n.b,a=\`baz\${Math.random()}\`;return()=>({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; - -exports[`when applied with uglify-es options matches snapshot for \`parse\` options: manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; - -exports[`when applied with uglify-es options matches snapshot for \`safari10\` option: manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; - -exports[`when applied with uglify-es options matches snapshot for \`toplevel\` option: manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; + +exports[`when applied with uglifyOptions options matches snapshot for \`ie8\` option: manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; + +exports[`when applied with uglifyOptions options matches snapshot for \`keep_classnames\` option: manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+o.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; + +exports[`when applied with uglifyOptions options matches snapshot for \`keep_fnames\` option: manifest.js 1`] = `"!function(e){var r=window.webpackJsonp;window.webpackJsonp=function webpackJsonpCallback(_,n,o){for(var u,c,p,a=0,i=[];a<_.length;a++)c=_[a],t[c]&&i.push(t[c][0]),t[c]=0;for(u in n)Object.prototype.hasOwnProperty.call(n,u)&&(e[u]=n[u]);for(r&&r(_,n,o);i.length;)i.shift()();if(o)for(a=0;a({a:b+__WEBPACK_IMPORTED_MODULE_0__dep__.a+baz,b:b,baz:baz})}},function(module,__webpack_exports__,__webpack_require__){\\"use strict\\";__webpack_exports__.a=\\"bar\\",__webpack_exports__.b=\\"foo\\"}],[0]);"`; + +exports[`when applied with uglifyOptions options matches snapshot for \`mangle\` option (false): manifest.js 1`] = `"!function(modules){var parentJsonpFunction=window.webpackJsonp;window.webpackJsonp=function(chunkIds,moreModules,executeModules){for(var moduleId,chunkId,result,i=0,resolves=[];i({a:t+n.a+baz,b:t,baz:baz})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; + +exports[`when applied with uglifyOptions options matches snapshot for \`mangle\` option (object): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; + +exports[`when applied with uglifyOptions options matches snapshot for \`mangle\` option (true): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; + +exports[`when applied with uglifyOptions options matches snapshot for \`nameCache\` option: manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a ({ + a: t + n.a + a, + b: t, + baz: a + }); + }; +}, function(t, a, e) { + \\"use strict\\"; + a.a = \\"bar\\", a.b = \\"foo\\"; +} ], [ 0 ]);" +`; + +exports[`when applied with uglifyOptions options matches snapshot for \`output\` option: manifest.js 1`] = ` +"!function(r) { + var n = window.webpackJsonp; + window.webpackJsonp = function(e, u, c) { + for (var f, i, p, a = 0, l = []; a < e.length; a++) i = e[a], o[i] && l.push(o[i][0]), + o[i] = 0; + for (f in u) Object.prototype.hasOwnProperty.call(u, f) && (r[f] = u[f]); + for (n && n(e, u, c); l.length; ) l.shift()(); + if (c) for (a = 0; a < c.length; a++) p = t(t.s = c[a]); + return p; + }; + var e = {}, o = { + 1: 0 + }; + function t(n) { + if (e[n]) return e[n].exports; + var o = e[n] = { + i: n, + l: !1, + exports: {} + }; + return r[n].call(o.exports, o, o.exports, t), o.l = !0, o.exports; + } + t.m = r, t.c = e, t.d = function(r, n, e) { + t.o(r, n) || Object.defineProperty(r, n, { + configurable: !1, + enumerable: !0, + get: e + }); + }, t.n = function(r) { + var n = r && r.__esModule ? function() { + return r.default; + } : function() { + return r; + }; + return t.d(n, \\"a\\", n), n; + }, t.o = function(r, n) { + return Object.prototype.hasOwnProperty.call(r, n); + }, t.p = \\"\\", t.oe = function(r) { + throw console.error(r), r; + }; +}([]);" +`; + +exports[`when applied with uglifyOptions options matches snapshot for \`output\` option: warnings 1`] = `Array []`; + +exports[`when applied with uglifyOptions options matches snapshot for \`parse\` options: errors 1`] = `Array []`; + +exports[`when applied with uglifyOptions options matches snapshot for \`parse\` options: main.js 1`] = `"webpackJsonp([0],[function(t,a,e){\\"use strict\\";Object.defineProperty(a,\\"__esModule\\",{value:!0});var n=e(1);a.default=function(){const t=n.b,a=\`baz\${Math.random()}\`;return()=>({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; + +exports[`when applied with uglifyOptions options matches snapshot for \`parse\` options: manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; + +exports[`when applied with uglifyOptions options matches snapshot for \`safari10\` option: manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; + +exports[`when applied with uglifyOptions options matches snapshot for \`toplevel\` option: manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a { - it('matches snapshot for ecma 5', () => { +describe('when applied with uglifyOptions options', () => { + it('matches snapshot for `ecma` option (ecma 5)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/ecma-5/entry.js`, output: { @@ -42,7 +42,7 @@ describe('when applied with uglify-es options', () => { }); }); - it('matches snapshot for ecma 6', () => { + it('matches snapshot for `ecma` option (ecma 8)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/ecma-6/entry.js`, output: { @@ -78,7 +78,7 @@ describe('when applied with uglify-es options', () => { }); }); - it('matches snapshot for ecma 7', () => { + it('matches snapshot for `ecma` option (ecma 7)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/ecma-7/entry.js`, output: { @@ -113,7 +113,7 @@ describe('when applied with uglify-es options', () => { }); }); - it('matches snapshot for ecma 8', () => { + it('matches snapshot for `ecma` option (ecma 8)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/ecma-8/entry.js`, output: { @@ -149,6 +149,68 @@ describe('when applied with uglify-es options', () => { }); }); + it('matches snapshot for `warnings` option (boolean false)', () => { + const compiler = createCompiler({ + entry: `${__dirname}/fixtures/unreachable-code.js`, + output: { + path: `${__dirname}/dist`, + filename: '[name].js', + chunkFilename: '[id].[name].js', + }, + }); + + new UglifyJsPlugin({ + uglifyOptions: { + warnings: false, + }, + }).apply(compiler); + + return compile(compiler).then((stats) => { + const errors = stats.compilation.errors.map(cleanErrorStack); + const warnings = stats.compilation.warnings.map(cleanErrorStack); + + expect(errors).toMatchSnapshot('errors'); + expect(warnings).toMatchSnapshot('warnings'); + + for (const file in stats.compilation.assets) { + if (Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)) { + expect(stats.compilation.assets[file].source()).toMatchSnapshot(file); + } + } + }); + }); + + it('matches snapshot for `warnings` option (boolean true)', () => { + const compiler = createCompiler({ + entry: `${__dirname}/fixtures/unreachable-code.js`, + output: { + path: `${__dirname}/dist`, + filename: '[name].js', + chunkFilename: '[id].[name].js', + }, + }); + + new UglifyJsPlugin({ + uglifyOptions: { + warnings: true, + }, + }).apply(compiler); + + return compile(compiler).then((stats) => { + const errors = stats.compilation.errors.map(cleanErrorStack); + const warnings = stats.compilation.warnings.map(cleanErrorStack); + + expect(errors).toMatchSnapshot('errors'); + expect(warnings).toMatchSnapshot('warnings'); + + for (const file in stats.compilation.assets) { + if (Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)) { + expect(stats.compilation.assets[file].source()).toMatchSnapshot(file); + } + } + }); + }); + it('matches snapshot for `parse` options', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/import-export/entry.js`, From c18ec0e03bda3d93a01c5c9e7a828789e492c21e Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Mon, 30 Jul 2018 16:27:36 +0300 Subject: [PATCH 05/11] refactor: use same test names --- .../__snapshots__/exclude-option.test.js.snap | 24 +++--- .../__snapshots__/include-option.test.js.snap | 24 +++--- test/__snapshots__/minify-option.test.js.snap | 40 ++++----- .../parallel-option.test.js.snap | 42 +++++----- test/__snapshots__/test-option.test.js.snap | 84 +++++++++---------- test/exclude-option.test.js | 6 +- test/include-option.test.js | 6 +- test/minify-option.test.js | 6 +- test/parallel-option.test.js | 8 +- test/test-option.test.js | 6 +- 10 files changed, 123 insertions(+), 123 deletions(-) diff --git a/test/__snapshots__/exclude-option.test.js.snap b/test/__snapshots__/exclude-option.test.js.snap index cb1859ba..d8ec2996 100644 --- a/test/__snapshots__/exclude-option.test.js.snap +++ b/test/__snapshots__/exclude-option.test.js.snap @@ -1,10 +1,10 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`when applied with exclude option matches snapshot for a single exclude value: entry.a3533847fa24ef98733c.js 1`] = `"webpackJsonp([2],{2:function(o,n){o.exports=function(){console.log(7)}}},[2]);"`; +exports[`when applied with \`exclude\` option matches snapshot for a single \`exclude\` value: entry.a3533847fa24ef98733c.js 1`] = `"webpackJsonp([2],{2:function(o,n){o.exports=function(){console.log(7)}}},[2]);"`; -exports[`when applied with exclude option matches snapshot for a single exclude value: errors 1`] = `Array []`; +exports[`when applied with \`exclude\` option matches snapshot for a single \`exclude\` value: errors 1`] = `Array []`; -exports[`when applied with exclude option matches snapshot for a single exclude value: excluded1.4d3a1b43eccbc2acc9d6.js 1`] = ` +exports[`when applied with \`exclude\` option matches snapshot for a single \`exclude\` value: excluded1.4d3a1b43eccbc2acc9d6.js 1`] = ` "webpackJsonp([1],[ /* 0 */ /***/ (function(module, exports) { @@ -18,17 +18,17 @@ module.exports = function Bar1() { ],[0]);" `; -exports[`when applied with exclude option matches snapshot for a single exclude value: excluded2.a96f544a34079b25c7b4.js 1`] = `"webpackJsonp([0],[,function(o,n){o.exports=function(){console.log(7)}}],[1]);"`; +exports[`when applied with \`exclude\` option matches snapshot for a single \`exclude\` value: excluded2.a96f544a34079b25c7b4.js 1`] = `"webpackJsonp([0],[,function(o,n){o.exports=function(){console.log(7)}}],[1]);"`; -exports[`when applied with exclude option matches snapshot for a single exclude value: manifest.d37b2b873f771997c745.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a{console.log(\\"Good\\")}),o.default=\\"Awesome\\"}},[4]);"`; +exports[`when applied with \`test\` option matches snapshot for a single \`test\` value: AsyncImportExport.js?var=6294434024e08d3418c3 1`] = ` +"webpackJsonp([4],{ + +/***/ 4: +/***/ (function(module, __webpack_exports__, __webpack_require__) { + +\\"use strict\\"; +Object.defineProperty(__webpack_exports__, \\"__esModule\\", { value: true }); +__webpack_require__.e/* import() */(0).then(__webpack_require__.bind(null, 5)).then(() => { + console.log('Good') +}); + +/* harmony default export */ __webpack_exports__[\\"default\\"] = (\\"Awesome\\"); + + +/***/ }) + +},[4]);" +`; -exports[`when applied with test option matches snapshot for a multiple test values: errors 1`] = `Array []`; +exports[`when applied with \`test\` option matches snapshot for a single \`test\` value: errors 1`] = `Array []`; -exports[`when applied with test option matches snapshot for a multiple test values: importExport.js?var=6294434024e08d3418c3 1`] = ` +exports[`when applied with \`test\` option matches snapshot for a single \`test\` value: importExport.js?var=6294434024e08d3418c3 1`] = ` "webpackJsonp([1],[ /* 0 */, /* 1 */, @@ -62,9 +80,9 @@ const bar = 'bar'; ],[2]);" `; -exports[`when applied with test option matches snapshot for a multiple test values: js.js?var=6294434024e08d3418c3 1`] = `"webpackJsonp([3],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`; +exports[`when applied with \`test\` option matches snapshot for a single \`test\` value: js.js?var=6294434024e08d3418c3 1`] = `"webpackJsonp([3],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`; -exports[`when applied with test option matches snapshot for a multiple test values: manifest.js?var=6294434024e08d3418c3 1`] = ` +exports[`when applied with \`test\` option matches snapshot for a single \`test\` value: manifest.js?var=6294434024e08d3418c3 1`] = ` "/******/ (function(modules) { // webpackBootstrap /******/ // install a JSONP callback for chunk loading /******/ var parentJsonpFunction = window[\\"webpackJsonp\\"]; @@ -217,11 +235,11 @@ exports[`when applied with test option matches snapshot for a multiple test valu /******/ ([]);" `; -exports[`when applied with test option matches snapshot for a multiple test values: mjs.js?var=6294434024e08d3418c3 1`] = `"webpackJsonp([2],[,function(o,n){o.exports=function(){console.log(7)}}],[1]);"`; +exports[`when applied with \`test\` option matches snapshot for a single \`test\` value: mjs.js?var=6294434024e08d3418c3 1`] = `"webpackJsonp([2],[,function(o,n){o.exports=function(){console.log(7)}}],[1]);"`; -exports[`when applied with test option matches snapshot for a multiple test values: warnings 1`] = `Array []`; +exports[`when applied with \`test\` option matches snapshot for a single \`test\` value: warnings 1`] = `Array []`; -exports[`when applied with test option matches snapshot for a single test value: 0.0.js?ver=6294434024e08d3418c3 1`] = ` +exports[`when applied with \`test\` option matches snapshot for multiple \`test\` values: 0.0.js?ver=6294434024e08d3418c3 1`] = ` "webpackJsonp([0],{ /***/ 5: @@ -237,29 +255,11 @@ Object.defineProperty(__webpack_exports__, \\"__esModule\\", { value: true }); });" `; -exports[`when applied with test option matches snapshot for a single test value: AsyncImportExport.js?var=6294434024e08d3418c3 1`] = ` -"webpackJsonp([4],{ - -/***/ 4: -/***/ (function(module, __webpack_exports__, __webpack_require__) { - -\\"use strict\\"; -Object.defineProperty(__webpack_exports__, \\"__esModule\\", { value: true }); -__webpack_require__.e/* import() */(0).then(__webpack_require__.bind(null, 5)).then(() => { - console.log('Good') -}); - -/* harmony default export */ __webpack_exports__[\\"default\\"] = (\\"Awesome\\"); - - -/***/ }) - -},[4]);" -`; +exports[`when applied with \`test\` option matches snapshot for multiple \`test\` values: AsyncImportExport.js?var=6294434024e08d3418c3 1`] = `"webpackJsonp([4],{4:function(e,o,n){\\"use strict\\";Object.defineProperty(o,\\"__esModule\\",{value:!0}),n.e(0).then(n.bind(null,5)).then(()=>{console.log(\\"Good\\")}),o.default=\\"Awesome\\"}},[4]);"`; -exports[`when applied with test option matches snapshot for a single test value: errors 1`] = `Array []`; +exports[`when applied with \`test\` option matches snapshot for multiple \`test\` values: errors 1`] = `Array []`; -exports[`when applied with test option matches snapshot for a single test value: importExport.js?var=6294434024e08d3418c3 1`] = ` +exports[`when applied with \`test\` option matches snapshot for multiple \`test\` values: importExport.js?var=6294434024e08d3418c3 1`] = ` "webpackJsonp([1],[ /* 0 */, /* 1 */, @@ -301,9 +301,9 @@ const bar = 'bar'; ],[2]);" `; -exports[`when applied with test option matches snapshot for a single test value: js.js?var=6294434024e08d3418c3 1`] = `"webpackJsonp([3],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`; +exports[`when applied with \`test\` option matches snapshot for multiple \`test\` values: js.js?var=6294434024e08d3418c3 1`] = `"webpackJsonp([3],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`; -exports[`when applied with test option matches snapshot for a single test value: manifest.js?var=6294434024e08d3418c3 1`] = ` +exports[`when applied with \`test\` option matches snapshot for multiple \`test\` values: manifest.js?var=6294434024e08d3418c3 1`] = ` "/******/ (function(modules) { // webpackBootstrap /******/ // install a JSONP callback for chunk loading /******/ var parentJsonpFunction = window[\\"webpackJsonp\\"]; @@ -456,22 +456,22 @@ exports[`when applied with test option matches snapshot for a single test value: /******/ ([]);" `; -exports[`when applied with test option matches snapshot for a single test value: mjs.js?var=6294434024e08d3418c3 1`] = `"webpackJsonp([2],[,function(o,n){o.exports=function(){console.log(7)}}],[1]);"`; +exports[`when applied with \`test\` option matches snapshot for multiple \`test\` values: mjs.js?var=6294434024e08d3418c3 1`] = `"webpackJsonp([2],[,function(o,n){o.exports=function(){console.log(7)}}],[1]);"`; -exports[`when applied with test option matches snapshot for a single test value: warnings 1`] = `Array []`; +exports[`when applied with \`test\` option matches snapshot for multiple \`test\` values: warnings 1`] = `Array []`; -exports[`when applied with test option matches snapshot with empty value: 0.0.js?ver=6294434024e08d3418c3 1`] = `"webpackJsonp([0],{5:function(e,t,c){\\"use strict\\";Object.defineProperty(t,\\"__esModule\\",{value:!0}),t.default=\\"async-dep\\"}});"`; +exports[`when applied with \`test\` option matches snapshot with empty value: 0.0.js?ver=6294434024e08d3418c3 1`] = `"webpackJsonp([0],{5:function(e,t,c){\\"use strict\\";Object.defineProperty(t,\\"__esModule\\",{value:!0}),t.default=\\"async-dep\\"}});"`; -exports[`when applied with test option matches snapshot with empty value: AsyncImportExport.js?var=6294434024e08d3418c3 1`] = `"webpackJsonp([4],{4:function(e,o,n){\\"use strict\\";Object.defineProperty(o,\\"__esModule\\",{value:!0}),n.e(0).then(n.bind(null,5)).then(()=>{console.log(\\"Good\\")}),o.default=\\"Awesome\\"}},[4]);"`; +exports[`when applied with \`test\` option matches snapshot with empty value: AsyncImportExport.js?var=6294434024e08d3418c3 1`] = `"webpackJsonp([4],{4:function(e,o,n){\\"use strict\\";Object.defineProperty(o,\\"__esModule\\",{value:!0}),n.e(0).then(n.bind(null,5)).then(()=>{console.log(\\"Good\\")}),o.default=\\"Awesome\\"}},[4]);"`; -exports[`when applied with test option matches snapshot with empty value: errors 1`] = `Array []`; +exports[`when applied with \`test\` option matches snapshot with empty value: errors 1`] = `Array []`; -exports[`when applied with test option matches snapshot with empty value: importExport.js?var=6294434024e08d3418c3 1`] = `"webpackJsonp([1],[,,function(t,a,e){\\"use strict\\";Object.defineProperty(a,\\"__esModule\\",{value:!0});var n=e(3);a.default=function(){const t=n.b,a=\`baz\${Math.random()}\`;return()=>({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[2]);"`; +exports[`when applied with \`test\` option matches snapshot with empty value: importExport.js?var=6294434024e08d3418c3 1`] = `"webpackJsonp([1],[,,function(t,a,e){\\"use strict\\";Object.defineProperty(a,\\"__esModule\\",{value:!0});var n=e(3);a.default=function(){const t=n.b,a=\`baz\${Math.random()}\`;return()=>({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[2]);"`; -exports[`when applied with test option matches snapshot with empty value: js.js?var=6294434024e08d3418c3 1`] = `"webpackJsonp([3],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`; +exports[`when applied with \`test\` option matches snapshot with empty value: js.js?var=6294434024e08d3418c3 1`] = `"webpackJsonp([3],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`; -exports[`when applied with test option matches snapshot with empty value: manifest.js?var=6294434024e08d3418c3 1`] = `"!function(e){var r=window.webpackJsonp;window.webpackJsonp=function(n,c,u){for(var i,a,f,l=0,s=[];l { +describe('when applied with `exclude` option', () => { let compiler; beforeEach(() => { @@ -18,7 +18,7 @@ describe('when applied with exclude option', () => { }); }); - it('matches snapshot for a single exclude value', () => { + it('matches snapshot for a single `exclude` value', () => { new UglifyJsPlugin({ exclude: /excluded1/i, }).apply(compiler); @@ -39,7 +39,7 @@ describe('when applied with exclude option', () => { }); }); - it('matches snapshot for multiple exclude values', () => { + it('matches snapshot for multiple `exclude` values', () => { new UglifyJsPlugin({ exclude: [ /excluded1/i, diff --git a/test/include-option.test.js b/test/include-option.test.js index dbee027f..2869710e 100644 --- a/test/include-option.test.js +++ b/test/include-option.test.js @@ -5,7 +5,7 @@ import { compile, } from './helpers'; -describe('when applied with include option', () => { +describe('when applied with `include` option', () => { let compiler; beforeEach(() => { @@ -18,7 +18,7 @@ describe('when applied with include option', () => { }); }); - it('matches snapshot for a single include value', () => { + it('matches snapshot for a single `include` value', () => { new UglifyJsPlugin({ include: /included1/i, }).apply(compiler); @@ -39,7 +39,7 @@ describe('when applied with include option', () => { }); }); - it('matches snapshot for multiple include values', () => { + it('matches snapshot for multiple `include` values', () => { new UglifyJsPlugin({ include: [ /included1/i, diff --git a/test/minify-option.test.js b/test/minify-option.test.js index ad143e00..21444eaf 100644 --- a/test/minify-option.test.js +++ b/test/minify-option.test.js @@ -2,7 +2,7 @@ import path from 'path'; import UglifyJsPlugin from '../src'; import { cleanErrorStack, compile, createCompiler } from './helpers'; -describe('when applied with minify option', () => { +describe('when applied with `minify` option', () => { it('matches snapshot for `uglify-js` minifier', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/minify/es5.js`, @@ -114,7 +114,7 @@ describe('when applied with minify option', () => { }); }); - it('matches snapshot for `terser` minifier and `sourceMap: true`', () => { + it('matches snapshot for `terser` minifier and `sourceMap` is `true`', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/minify/es6.js`, output: { @@ -169,7 +169,7 @@ describe('when applied with minify option', () => { }); }); - it('matches snapshot for `terser` minifier and `parallel: true`', () => { + it('matches snapshot for `terser` minifier and `parallel` is `true`', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/minify/es6.js`, output: { diff --git a/test/parallel-option.test.js b/test/parallel-option.test.js index b6f53287..b494a23c 100644 --- a/test/parallel-option.test.js +++ b/test/parallel-option.test.js @@ -22,7 +22,7 @@ jest.mock('worker-farm', () => { return mock; }); -describe('when applied with parallel option', () => { +describe('when applied with `parallel` option', () => { let compiler; beforeEach(() => { @@ -39,7 +39,7 @@ describe('when applied with parallel option', () => { }); }); - it('matches snapshot with false value', () => { + it('matches snapshot for `false` value', () => { new UglifyJsPlugin({ parallel: false }).apply(compiler); return compile(compiler).then((stats) => { @@ -60,7 +60,7 @@ describe('when applied with parallel option', () => { }); }); - it('matches snapshot with true value', () => { + it('matches snapshot for `true` value', () => { new UglifyJsPlugin({ parallel: true }).apply(compiler); return compile(compiler).then((stats) => { @@ -83,7 +83,7 @@ describe('when applied with parallel option', () => { }); }); - it('matches snapshot with 2 value (number)', () => { + it('matches snapshot for `2` value (number)', () => { new UglifyJsPlugin({ parallel: 2 }).apply(compiler); return compile(compiler).then((stats) => { diff --git a/test/test-option.test.js b/test/test-option.test.js index 1f204f11..1085340e 100644 --- a/test/test-option.test.js +++ b/test/test-option.test.js @@ -5,7 +5,7 @@ import { compile, } from './helpers'; -describe('when applied with test option', () => { +describe('when applied with `test` option', () => { let compiler; beforeEach(() => { @@ -42,7 +42,7 @@ describe('when applied with test option', () => { }); }); - it('matches snapshot for a single test value', () => { + it('matches snapshot for a single `test` value', () => { new UglifyJsPlugin({ test: /(m)?js\.js(\?.*)?$/i, }).apply(compiler); @@ -62,7 +62,7 @@ describe('when applied with test option', () => { }); }); - it('matches snapshot for a multiple test values', () => { + it('matches snapshot for multiple `test` values', () => { new UglifyJsPlugin({ test: [ /(m)?js\.js(\?.*)?$/i, From dd849ed9cccb8388d91b4e3d2512474bd33494c3 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Mon, 30 Jul 2018 19:18:15 +0300 Subject: [PATCH 06/11] refactor: `cache` option tests --- test/__snapshots__/cache-option.test.js.snap | 191 +++-- test/cache-option.test.js | 755 ++++++------------- 2 files changed, 359 insertions(+), 587 deletions(-) diff --git a/test/__snapshots__/cache-option.test.js.snap b/test/__snapshots__/cache-option.test.js.snap index c31f5d6b..ad5e3d87 100644 --- a/test/__snapshots__/cache-option.test.js.snap +++ b/test/__snapshots__/cache-option.test.js.snap @@ -1,117 +1,204 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`when options.cache false matches snapshot: errors 1`] = `Array []`; +exports[`when applied with \`cache\` option matches snapshot for \`false\` value: errors 1`] = `Array []`; -exports[`when options.cache false matches snapshot: main.0c220ec66316af2c1b24.js 1`] = `"webpackJsonp([0],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`; +exports[`when applied with \`cache\` option matches snapshot for \`false\` value: four.0741ac92c5e76ec45240.js 1`] = `"webpackJsonp([3],[],[0]);"`; -exports[`when options.cache false matches snapshot: manifest.d6857f782c13a99b5917.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a { - const assets = { - 'test.js': { - source: () => 'function test(foo) { foo = 1; }', - }, - 'test1.js': { - source: () => 'function test1(foo) { foo = 1; }', - }, - 'test2.js': { - source: () => 'function test2(foo) { foo = 1; }', - }, - 'test3.js': { - source: () => 'function test3(foo) { foo = 1; }', - }, - }; - - describe('false', () => { - let eventBindings; - let eventBinding; - - beforeAll(() => cacache.rm.all(cacheDir)); - - afterAll(() => cacache.rm.all(cacheDir)); - - beforeEach(() => { - const pluginEnvironment = new PluginEnvironment(); - const compilerEnv = pluginEnvironment.getEnvironmentStub(); - compilerEnv.context = ''; - - const plugin = new UglifyJsPlugin({ - cache: false, - }); - plugin.apply(compilerEnv); - eventBindings = pluginEnvironment.getEventBindings(); - }); - - it('binds one event handler', () => { - expect(eventBindings.length).toBe(1); +const otherCacheDir = findCacheDir({ name: 'other-cache-directory' }); + +describe('when applied with `cache` option', () => { + let compiler; + + beforeEach(() => { + compiler = createCompiler({ + entry: { + one: `${__dirname}/fixtures/entry.js`, + two: `${__dirname}/fixtures/entry.js`, + three: `${__dirname}/fixtures/entry.js`, + four: `${__dirname}/fixtures/entry.js`, + }, }); - describe('compilation handler', () => { - beforeEach(() => { - [eventBinding] = eventBindings; - }); - - it('binds to compilation event', () => { - expect(eventBinding.name).toBe('compilation'); - }); - - describe('when called', () => { - let chunkPluginEnvironment; - let compilationEventBindings; - let compilationEventBinding; - let compilation; - let callback; - - beforeEach(() => { - chunkPluginEnvironment = new PluginEnvironment(); - compilation = chunkPluginEnvironment.getEnvironmentStub(); - compilation.assets = Object.assign({}, assets); - compilation.errors = []; - - eventBinding.handler(compilation); - compilationEventBindings = chunkPluginEnvironment.getEventBindings(); - }); - - it('binds one event handler', () => { - expect(compilationEventBindings.length).toBe(1); - }); - - describe('optimize-chunk-assets handler', () => { - beforeEach(() => { - [compilationEventBinding] = compilationEventBindings; - }); - - it('binds to optimize-chunk-assets event', () => { - expect(compilationEventBinding.name).toEqual('optimize-chunk-assets'); - }); + return Promise.all([ + cacache.rm.all(cacheDir), + cacache.rm.all(otherCacheDir), + ]); + }); - it('only calls callback once', (done) => { - callback = jest.fn(); - compilationEventBinding.handler([''], () => { - callback(); - expect(callback.mock.calls.length).toBe(1); - done(); - }); - }); + afterEach(() => Promise.all([ + cacache.rm.all(cacheDir), + cacache.rm.all(otherCacheDir), + ])); - it('cache files', (done) => { - const files = ['test.js', 'test1.js', 'test2.js', 'test3.js']; + it('matches snapshot for `false` value', () => { + new UglifyJsPlugin({ cache: false }).apply(compiler); - cacache.get = jest.fn(cacache.get); - cacache.put = jest.fn(cacache.put); + cacache.get = jest.fn(cacache.get); + cacache.put = jest.fn(cacache.put); - compilationEventBinding.handler([{ - files, - }], () => { - // Cache disabled so we don't run `get` or `put` - expect(cacache.get.mock.calls.length).toBe(0); - expect(cacache.put.mock.calls.length).toBe(0); + return compile(compiler).then((stats) => { + const errors = stats.compilation.errors.map(cleanErrorStack); + const warnings = stats.compilation.warnings.map(cleanErrorStack); - cacache - .ls(cacheDir) - .then((cacheEntriesList) => { - const cacheKeys = Object.keys(cacheEntriesList); + expect(errors).toMatchSnapshot('errors'); + expect(warnings).toMatchSnapshot('warnings'); - expect(cacheKeys.length).toBe(0); - done(); - }); - }); - }); - }); - }); - }); - - it('matches snapshot', () => { - const compiler = createCompiler(); - new UglifyJsPlugin({ cache: true }).apply(compiler); + for (const file in stats.compilation.assets) { + if (Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)) { + expect(stats.compilation.assets[file].source()).toMatchSnapshot(file); + } + } - return compile(compiler) - .then((stats) => { - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); + // Cache disabled so we don't run `get` or `put` + expect(cacache.get.mock.calls.length).toBe(0); + expect(cacache.put.mock.calls.length).toBe(0); - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); + return Promise.resolve() + .then(() => cacache.ls(cacheDir)) + .then((cacheEntriesList) => { + const cacheKeys = Object.keys(cacheEntriesList); - for (const file in stats.compilation.assets) { - if (Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)) { - expect(stats.compilation.assets[file].source()).toMatchSnapshot(file); - } - } + expect(cacheKeys.length).toBe(0); }); }); }); - describe('true', () => { - let eventBindings; - let eventBinding; + it('matches snapshot for `true` value', () => { + new UglifyJsPlugin({ cache: true }).apply(compiler); - beforeAll(() => cacache.rm.all(cacheDir)); + cacache.get = jest.fn(cacache.get); + cacache.put = jest.fn(cacache.put); - afterAll(() => cacache.rm.all(cacheDir)); + return compile(compiler).then((stats) => { + const errors = stats.compilation.errors.map(cleanErrorStack); + const warnings = stats.compilation.warnings.map(cleanErrorStack); - beforeEach(() => { - const pluginEnvironment = new PluginEnvironment(); - const compilerEnv = pluginEnvironment.getEnvironmentStub(); - compilerEnv.context = ''; + expect(errors).toMatchSnapshot('errors'); + expect(warnings).toMatchSnapshot('warnings'); - const plugin = new UglifyJsPlugin({ - cache: true, - }); - plugin.apply(compilerEnv); - eventBindings = pluginEnvironment.getEventBindings(); - }); + for (const file in stats.compilation.assets) { + if (Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)) { + expect(stats.compilation.assets[file].source()).toMatchSnapshot(file); + } + } - it('binds one event handler', () => { - expect(eventBindings.length).toBe(1); - }); + const countAssets = Object.keys(stats.compilation.assets).length; - describe('compilation handler', () => { - beforeEach(() => { - [eventBinding] = eventBindings; - }); - - it('binds to compilation event', () => { - expect(eventBinding.name).toBe('compilation'); - }); - - describe('when called', () => { - let chunkPluginEnvironment; - let compilationEventBindings; - let compilationEventBinding; - let compilation; - let callback; - - beforeEach(() => { - chunkPluginEnvironment = new PluginEnvironment(); - compilation = chunkPluginEnvironment.getEnvironmentStub(); - compilation.assets = Object.assign({}, assets); - compilation.errors = []; - - eventBinding.handler(compilation); - compilationEventBindings = chunkPluginEnvironment.getEventBindings(); - }); + // Try to found cached files, but we don't have their in cache + expect(cacache.get.mock.calls.length).toBe(countAssets); + // Put files in cache + expect(cacache.put.mock.calls.length).toBe(countAssets); - it('binds one event handler', () => { - expect(compilationEventBindings.length).toBe(1); - }); + return Promise.resolve() + .then(() => cacache.ls(cacheDir)) + .then((cacheEntriesList) => { + const cacheKeys = Object.keys(cacheEntriesList); - describe('optimize-chunk-assets handler', () => { - beforeEach(() => { - [compilationEventBinding] = compilationEventBindings; - }); + // Make sure that we cached files + expect(cacheKeys.length).toBe(countAssets); - it('binds to optimize-chunk-assets event', () => { - expect(compilationEventBinding.name).toEqual('optimize-chunk-assets'); - }); - - it('only calls callback once', (done) => { - callback = jest.fn(); - compilationEventBinding.handler([''], () => { - callback(); - expect(callback.mock.calls.length).toBe(1); - done(); - }); - }); + cacheKeys.forEach((cacheEntry) => { + // eslint-disable-next-line no-new-func + const cacheEntryOptions = new Function(`'use strict'\nreturn ${cacheEntry}`)(); + const basename = path.basename(cacheEntryOptions.path); - it('cache files', (done) => { - const files = ['test.js', 'test1.js', 'test2.js', 'test3.js']; - - cacache.get = jest.fn(cacache.get); - cacache.put = jest.fn(cacache.put); - - compilationEventBinding.handler([{ - files, - }], () => { - // Try to found cached files, but we don't have their in cache - expect(cacache.get.mock.calls.length).toBe(4); - // Put files in cache - expect(cacache.put.mock.calls.length).toBe(4); - - cacache - .ls(cacheDir) - .then((cacheEntriesList) => { - const cacheKeys = Object.keys(cacheEntriesList); - - // Make sure that we cached files - expect(cacheKeys.length).toBe(files.length); - cacheKeys.forEach((cacheEntry) => { - // eslint-disable-next-line no-new-func - const cacheEntryOptions = new Function(`'use strict'\nreturn ${cacheEntry}`)(); - - expect([cacheEntryOptions.path, cacheEntryOptions.hash]) - .toMatchSnapshot(cacheEntryOptions.path); - }); - - // Reset compilation assets and mocks - compilation.assets = Object.assign({}, assets); - compilation.errors = []; - - cacache.get.mockClear(); - cacache.put.mockClear(); - - compilationEventBinding.handler([{ - files, - }], () => { - // Now we have cached files so we get their and don't put - expect(cacache.get.mock.calls.length).toBe(4); - expect(cacache.put.mock.calls.length).toBe(0); - - done(); - }); - }); - }); + expect([basename, cacheEntryOptions.hash]).toMatchSnapshot(basename); }); - }); - }); - }); - - it('matches snapshot', () => { - const compiler = createCompiler(); - new UglifyJsPlugin({ cache: true }).apply(compiler); - - return compile(compiler) - .then((stats) => { - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - - for (const file in stats.compilation.assets) { - if (Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)) { - expect(stats.compilation.assets[file].source()).toMatchSnapshot(file); + cacache.get.mockClear(); + cacache.put.mockClear(); + }) + // Run second compilation to ensure cached files will be taken from cache + .then(() => compile(compiler)) + .then((newStats) => { + const newErrors = newStats.compilation.errors.map(cleanErrorStack); + const newWarnings = newStats.compilation.warnings.map(cleanErrorStack); + + expect(newErrors).toMatchSnapshot('errors'); + expect(newWarnings).toMatchSnapshot('warnings'); + + for (const file in newStats.compilation.assets) { + if (Object.prototype.hasOwnProperty.call(newStats.compilation.assets, file)) { + expect(newStats.compilation.assets[file].source()).toMatchSnapshot(file); } } + + const newCountAssets = Object.keys(newStats.compilation.assets).length; + + // Now we have cached files so we get their and don't put + expect(cacache.get.mock.calls.length).toBe(newCountAssets); + expect(cacache.put.mock.calls.length).toBe(0); }); }); }); - describe('string', () => { - const othercacheDir = findCacheDir({ name: 'other-cache-directory' }); - let eventBindings; - let eventBinding; + it('matches snapshot for `other-cache-directory` value (string)', () => { + new UglifyJsPlugin({ cache: otherCacheDir }).apply(compiler); - beforeAll(() => cacache.rm.all(othercacheDir)); + cacache.get = jest.fn(cacache.get); + cacache.put = jest.fn(cacache.put); - afterAll(() => cacache.rm.all(othercacheDir)); + return compile(compiler).then((stats) => { + const errors = stats.compilation.errors.map(cleanErrorStack); + const warnings = stats.compilation.warnings.map(cleanErrorStack); - beforeEach(() => { - const pluginEnvironment = new PluginEnvironment(); - const compilerEnv = pluginEnvironment.getEnvironmentStub(); - compilerEnv.context = ''; + expect(errors).toMatchSnapshot('errors'); + expect(warnings).toMatchSnapshot('warnings'); - const plugin = new UglifyJsPlugin({ - cache: othercacheDir, - }); - plugin.apply(compilerEnv); - eventBindings = pluginEnvironment.getEventBindings(); - }); + for (const file in stats.compilation.assets) { + if (Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)) { + expect(stats.compilation.assets[file].source()).toMatchSnapshot(file); + } + } - it('binds one event handler', () => { - expect(eventBindings.length).toBe(1); - }); + const countAssets = Object.keys(stats.compilation.assets).length; - describe('compilation handler', () => { - beforeEach(() => { - [eventBinding] = eventBindings; - }); - - it('binds to compilation event', () => { - expect(eventBinding.name).toBe('compilation'); - }); - - describe('when called', () => { - let chunkPluginEnvironment; - let compilationEventBindings; - let compilationEventBinding; - let compilation; - let callback; - - beforeEach(() => { - chunkPluginEnvironment = new PluginEnvironment(); - compilation = chunkPluginEnvironment.getEnvironmentStub(); - compilation.assets = Object.assign({}, assets); - compilation.errors = []; - - eventBinding.handler(compilation); - compilationEventBindings = chunkPluginEnvironment.getEventBindings(); - }); + // Try to found cached files, but we don't have their in cache + expect(cacache.get.mock.calls.length).toBe(countAssets); + // Put files in cache + expect(cacache.put.mock.calls.length).toBe(countAssets); - it('binds one event handler', () => { - expect(compilationEventBindings.length).toBe(1); - }); + return Promise.resolve() + .then(() => cacache.ls(otherCacheDir)) + .then((cacheEntriesList) => { + const cacheKeys = Object.keys(cacheEntriesList); - describe('optimize-chunk-assets handler', () => { - beforeEach(() => { - [compilationEventBinding] = compilationEventBindings; - }); + // Make sure that we cached files + expect(cacheKeys.length).toBe(countAssets); - it('binds to optimize-chunk-assets event', () => { - expect(compilationEventBinding.name).toEqual('optimize-chunk-assets'); - }); - - it('only calls callback once', (done) => { - callback = jest.fn(); - compilationEventBinding.handler([''], () => { - callback(); - expect(callback.mock.calls.length).toBe(1); - done(); - }); - }); + cacheKeys.forEach((cacheEntry) => { + // eslint-disable-next-line no-new-func + const cacheEntryOptions = new Function(`'use strict'\nreturn ${cacheEntry}`)(); + const basename = path.basename(cacheEntryOptions.path); - it('cache files', (done) => { - const files = ['test.js', 'test1.js', 'test2.js', 'test3.js']; - - cacache.get = jest.fn(cacache.get); - cacache.put = jest.fn(cacache.put); - - compilationEventBinding.handler([{ - files, - }], () => { - // Try to found cached files, but we don't have their in cache - expect(cacache.get.mock.calls.length).toBe(4); - // Put files in cache - expect(cacache.put.mock.calls.length).toBe(4); - - cacache - .ls(othercacheDir) - .then((cacheEntriesList) => { - const cacheKeys = Object.keys(cacheEntriesList); - - // Make sure that we cached files - expect(cacheKeys.length).toBe(files.length); - cacheKeys.forEach((cacheEntry) => { - // eslint-disable-next-line no-new-func - const cacheEntryOptions = new Function(`'use strict'\nreturn ${cacheEntry}`)(); - - expect([cacheEntryOptions.path, cacheEntryOptions.hash]) - .toMatchSnapshot(cacheEntryOptions.path); - }); - - // Reset compilation assets and mocks - compilation.assets = Object.assign({}, assets); - compilation.errors = []; - - cacache.get.mockClear(); - cacache.put.mockClear(); - - compilationEventBinding.handler([{ - files, - }], () => { - // Now we have cached files so we get their and don't put - expect(cacache.get.mock.calls.length).toBe(4); - expect(cacache.put.mock.calls.length).toBe(0); - - done(); - }); - }); - }); + expect([basename, cacheEntryOptions.hash]).toMatchSnapshot(basename); }); - }); - }); - }); - - it('matches snapshot', () => { - const compiler = createCompiler(); - new UglifyJsPlugin({ cache: othercacheDir }).apply(compiler); - - return compile(compiler) - .then((stats) => { - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - - for (const file in stats.compilation.assets) { - if (Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)) { - expect(stats.compilation.assets[file].source()).toMatchSnapshot(file); + cacache.get.mockClear(); + cacache.put.mockClear(); + }) + // Run second compilation to ensure cached files will be taken from cache + .then(() => compile(compiler)) + .then((newStats) => { + const newErrors = newStats.compilation.errors.map(cleanErrorStack); + const newWarnings = newStats.compilation.warnings.map(cleanErrorStack); + + expect(newErrors).toMatchSnapshot('errors'); + expect(newWarnings).toMatchSnapshot('warnings'); + + for (const file in newStats.compilation.assets) { + if (Object.prototype.hasOwnProperty.call(newStats.compilation.assets, file)) { + expect(newStats.compilation.assets[file].source()).toMatchSnapshot(file); } } - }); - }); - }); - - describe('with cacheKey option', () => { - let eventBindings; - let eventBinding; - beforeAll(() => cacache.rm.all(cacheDir)); + const newCountAssets = Object.keys(newStats.compilation.assets).length; - afterAll(() => cacache.rm.all(cacheDir)); - - beforeEach(() => { - const pluginEnvironment = new PluginEnvironment(); - const compilerEnv = pluginEnvironment.getEnvironmentStub(); - compilerEnv.context = ''; - - const plugin = new UglifyJsPlugin({ - cache: true, - cacheKeys: (defaultCacheKeys, file) => { - // eslint-disable-next-line no-param-reassign - defaultCacheKeys.myCacheKey = 1; - // eslint-disable-next-line no-param-reassign - defaultCacheKeys.myCacheKeyBasedOnFile = `file-${file}`; - - return defaultCacheKeys; - }, - }); - plugin.apply(compilerEnv); - eventBindings = pluginEnvironment.getEventBindings(); - }); - - it('binds one event handler', () => { - expect(eventBindings.length).toBe(1); - }); - - describe('compilation handler', () => { - beforeEach(() => { - [eventBinding] = eventBindings; - }); - - it('binds to compilation event', () => { - expect(eventBinding.name).toBe('compilation'); - }); - - describe('when called', () => { - let chunkPluginEnvironment; - let compilationEventBindings; - let compilationEventBinding; - let compilation; - let callback; - - beforeEach(() => { - chunkPluginEnvironment = new PluginEnvironment(); - compilation = chunkPluginEnvironment.getEnvironmentStub(); - compilation.assets = Object.assign({}, assets); - compilation.errors = []; - - eventBinding.handler(compilation); - compilationEventBindings = chunkPluginEnvironment.getEventBindings(); - }); - - it('binds one event handler', () => { - expect(compilationEventBindings.length).toBe(1); + // Now we have cached files so we get their and don't put + expect(cacache.get.mock.calls.length).toBe(newCountAssets); + expect(cacache.put.mock.calls.length).toBe(0); }); + }); + }); - describe('optimize-chunk-assets handler', () => { - beforeEach(() => { - [compilationEventBinding] = compilationEventBindings; - }); - - it('binds to optimize-chunk-assets event', () => { - expect(compilationEventBinding.name).toEqual('optimize-chunk-assets'); - }); - - it('only calls callback once', (done) => { - callback = jest.fn(); - compilationEventBinding.handler([''], () => { - callback(); - expect(callback.mock.calls.length).toBe(1); - done(); - }); - }); - - it('cache files', (done) => { - const files = ['test.js', 'test1.js', 'test2.js', 'test3.js']; - - cacache.get = jest.fn(cacache.get); - cacache.put = jest.fn(cacache.put); - - compilationEventBinding.handler([{ - files, - }], () => { - // Try to found cached files, but we don't have their in cache - expect(cacache.get.mock.calls.length).toBe(4); - // Put files in cache - expect(cacache.put.mock.calls.length).toBe(4); - - cacache - .ls(cacheDir) - .then((cacheEntriesList) => { - const cacheKeys = Object.keys(cacheEntriesList); - - // Make sure that we cached files - expect(cacheKeys.length).toBe(files.length); - cacheKeys.forEach((cacheEntry) => { - // eslint-disable-next-line no-new-func - const cacheEntryOptions = new Function(`'use strict'\nreturn ${cacheEntry}`)(); - - expect(cacheEntryOptions.myCacheKey).toBe(1); - expect(cacheEntryOptions.myCacheKeyBasedOnFile).toMatch(/file-test(.)?\.js/); - expect([cacheEntryOptions.path, cacheEntryOptions.hash]) - .toMatchSnapshot(cacheEntryOptions.path); - }); - - // Reset compilation assets and mocks - compilation.assets = Object.assign({}, assets); - compilation.errors = []; - - cacache.get.mockClear(); - cacache.put.mockClear(); - - compilationEventBinding.handler([{ - files, - }], () => { - // Now we have cached files so we get their and don't put - expect(cacache.get.mock.calls.length).toBe(4); - expect(cacache.put.mock.calls.length).toBe(0); - - done(); - }); - }); - }); + it('matches snapshot for `true` value and `cacheKey` is custom `function`', () => { + new UglifyJsPlugin({ + cache: true, + cacheKeys: (defaultCacheKeys, file) => { + // eslint-disable-next-line no-param-reassign + defaultCacheKeys.myCacheKey = 1; + // eslint-disable-next-line no-param-reassign + defaultCacheKeys.myCacheKeyBasedOnFile = `file-${file}`; + + return defaultCacheKeys; + }, + }).apply(compiler); + + cacache.get = jest.fn(cacache.get); + cacache.put = jest.fn(cacache.put); + + return compile(compiler).then((stats) => { + const errors = stats.compilation.errors.map(cleanErrorStack); + const warnings = stats.compilation.warnings.map(cleanErrorStack); + + expect(errors).toMatchSnapshot('errors'); + expect(warnings).toMatchSnapshot('warnings'); + + for (const file in stats.compilation.assets) { + if (Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)) { + expect(stats.compilation.assets[file].source()).toMatchSnapshot(file); + } + } + + const countAssets = Object.keys(stats.compilation.assets).length; + + // Try to found cached files, but we don't have their in cache + expect(cacache.get.mock.calls.length).toBe(countAssets); + // Put files in cache + expect(cacache.put.mock.calls.length).toBe(countAssets); + + return Promise.resolve() + .then(() => cacache.ls(cacheDir)) + .then((cacheEntriesList) => { + const cacheKeys = Object.keys(cacheEntriesList); + + // Make sure that we cached files + expect(cacheKeys.length).toBe(countAssets); + + cacheKeys.forEach((cacheEntry) => { + // eslint-disable-next-line no-new-func + const cacheEntryOptions = new Function(`'use strict'\nreturn ${cacheEntry}`)(); + const basename = path.basename(cacheEntryOptions.path); + + expect(cacheEntryOptions.myCacheKey).toBe(1); + expect(cacheEntryOptions.myCacheKeyBasedOnFile).toMatch(/file-(.+)?\.js/); + expect([basename, cacheEntryOptions.hash]).toMatchSnapshot(basename); }); - }); - }); - }); - it('matches snapshot', () => { - const compiler = createCompiler(); - new UglifyJsPlugin({ - cache: true, - cacheKeys: (defaultCacheKeys, file) => { - // eslint-disable-next-line no-param-reassign - defaultCacheKeys.myCacheKey = 1; - // eslint-disable-next-line no-param-reassign - defaultCacheKeys.myCacheLeyBasedOnFile = `file-${file}`; - - return defaultCacheKeys; - }, - }).apply(compiler); - - return compile(compiler) - .then((stats) => { - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - - for (const file in stats.compilation.assets) { - if (Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)) { - expect(stats.compilation.assets[file].source()).toMatchSnapshot(file); + cacache.get.mockClear(); + cacache.put.mockClear(); + }) + // Run second compilation to ensure cached files will be taken from cache + .then(() => compile(compiler)) + .then((newStats) => { + const newErrors = newStats.compilation.errors.map(cleanErrorStack); + const newWarnings = newStats.compilation.warnings.map(cleanErrorStack); + + expect(newErrors).toMatchSnapshot('errors'); + expect(newWarnings).toMatchSnapshot('warnings'); + + for (const file in newStats.compilation.assets) { + if (Object.prototype.hasOwnProperty.call(newStats.compilation.assets, file)) { + expect(newStats.compilation.assets[file].source()).toMatchSnapshot(file); } } + + const newCountAssets = Object.keys(newStats.compilation.assets).length; + + // Now we have cached files so we get their and don't put + expect(cacache.get.mock.calls.length).toBe(newCountAssets); + expect(cacache.put.mock.calls.length).toBe(0); }); }); }); From 9fdb31a5e39cc3571606c785dcf5a91bdf177fd4 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Mon, 30 Jul 2018 19:30:23 +0300 Subject: [PATCH 07/11] refactor: `uglifyOptions` options tests --- .../uglifyOptions-option.test.js.snap | 278 ++++++++++-------- test/uglifyOptions-option.test.js | 199 +++++++++++-- 2 files changed, 336 insertions(+), 141 deletions(-) diff --git a/test/__snapshots__/uglifyOptions-option.test.js.snap b/test/__snapshots__/uglifyOptions-option.test.js.snap index b3c3647a..bfdc46fd 100644 --- a/test/__snapshots__/uglifyOptions-option.test.js.snap +++ b/test/__snapshots__/uglifyOptions-option.test.js.snap @@ -8,33 +8,33 @@ exports[`when applied with uglifyOptions options disable inline optimization by exports[`when applied with uglifyOptions options disable inline optimization by default (have a lot of problems): warnings 1`] = `Array []`; -exports[`when applied with uglifyOptions options matches snapshot for \`compress\` option (boolean false): errors 1`] = `Array []`; +exports[`when applied with uglifyOptions options matches snapshot for \`compress\` option (boolean \`false\` value): errors 1`] = `Array []`; -exports[`when applied with uglifyOptions options matches snapshot for \`compress\` option (boolean false): main.js 1`] = `"webpackJsonp([0],[function(t,e,n){\\"use strict\\";Object.defineProperty(e,\\"__esModule\\",{value:true});var a=n(1);function o(){const t=a[\\"b\\"];const e=\`baz\${Math.random()}\`;return()=>{return{a:t+a[\\"a\\"]+e,b:t,baz:e}}}e[\\"default\\"]=o},function(t,e,n){\\"use strict\\";const a=\\"bar\\";e[\\"a\\"]=a;e[\\"b\\"]=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`compress\` option (boolean \`false\` value): main.js 1`] = `"webpackJsonp([0],[function(t,e,n){\\"use strict\\";Object.defineProperty(e,\\"__esModule\\",{value:true});var a=n(1);function o(){const t=a[\\"b\\"];const e=\`baz\${Math.random()}\`;return()=>{return{a:t+a[\\"a\\"]+e,b:t,baz:e}}}e[\\"default\\"]=o},function(t,e,n){\\"use strict\\";const a=\\"bar\\";e[\\"a\\"]=a;e[\\"b\\"]=\\"foo\\"}],[0]);"`; -exports[`when applied with uglifyOptions options matches snapshot for \`compress\` option (boolean false): manifest.js 1`] = `"(function(r){var e=window[\\"webpackJsonp\\"];window[\\"webpackJsonp\\"]=function n(f,i,u){var c,a,l=0,p=[],s;for(;l({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`compress\` option (boolean \`true\` value): main.js 1`] = `"webpackJsonp([0],[function(t,a,e){\\"use strict\\";Object.defineProperty(a,\\"__esModule\\",{value:!0});var n=e(1);a.default=function(){const t=n.b,a=\`baz\${Math.random()}\`;return()=>({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; -exports[`when applied with uglifyOptions options matches snapshot for \`compress\` option (boolean true): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`compress\` option (object value): main.js 1`] = `"webpackJsonp([0],[function(t,a,e){\\"use strict\\";Object.defineProperty(a,\\"__esModule\\",{value:!0});var n=e(1);a.default=function(){const t=n.b;const a=\`baz\${Math.random()}\`;return()=>({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; -exports[`when applied with uglifyOptions options matches snapshot for \`compress\` option (object): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){var f,i,p,a=0,l=[];for(;a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; + +exports[`when applied with uglifyOptions options matches snapshot for \`ie8\` option (boolean \`false\` value): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; + +exports[`when applied with uglifyOptions options matches snapshot for \`ie8\` option (boolean \`true\` value): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`keep_classnames\` option (boolean \`false\` value): main.js 1`] = `"webpackJsonp([0],[function(t,a,e){\\"use strict\\";Object.defineProperty(a,\\"__esModule\\",{value:!0});var n=e(1);a.default=function(){const t=n.b,a=\`baz\${Math.random()}\`;return()=>({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; -exports[`when applied with uglifyOptions options matches snapshot for \`ie8\` option: manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`keep_classnames\` option (boolean \`true\` value): main.js 1`] = `"webpackJsonp([0],[function(t,a,e){\\"use strict\\";Object.defineProperty(a,\\"__esModule\\",{value:!0});var n=e(1);a.default=function(){const t=n.b,a=\`baz\${Math.random()}\`;return()=>({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; -exports[`when applied with uglifyOptions options matches snapshot for \`keep_classnames\` option: manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+o.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`keep_fnames\` option (boolean \`false\` value): main.js 1`] = `"webpackJsonp([0],[function(t,a,e){\\"use strict\\";Object.defineProperty(a,\\"__esModule\\",{value:!0});var n=e(1);a.default=function(){const t=n.b,a=\`baz\${Math.random()}\`;return()=>({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; -exports[`when applied with uglifyOptions options matches snapshot for \`keep_fnames\` option: manifest.js 1`] = `"!function(e){var r=window.webpackJsonp;window.webpackJsonp=function webpackJsonpCallback(_,n,o){for(var u,c,p,a=0,i=[];a<_.length;a++)c=_[a],t[c]&&i.push(t[c][0]),t[c]=0;for(u in n)Object.prototype.hasOwnProperty.call(n,u)&&(e[u]=n[u]);for(r&&r(_,n,o);i.length;)i.shift()();if(o)for(a=0;a({a:b+__WEBPACK_IMPORTED_MODULE_0__dep__.a+baz,b:b,baz:baz})}},function(module,__webpack_exports__,__webpack_require__){\\"use strict\\";__webpack_exports__.a=\\"bar\\",__webpack_exports__.b=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`keep_fnames\` option (boolean \`true\` value): main.js 1`] = `"webpackJsonp([0],[function(t,a,e){\\"use strict\\";Object.defineProperty(a,\\"__esModule\\",{value:!0});var o=e(1);a.default=function Foo(){const t=o.b,a=\`baz\${Math.random()}\`;return()=>({a:t+o.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; -exports[`when applied with uglifyOptions options matches snapshot for \`mangle\` option (false): manifest.js 1`] = `"!function(modules){var parentJsonpFunction=window.webpackJsonp;window.webpackJsonp=function(chunkIds,moreModules,executeModules){for(var moduleId,chunkId,result,i=0,resolves=[];i({a:t+n.a+baz,b:t,baz:baz})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`mangle\` option (boolean \`false\` value): main.js 1`] = `"webpackJsonp([0],[function(module,__webpack_exports__,__webpack_require__){\\"use strict\\";Object.defineProperty(__webpack_exports__,\\"__esModule\\",{value:!0});var __WEBPACK_IMPORTED_MODULE_0__dep__=__webpack_require__(1);__webpack_exports__.default=function(){const b=__WEBPACK_IMPORTED_MODULE_0__dep__.b,baz=\`baz\${Math.random()}\`;return()=>({a:b+__WEBPACK_IMPORTED_MODULE_0__dep__.a+baz,b:b,baz:baz})}},function(module,__webpack_exports__,__webpack_require__){\\"use strict\\";__webpack_exports__.a=\\"bar\\",__webpack_exports__.b=\\"foo\\"}],[0]);"`; -exports[`when applied with uglifyOptions options matches snapshot for \`mangle\` option (object): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`mangle\` option (boolean \`true\` value): main.js 1`] = `"webpackJsonp([0],[function(t,a,e){\\"use strict\\";Object.defineProperty(a,\\"__esModule\\",{value:!0});var n=e(1);a.default=function(){const t=n.b,a=\`baz\${Math.random()}\`;return()=>({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; -exports[`when applied with uglifyOptions options matches snapshot for \`mangle\` option (true): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`mangle\` option (object value): main.js 1`] = `"webpackJsonp([0],[function(t,a,e){\\"use strict\\";Object.defineProperty(a,\\"__esModule\\",{value:!0});var n=e(1);a.default=function(){const t=n.b,baz=\`baz\${Math.random()}\`;return()=>({a:t+n.a+baz,b:t,baz:baz})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; -exports[`when applied with uglifyOptions options matches snapshot for \`nameCache\` option: manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; + +exports[`when applied with uglifyOptions options matches snapshot for \`nameCache\` option (empty object value): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; + +exports[`when applied with uglifyOptions options matches snapshot for \`parse\` option (object value): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; + +exports[`when applied with uglifyOptions options matches snapshot for \`safari10\` option (boolean \`false\` value): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`safari10\` option (boolean \`true\` value): main.js 1`] = `"webpackJsonp([0],[function(t,a,e){\\"use strict\\";Object.defineProperty(a,\\"__esModule\\",{value:!0});var n=e(1);a.default=function(){const t=n.b,a=\`baz\${Math.random()}\`;return()=>({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; -exports[`when applied with uglifyOptions options matches snapshot for \`parse\` options: manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`toplevel\` option (boolean \`false\` value): main.js 1`] = `"webpackJsonp([0],[function(t,a,e){\\"use strict\\";Object.defineProperty(a,\\"__esModule\\",{value:!0});var n=e(1);a.default=function(){const t=n.b,a=\`baz\${Math.random()}\`;return()=>({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; -exports[`when applied with uglifyOptions options matches snapshot for \`safari10\` option: manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`toplevel\` option (boolean \`true\` value): main.js 1`] = `"webpackJsonp([0],[function(t,a,e){\\"use strict\\";Object.defineProperty(a,\\"__esModule\\",{value:!0});var n=e(1);a.default=function(){const t=n.b,a=\`baz\${Math.random()}\`;return()=>({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; -exports[`when applied with uglifyOptions options matches snapshot for \`toplevel\` option: manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a { - it('matches snapshot for `ecma` option (ecma 5)', () => { + it('matches snapshot for `ecma` option (value is `5`)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/ecma-5/entry.js`, output: { @@ -42,7 +42,7 @@ describe('when applied with uglifyOptions options', () => { }); }); - it('matches snapshot for `ecma` option (ecma 8)', () => { + it('matches snapshot for `ecma` option (value is `6`)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/ecma-6/entry.js`, output: { @@ -78,7 +78,7 @@ describe('when applied with uglifyOptions options', () => { }); }); - it('matches snapshot for `ecma` option (ecma 7)', () => { + it('matches snapshot for `ecma` option (value is `7`)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/ecma-7/entry.js`, output: { @@ -113,7 +113,7 @@ describe('when applied with uglifyOptions options', () => { }); }); - it('matches snapshot for `ecma` option (ecma 8)', () => { + it('matches snapshot for `ecma` option (value is `8`)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/ecma-8/entry.js`, output: { @@ -149,7 +149,7 @@ describe('when applied with uglifyOptions options', () => { }); }); - it('matches snapshot for `warnings` option (boolean false)', () => { + it('matches snapshot for `warnings` option (boolean `true` value)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/unreachable-code.js`, output: { @@ -161,7 +161,7 @@ describe('when applied with uglifyOptions options', () => { new UglifyJsPlugin({ uglifyOptions: { - warnings: false, + warnings: true, }, }).apply(compiler); @@ -180,7 +180,7 @@ describe('when applied with uglifyOptions options', () => { }); }); - it('matches snapshot for `warnings` option (boolean true)', () => { + it('matches snapshot for `warnings` option (boolean `false` value)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/unreachable-code.js`, output: { @@ -192,7 +192,7 @@ describe('when applied with uglifyOptions options', () => { new UglifyJsPlugin({ uglifyOptions: { - warnings: true, + warnings: false, }, }).apply(compiler); @@ -211,7 +211,7 @@ describe('when applied with uglifyOptions options', () => { }); }); - it('matches snapshot for `parse` options', () => { + it('matches snapshot for `parse` option (object value)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/import-export/entry.js`, output: { @@ -244,7 +244,7 @@ describe('when applied with uglifyOptions options', () => { }); }); - it('matches snapshot for `compress` option (boolean true)', () => { + it('matches snapshot for `compress` option (boolean `true` value)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/import-export/entry.js`, output: { @@ -275,7 +275,7 @@ describe('when applied with uglifyOptions options', () => { }); }); - it('matches snapshot for `compress` option (boolean false)', () => { + it('matches snapshot for `compress` option (boolean `false` value)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/import-export/entry.js`, output: { @@ -306,7 +306,7 @@ describe('when applied with uglifyOptions options', () => { }); }); - it('matches snapshot for `compress` option (object)', () => { + it('matches snapshot for `compress` option (object value)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/import-export/entry.js`, output: { @@ -339,7 +339,7 @@ describe('when applied with uglifyOptions options', () => { }); }); - it('matches snapshot for `mangle` option (true)', () => { + it('matches snapshot for `mangle` option (boolean `true` value)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/import-export/entry.js`, output: { @@ -370,7 +370,7 @@ describe('when applied with uglifyOptions options', () => { }); }); - it('matches snapshot for `mangle` option (false)', () => { + it('matches snapshot for `mangle` option (boolean `false` value)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/import-export/entry.js`, output: { @@ -401,7 +401,7 @@ describe('when applied with uglifyOptions options', () => { }); }); - it('matches snapshot for `mangle` option (object)', () => { + it('matches snapshot for `mangle` option (object value)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/import-export/entry.js`, output: { @@ -434,7 +434,7 @@ describe('when applied with uglifyOptions options', () => { }); }); - it('matches snapshot for `output` option', () => { + it('matches snapshot for `output` option (object value)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/import-export/entry.js`, output: { @@ -467,7 +467,7 @@ describe('when applied with uglifyOptions options', () => { }); }); - it('matches snapshot for `toplevel` option', () => { + it('matches snapshot for `toplevel` option (boolean `true` value)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/import-export/entry.js`, output: { @@ -498,7 +498,38 @@ describe('when applied with uglifyOptions options', () => { }); }); - it('matches snapshot for `nameCache` option', () => { + it('matches snapshot for `toplevel` option (boolean `false` value)', () => { + const compiler = createCompiler({ + entry: `${__dirname}/fixtures/import-export/entry.js`, + output: { + path: `${__dirname}/dist-import-export`, + filename: '[name].js', + chunkFilename: '[id].[name].js', + }, + }); + + new UglifyJsPlugin({ + uglifyOptions: { + toplevel: false, + }, + }).apply(compiler); + + return compile(compiler).then((stats) => { + const errors = stats.compilation.errors.map(cleanErrorStack); + const warnings = stats.compilation.warnings.map(cleanErrorStack); + + expect(errors).toMatchSnapshot('errors'); + expect(warnings).toMatchSnapshot('warnings'); + + for (const file in stats.compilation.assets) { + if (Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)) { + expect(stats.compilation.assets[file].source()).toMatchSnapshot(file); + } + } + }); + }); + + it('matches snapshot for `nameCache` option (empty object value)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/import-export/entry.js`, output: { @@ -529,7 +560,7 @@ describe('when applied with uglifyOptions options', () => { }); }); - it('matches snapshot for `ie8` option', () => { + it('matches snapshot for `ie8` option (boolean `true` value)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/import-export/entry.js`, output: { @@ -560,7 +591,38 @@ describe('when applied with uglifyOptions options', () => { }); }); - it('matches snapshot for `keep_classnames` option', () => { + it('matches snapshot for `ie8` option (boolean `false` value)', () => { + const compiler = createCompiler({ + entry: `${__dirname}/fixtures/import-export/entry.js`, + output: { + path: `${__dirname}/dist-import-export`, + filename: '[name].js', + chunkFilename: '[id].[name].js', + }, + }); + + new UglifyJsPlugin({ + uglifyOptions: { + ie8: false, + }, + }).apply(compiler); + + return compile(compiler).then((stats) => { + const errors = stats.compilation.errors.map(cleanErrorStack); + const warnings = stats.compilation.warnings.map(cleanErrorStack); + + expect(errors).toMatchSnapshot('errors'); + expect(warnings).toMatchSnapshot('warnings'); + + for (const file in stats.compilation.assets) { + if (Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)) { + expect(stats.compilation.assets[file].source()).toMatchSnapshot(file); + } + } + }); + }); + + it('matches snapshot for `keep_classnames` option (boolean `true` value)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/import-export/entry.js`, output: { @@ -591,7 +653,38 @@ describe('when applied with uglifyOptions options', () => { }); }); - it('matches snapshot for `keep_fnames` option', () => { + it('matches snapshot for `keep_classnames` option (boolean `false` value)', () => { + const compiler = createCompiler({ + entry: `${__dirname}/fixtures/import-export/entry.js`, + output: { + path: `${__dirname}/dist-import-export`, + filename: '[name].js', + chunkFilename: '[id].[name].js', + }, + }); + + new UglifyJsPlugin({ + uglifyOptions: { + keep_classnames: false, + }, + }).apply(compiler); + + return compile(compiler).then((stats) => { + const errors = stats.compilation.errors.map(cleanErrorStack); + const warnings = stats.compilation.warnings.map(cleanErrorStack); + + expect(errors).toMatchSnapshot('errors'); + expect(warnings).toMatchSnapshot('warnings'); + + for (const file in stats.compilation.assets) { + if (Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)) { + expect(stats.compilation.assets[file].source()).toMatchSnapshot(file); + } + } + }); + }); + + it('matches snapshot for `keep_fnames` option (boolean `true` value)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/import-export/entry.js`, output: { @@ -622,7 +715,38 @@ describe('when applied with uglifyOptions options', () => { }); }); - it('matches snapshot for `safari10` option', () => { + it('matches snapshot for `keep_fnames` option (boolean `false` value)', () => { + const compiler = createCompiler({ + entry: `${__dirname}/fixtures/import-export/entry.js`, + output: { + path: `${__dirname}/dist-import-export`, + filename: '[name].js', + chunkFilename: '[id].[name].js', + }, + }); + + new UglifyJsPlugin({ + uglifyOptions: { + keep_fnames: false, + }, + }).apply(compiler); + + return compile(compiler).then((stats) => { + const errors = stats.compilation.errors.map(cleanErrorStack); + const warnings = stats.compilation.warnings.map(cleanErrorStack); + + expect(errors).toMatchSnapshot('errors'); + expect(warnings).toMatchSnapshot('warnings'); + + for (const file in stats.compilation.assets) { + if (Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)) { + expect(stats.compilation.assets[file].source()).toMatchSnapshot(file); + } + } + }); + }); + + it('matches snapshot for `safari10` option (boolean `true` value)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/import-export/entry.js`, output: { @@ -653,6 +777,37 @@ describe('when applied with uglifyOptions options', () => { }); }); + it('matches snapshot for `safari10` option (boolean `false` value)', () => { + const compiler = createCompiler({ + entry: `${__dirname}/fixtures/import-export/entry.js`, + output: { + path: `${__dirname}/dist-import-export`, + filename: '[name].js', + chunkFilename: '[id].[name].js', + }, + }); + + new UglifyJsPlugin({ + uglifyOptions: { + safari10: false, + }, + }).apply(compiler); + + return compile(compiler).then((stats) => { + const errors = stats.compilation.errors.map(cleanErrorStack); + const warnings = stats.compilation.warnings.map(cleanErrorStack); + + expect(errors).toMatchSnapshot('errors'); + expect(warnings).toMatchSnapshot('warnings'); + + for (const file in stats.compilation.assets) { + if (Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)) { + expect(stats.compilation.assets[file].source()).toMatchSnapshot(file); + } + } + }); + }); + it('disable inline optimization by default (have a lot of problems)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/inline-optimization.js`, From 35d357aad5ab12606c9698bdb74ec09200ef74eb Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Mon, 30 Jul 2018 20:05:16 +0300 Subject: [PATCH 08/11] refactor: `warningsFilter` option tests --- .../warningsFilter-option.test.js.snap | 33 +++ test/all-options.test.js | 198 ------------------ test/fixtures/unreachable-code-2.js | 1 + test/warningsFilter-option.test.js | 81 +++++++ 4 files changed, 115 insertions(+), 198 deletions(-) create mode 100644 test/__snapshots__/warningsFilter-option.test.js.snap create mode 100644 test/fixtures/unreachable-code-2.js create mode 100644 test/warningsFilter-option.test.js diff --git a/test/__snapshots__/warningsFilter-option.test.js.snap b/test/__snapshots__/warningsFilter-option.test.js.snap new file mode 100644 index 00000000..5b005be9 --- /dev/null +++ b/test/__snapshots__/warningsFilter-option.test.js.snap @@ -0,0 +1,33 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`when applied with \`warningsFilter\` option matches snapshot for a \`function\` value and \`sourceMap\` is \`false\`: errors 1`] = `Array []`; + +exports[`when applied with \`warningsFilter\` option matches snapshot for a \`function\` value and \`sourceMap\` is \`false\`: manifest.7064adc867fd70846e42.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a { /* eslint-enable no-underscore-dangle */ }); }); - - describe('with warningsFilter set', () => { - describe('and the filter returns true without source map', () => { - beforeEach(() => { - const pluginEnvironment = new PluginEnvironment(); - const compilerEnv = pluginEnvironment.getEnvironmentStub(); - compilerEnv.context = ''; - - const plugin = new UglifyJsPlugin({ - warningsFilter: () => true, - sourceMap: true, - uglifyOptions: { - warnings: true, - mangle: false, - output: { - beautify: true, - }, - }, - }); - plugin.apply(compilerEnv); - eventBindings = pluginEnvironment.getEventBindings(); - - chunkPluginEnvironment = new PluginEnvironment(); - compilation = chunkPluginEnvironment.getEnvironmentStub(); - compilation.assets = { - 'test.js': { - source: () => 'function foo(x) { if (x) { return bar(); not_called1(); } }', - }, - }; - compilation.errors = []; - compilation.warnings = []; - - eventBindings[0].handler(compilation); - compilationEventBindings = chunkPluginEnvironment.getEventBindings(); - }); - - it('should get all warnings', () => { - compilationEventBindings[1].handler([{ - files: ['test.js'], - }], () => { - expect(compilation.warnings.length).toBe(1); - expect(compilation.warnings[0]).toBeInstanceOf(Error); - expect(compilation.warnings[0].message).toEqual(expect.stringContaining('Dropping unreachable code')); - }); - }); - }); - - describe('and the filter returns true with source map', () => { - beforeEach(() => { - const pluginEnvironment = new PluginEnvironment(); - const compilerEnv = pluginEnvironment.getEnvironmentStub(); - compilerEnv.context = ''; - - const plugin = new UglifyJsPlugin({ - warningsFilter: () => true, - sourceMap: true, - uglifyOptions: { - warnings: true, - mangle: false, - output: { - beautify: true, - }, - }, - }); - plugin.apply(compilerEnv); - eventBindings = pluginEnvironment.getEventBindings(); - - chunkPluginEnvironment = new PluginEnvironment(); - compilation = chunkPluginEnvironment.getEnvironmentStub(); - compilation.assets = { - 'test.js': { - sourceAndMap: () => { - return { - source: 'function foo(x) { if (x) { return bar(); not_called1(); } }', - map: { - version: 3, - sources: ['test.js'], - names: ['foo', 'x', 'bar', 'not_called1'], - mappings: 'AAAA,QAASA,KAAIC,GACT,GAAIA,EAAG,CACH,MAAOC,MACPC', - }, - }; - }, - }, - }; - compilation.errors = []; - compilation.warnings = []; - - eventBindings[0].handler(compilation); - compilationEventBindings = chunkPluginEnvironment.getEventBindings(); - }); - - it('should get all warnings', () => { - compilationEventBindings[1].handler([{ - files: ['test.js'], - }], () => { - expect(compilation.warnings.length).toBe(1); - expect(compilation.warnings[0]).toBeInstanceOf(Error); - expect(compilation.warnings[0].message).toEqual(expect.stringContaining('Dropping unreachable code')); - }); - }); - }); - - describe('and the filter returns false without source map', () => { - beforeEach(() => { - const pluginEnvironment = new PluginEnvironment(); - const compilerEnv = pluginEnvironment.getEnvironmentStub(); - compilerEnv.context = ''; - - const plugin = new UglifyJsPlugin({ - warningsFilter: () => false, - sourceMap: true, - uglifyOptions: { - warnings: true, - mangle: false, - output: { - beautify: true, - }, - }, - }); - plugin.apply(compilerEnv); - eventBindings = pluginEnvironment.getEventBindings(); - - chunkPluginEnvironment = new PluginEnvironment(); - compilation = chunkPluginEnvironment.getEnvironmentStub(); - compilation.assets = { - 'test.js': { - source: () => 'function foo(x) { if (x) { return bar(); not_called1(); } }', - }, - }; - compilation.errors = []; - compilation.warnings = []; - - eventBindings[0].handler(compilation); - compilationEventBindings = chunkPluginEnvironment.getEventBindings(); - }); - - it('should get no warnings', () => { - compilationEventBindings[1].handler([{ - files: ['test.js'], - }], () => { - expect(compilation.warnings.length).toBe(0); - }); - }); - }); - - describe('and the filter returns false with source map', () => { - beforeEach(() => { - const pluginEnvironment = new PluginEnvironment(); - const compilerEnv = pluginEnvironment.getEnvironmentStub(); - compilerEnv.context = ''; - - const plugin = new UglifyJsPlugin({ - warningsFilter: () => false, - sourceMap: true, - uglifyOptions: { - warnings: true, - mangle: false, - output: { - beautify: true, - }, - }, - }); - plugin.apply(compilerEnv); - eventBindings = pluginEnvironment.getEventBindings(); - - chunkPluginEnvironment = new PluginEnvironment(); - compilation = chunkPluginEnvironment.getEnvironmentStub(); - compilation.assets = { - 'test.js': { - sourceAndMap: () => { - return { - source: 'function foo(x) { if (x) { return bar(); not_called1(); } }', - map: { - version: 3, - sources: ['test.js'], - names: ['foo', 'x', 'bar', 'not_called1'], - mappings: 'AAAA,QAASA,KAAIC,GACT,GAAIA,EAAG,CACH,MAAOC,MACPC', - }, - }; - }, - }, - }; - compilation.errors = []; - compilation.warnings = []; - - eventBindings[0].handler(compilation); - compilationEventBindings = chunkPluginEnvironment.getEventBindings(); - }); - - it('should get no warnings', () => { - compilationEventBindings[1].handler([{ - files: ['test.js'], - }], () => { - expect(compilation.warnings.length).toBe(0); - }); - }); - }); - }); }); }); }); diff --git a/test/fixtures/unreachable-code-2.js b/test/fixtures/unreachable-code-2.js new file mode 100644 index 00000000..f7abb5ab --- /dev/null +++ b/test/fixtures/unreachable-code-2.js @@ -0,0 +1 @@ +function foo(x) { if (x) { return bar(); not_called1(); } } diff --git a/test/warningsFilter-option.test.js b/test/warningsFilter-option.test.js new file mode 100644 index 00000000..55145589 --- /dev/null +++ b/test/warningsFilter-option.test.js @@ -0,0 +1,81 @@ +import UglifyJsPlugin from '../src/index'; +import { + cleanErrorStack, + createCompiler, + compile, +} from './helpers'; + +describe('when applied with `warningsFilter` option', () => { + let compiler; + + beforeEach(() => { + compiler = createCompiler({ + entry: { + one: `${__dirname}/fixtures/unreachable-code.js`, + two: `${__dirname}/fixtures/unreachable-code-2.js`, + }, + }); + }); + + it('matches snapshot for a `function` value and `sourceMap` is `true`', () => { + new UglifyJsPlugin({ + warningsFilter(source) { + if (/unreachable-code-2\.js/.test(source)) { + return true; + } + + return false; + }, + uglifyOptions: { + warnings: true, + }, + sourceMap: true, + }).apply(compiler); + + + return compile(compiler).then((stats) => { + const errors = stats.compilation.errors.map(cleanErrorStack); + const warnings = stats.compilation.warnings.map(cleanErrorStack); + + expect(errors).toMatchSnapshot('errors'); + expect(warnings).toMatchSnapshot('warnings'); + + for (const file in stats.compilation.assets) { + if (Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)) { + expect(stats.compilation.assets[file].source()).toMatchSnapshot(file); + } + } + }); + }); + + it('matches snapshot for a `function` value and `sourceMap` is `false`', () => { + new UglifyJsPlugin({ + warningsFilter(source) { + if (/unreachable-code-2\.js/.test(source)) { + return true; + } + + return false; + }, + uglifyOptions: { + warnings: true, + }, + sourceMap: false, + }).apply(compiler); + + + return compile(compiler).then((stats) => { + const errors = stats.compilation.errors.map(cleanErrorStack); + const warnings = stats.compilation.warnings.map(cleanErrorStack); + + expect(errors).toMatchSnapshot('errors'); + expect(warnings).toMatchSnapshot('warnings'); + + for (const file in stats.compilation.assets) { + if (Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)) { + expect(stats.compilation.assets[file].source()).toMatchSnapshot(file); + } + } + }); + }); +}); From 5117df59439be17ed2316ca22dee4d8ce8506cd3 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Mon, 30 Jul 2018 20:49:22 +0300 Subject: [PATCH 09/11] refactor: remove `extractComments` duplicate test --- test/extractComments-option.test.js | 53 ----------------------------- 1 file changed, 53 deletions(-) diff --git a/test/extractComments-option.test.js b/test/extractComments-option.test.js index 8639b923..0f65684c 100644 --- a/test/extractComments-option.test.js +++ b/test/extractComments-option.test.js @@ -53,59 +53,6 @@ describe('when options.extractComments', () => { }); }); - it('outputs warnings for unreachable code', () => { - const pluginEnvironment = new PluginEnvironment(); - const compilerEnv = pluginEnvironment.getEnvironmentStub(); - compilerEnv.context = ''; - - const plugin = new UglifyJsPlugin({ - uglifyOptions: { - warnings: true, - mangle: { - properties: { - builtins: true, - }, - }, - }, - extractComments: true, - }); - plugin.apply(compilerEnv); - - const [eventBinding] = pluginEnvironment.getEventBindings(); - const chunkPluginEnvironment = new PluginEnvironment(); - - const compilation = chunkPluginEnvironment.getEnvironmentStub(); - compilation.assets = { - 'test.js': { - source: () => 'var foo = 1;', - }, - 'test1.js': { - source: () => 'function foo(x) { if (x) { return bar(); not_called1(); } }', - map: () => { - return { - version: 3, - sources: ['test1.js'], - names: ['foo', 'x', 'bar', 'not_called1'], - mappings: 'AAAA,QAASA,KAAIC,GACT,GAAIA,EAAG,CACH,MAAOC,MACPC', - }; - }, - }, - }; - compilation.warnings = []; - compilation.errors = []; - - eventBinding.handler(compilation); - const [compilationEventBinding] = chunkPluginEnvironment.getEventBindings(); - - compilationEventBinding.handler([{ - files: ['test.js', 'test1.js'], - }], () => { - expect(compilation.warnings.length).toBe(1); - expect(compilation.warnings[0]).toBeInstanceOf(Error); - expect(compilation.warnings[0].message).toEqual(expect.stringContaining('Dropping unreachable code')); - }); - }); - it('normalizes when options.extractComments is not specify', () => { const pluginEnvironment = new PluginEnvironment(); const compilerEnv = pluginEnvironment.getEnvironmentStub(); From 2b1bd31cb7ab0e14cb2acadafb18215e8bb0df45 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Mon, 30 Jul 2018 20:59:28 +0300 Subject: [PATCH 10/11] refactor: move some tests from `all-options` to `UglifyHsPlugin` tests --- test/UglifyJsPlugin.test.js | 6 ++-- test/all-options.test.js | 59 ------------------------------------- 2 files changed, 3 insertions(+), 62 deletions(-) diff --git a/test/UglifyJsPlugin.test.js b/test/UglifyJsPlugin.test.js index 48a5aa21..27ec597c 100644 --- a/test/UglifyJsPlugin.test.js +++ b/test/UglifyJsPlugin.test.js @@ -54,6 +54,7 @@ describe('UglifyJsPlugin', () => { source: () => '/** @preserve Foo Bar */ function foo(longVariableName) { longVariableName = 1; }', }, }; + compilation.warnings = []; compilation.errors = []; eventBinding.handler(compilation); @@ -89,7 +90,7 @@ describe('UglifyJsPlugin', () => { }); }); - it('early returns if private property is already set', () => { + it('empty asset', () => { compilationEventBinding.handler([{ files: ['test.js'], }], () => { @@ -139,8 +140,7 @@ describe('UglifyJsPlugin', () => { files: ['test3.js'], }], () => { // eslint-disable-next-line no-underscore-dangle - expect(compilation.assets['test3.js']._value) - .not.toEqual(expect.stringContaining('longVariableName')); + expect(compilation.assets['test3.js']._value).not.toEqual(expect.stringContaining('longVariableName')); }); }); diff --git a/test/all-options.test.js b/test/all-options.test.js index d5cb2361..00f13fd9 100644 --- a/test/all-options.test.js +++ b/test/all-options.test.js @@ -1,4 +1,3 @@ -import { SourceMapSource } from 'webpack-sources'; import UglifyJsPlugin from '../src/index'; import { PluginEnvironment, @@ -207,32 +206,6 @@ describe('when applied with all options', () => { }); }); - it('outputs SourceMapSource for valid javascript', () => { - compilationEventBinding.handler([{ - files: ['test.js'], - }], () => { - expect(compilation.assets['test.js']).toBeInstanceOf(SourceMapSource); - }); - }); - - it('does not output mangled javascript', () => { - compilationEventBinding.handler([{ - files: ['test.js'], - }], () => { - // eslint-disable-next-line no-underscore-dangle - expect(compilation.assets['test.js']._value).toEqual(expect.stringContaining('longVariableName')); - }); - }); - - it('outputs beautified javascript', () => { - compilationEventBinding.handler([{ - files: ['test.js'], - }], () => { - // eslint-disable-next-line no-underscore-dangle - expect(compilation.assets['test.js']._value).toEqual(expect.stringContaining('\n')); - }); - }); - it('does not preserve comments', () => { compilationEventBinding.handler([{ files: ['test.js'], @@ -242,16 +215,6 @@ describe('when applied with all options', () => { }); }); - it('outputs parsing errors', () => { - compilationEventBinding.handler([{ - files: ['test1.js'], - }], () => { - expect(compilation.errors.length).toBe(1); - expect(compilation.errors[0]).toBeInstanceOf(Error); - expect(compilation.errors[0].message).toEqual(expect.stringContaining('[test1.js:1,0][test1.js:1,8]')); - }); - }); - it('outputs warnings for unreachable code', () => { compilationEventBinding.handler([{ files: ['test2.js'], @@ -261,28 +224,6 @@ describe('when applied with all options', () => { expect(compilation.warnings[0].message).toEqual(expect.stringContaining('Dropping unreachable code')); }); }); - - it('works with sourceAndMap assets as well', () => { - compilationEventBinding.handler([{ - files: ['test3.js'], - }], () => { - expect(compilation.errors.length).toBe(0); - expect(compilation.assets['test3.js']).toBeInstanceOf(SourceMapSource); - }); - }); - - it('extracts license information to separate file', () => { - compilationEventBinding.handler([{ - files: ['test4.js'], - }], () => { - expect(compilation.errors.length).toBe(0); - /* eslint-disable no-underscore-dangle */ - expect(compilation.assets['test4.license.js'].source()).toContain('/*! this comment should be extracted */'); - expect(compilation.assets['test4.license.js'].source()).toContain('// another comment that should be extracted to a separate file'); - expect(compilation.assets['test4.license.js'].source()).not.toEqual(expect.stringContaining('/* this will not be extracted */')); - /* eslint-enable no-underscore-dangle */ - }); - }); }); }); }); From 58fb6f58d6b584df96ea60df1e8b2b93ca6bcddb Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Mon, 30 Jul 2018 21:18:22 +0300 Subject: [PATCH 11/11] refactor: remove duplicated tests --- test/__snapshots__/all-options.test.js.snap | 62 ----- .../uglifyOptions-option.test.js.snap | 244 ++++++++++++------ test/all-options.test.js | 230 ----------------- test/uglifyOptions-option.test.js | 232 ++++------------- 4 files changed, 217 insertions(+), 551 deletions(-) delete mode 100644 test/__snapshots__/all-options.test.js.snap delete mode 100644 test/all-options.test.js diff --git a/test/__snapshots__/all-options.test.js.snap b/test/__snapshots__/all-options.test.js.snap deleted file mode 100644 index e10ef8e2..00000000 --- a/test/__snapshots__/all-options.test.js.snap +++ /dev/null @@ -1,62 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`when applied with all options matches snapshot: errors 1`] = `Array []`; - -exports[`when applied with all options matches snapshot: main.0c220ec66316af2c1b24.js 1`] = ` -"webpackJsonp([ 0 ], [ function(module, exports) { - module.exports = function() { - console.log(7); - }; -} ], [ 0 ]);" -`; - -exports[`when applied with all options matches snapshot: manifest.d6857f782c13a99b5917.js 1`] = ` -"!function(modules) { - var parentJsonpFunction = window.webpackJsonp; - window.webpackJsonp = function(chunkIds, moreModules, executeModules) { - for (var moduleId, chunkId, result, i = 0, resolves = []; i < chunkIds.length; i++) chunkId = chunkIds[i], - installedChunks[chunkId] && resolves.push(installedChunks[chunkId][0]), installedChunks[chunkId] = 0; - for (moduleId in moreModules) Object.prototype.hasOwnProperty.call(moreModules, moduleId) && (modules[moduleId] = moreModules[moduleId]); - for (parentJsonpFunction && parentJsonpFunction(chunkIds, moreModules, executeModules); resolves.length; ) resolves.shift()(); - if (executeModules) for (i = 0; i < executeModules.length; i++) result = __webpack_require__(__webpack_require__.s = executeModules[i]); - return result; - }; - var installedModules = {}, installedChunks = { - 1: 0 - }; - function __webpack_require__(moduleId) { - if (installedModules[moduleId]) return installedModules[moduleId].exports; - var module = installedModules[moduleId] = { - i: moduleId, - l: !1, - exports: {} - }; - return modules[moduleId].call(module.exports, module, module.exports, __webpack_require__), - module.l = !0, module.exports; - } - __webpack_require__.m = modules, __webpack_require__.c = installedModules, __webpack_require__.d = function(exports, name, getter) { - __webpack_require__.o(exports, name) || Object.defineProperty(exports, name, { - configurable: !1, - enumerable: !0, - get: getter - }); - }, __webpack_require__.n = function(module) { - var getter = module && module.__esModule ? function() { - return module.default; - } : function() { - return module; - }; - return __webpack_require__.d(getter, \\"a\\", getter), getter; - }, __webpack_require__.o = function(object, property) { - return Object.prototype.hasOwnProperty.call(object, property); - }, __webpack_require__.p = \\"\\", __webpack_require__.oe = function(err) { - throw console.error(err), err; - }; -}([]);" -`; - -exports[`when applied with all options matches snapshot: warnings 1`] = ` -Array [ - "UglifyJs Plugin: Dropping unused variable a [./test/fixtures/entry.js:4,0] in main.0c220ec66316af2c1b24.js", -] -`; diff --git a/test/__snapshots__/uglifyOptions-option.test.js.snap b/test/__snapshots__/uglifyOptions-option.test.js.snap index bfdc46fd..d940f7b8 100644 --- a/test/__snapshots__/uglifyOptions-option.test.js.snap +++ b/test/__snapshots__/uglifyOptions-option.test.js.snap @@ -2,39 +2,43 @@ exports[`when applied with uglifyOptions options disable inline optimization by default (have a lot of problems): errors 1`] = `Array []`; -exports[`when applied with uglifyOptions options disable inline optimization by default (have a lot of problems): main.js 1`] = `"webpackJsonp([0],[function(o,c,n){\\"use strict\\";console.log(42)}],[0]);"`; +exports[`when applied with uglifyOptions options disable inline optimization by default (have a lot of problems): main.9150fdfbc7675d270df7.js 1`] = `"webpackJsonp([0],[function(o,c,n){\\"use strict\\";console.log(42)}],[0]);"`; -exports[`when applied with uglifyOptions options disable inline optimization by default (have a lot of problems): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a{return{a:t+a[\\"a\\"]+e,b:t,baz:e}}}e[\\"default\\"]=o},function(t,e,n){\\"use strict\\";const a=\\"bar\\";e[\\"a\\"]=a;e[\\"b\\"]=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`compress\` option (boolean \`false\` value): main.0c220ec66316af2c1b24.js 1`] = ` +"webpackJsonp([0],[function(o,n){ +/* @preserve*/ +const c=2+2;o.exports=function o(){const n=2+2;console.log(n+1+2)}}],[0]);" +`; -exports[`when applied with uglifyOptions options matches snapshot for \`compress\` option (boolean \`false\` value): manifest.js 1`] = `"(function(r){var e=window[\\"webpackJsonp\\"];window[\\"webpackJsonp\\"]=function n(f,i,u){var c,a,l=0,p=[],s;for(;l({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`compress\` option (boolean \`true\` value): main.0c220ec66316af2c1b24.js 1`] = `"webpackJsonp([0],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`; -exports[`when applied with uglifyOptions options matches snapshot for \`compress\` option (boolean \`true\` value): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`compress\` option (object value): main.0c220ec66316af2c1b24.js 1`] = `"webpackJsonp([0],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`; -exports[`when applied with uglifyOptions options matches snapshot for \`compress\` option (object value): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){var f,i,p,a=0,l=[];for(;a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`ie8\` option (boolean \`false\` value): main.0c220ec66316af2c1b24.js 1`] = `"webpackJsonp([0],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`; -exports[`when applied with uglifyOptions options matches snapshot for \`ie8\` option (boolean \`false\` value): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`ie8\` option (boolean \`true\` value): main.0c220ec66316af2c1b24.js 1`] = `"webpackJsonp([0],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`; -exports[`when applied with uglifyOptions options matches snapshot for \`ie8\` option (boolean \`true\` value): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`keep_classnames\` option (boolean \`false\` value): main.0c220ec66316af2c1b24.js 1`] = `"webpackJsonp([0],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`; -exports[`when applied with uglifyOptions options matches snapshot for \`keep_classnames\` option (boolean \`false\` value): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`keep_classnames\` option (boolean \`true\` value): main.0c220ec66316af2c1b24.js 1`] = `"webpackJsonp([0],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`; -exports[`when applied with uglifyOptions options matches snapshot for \`keep_classnames\` option (boolean \`true\` value): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`keep_fnames\` option (boolean \`false\` value): main.0c220ec66316af2c1b24.js 1`] = `"webpackJsonp([0],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`; -exports[`when applied with uglifyOptions options matches snapshot for \`keep_fnames\` option (boolean \`false\` value): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+o.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`keep_fnames\` option (boolean \`true\` value): main.0c220ec66316af2c1b24.js 1`] = `"webpackJsonp([0],[function(o,n){o.exports=function Foo(){console.log(7)}}],[0]);"`; -exports[`when applied with uglifyOptions options matches snapshot for \`keep_fnames\` option (boolean \`true\` value): manifest.js 1`] = `"!function(e){var r=window.webpackJsonp;window.webpackJsonp=function webpackJsonpCallback(_,n,o){for(var u,c,p,a=0,i=[];a<_.length;a++)c=_[a],t[c]&&i.push(t[c][0]),t[c]=0;for(u in n)Object.prototype.hasOwnProperty.call(n,u)&&(e[u]=n[u]);for(r&&r(_,n,o);i.length;)i.shift()();if(o)for(a=0;a({a:b+__WEBPACK_IMPORTED_MODULE_0__dep__.a+baz,b:b,baz:baz})}},function(module,__webpack_exports__,__webpack_require__){\\"use strict\\";__webpack_exports__.a=\\"bar\\",__webpack_exports__.b=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`mangle\` option (boolean \`false\` value): main.0c220ec66316af2c1b24.js 1`] = `"webpackJsonp([0],[function(module,exports){module.exports=function(){console.log(7)}}],[0]);"`; -exports[`when applied with uglifyOptions options matches snapshot for \`mangle\` option (boolean \`false\` value): manifest.js 1`] = `"!function(modules){var parentJsonpFunction=window.webpackJsonp;window.webpackJsonp=function(chunkIds,moreModules,executeModules){for(var moduleId,chunkId,result,i=0,resolves=[];i({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`mangle\` option (boolean \`true\` value): main.0c220ec66316af2c1b24.js 1`] = `"webpackJsonp([0],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`; -exports[`when applied with uglifyOptions options matches snapshot for \`mangle\` option (boolean \`true\` value): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+baz,b:t,baz:baz})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`mangle\` option (object value): main.0c220ec66316af2c1b24.js 1`] = `"webpackJsonp([0],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`; -exports[`when applied with uglifyOptions options matches snapshot for \`mangle\` option (object value): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`nameCache\` option (empty object value): main.0c220ec66316af2c1b24.js 1`] = `"webpackJsonp([0],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`; -exports[`when applied with uglifyOptions options matches snapshot for \`nameCache\` option (empty object value): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a ({ - a: t + n.a + a, - b: t, - baz: a - }); +exports[`when applied with uglifyOptions options matches snapshot for \`output\` option (object value with \`true\` value for \`beautify\`): main.0c220ec66316af2c1b24.js 1`] = ` +"webpackJsonp([ 0 ], [ function(o, n) { + o.exports = function() { + console.log(7); }; -}, function(t, a, e) { - \\"use strict\\"; - a.a = \\"bar\\", a.b = \\"foo\\"; } ], [ 0 ]);" `; -exports[`when applied with uglifyOptions options matches snapshot for \`output\` option (object value): manifest.js 1`] = ` +exports[`when applied with uglifyOptions options matches snapshot for \`output\` option (object value with \`true\` value for \`beautify\`): manifest.d6857f782c13a99b5917.js 1`] = ` "!function(r) { var n = window.webpackJsonp; window.webpackJsonp = function(e, u, c) { @@ -454,65 +445,174 @@ exports[`when applied with uglifyOptions options matches snapshot for \`output\` }([]);" `; -exports[`when applied with uglifyOptions options matches snapshot for \`output\` option (object value): warnings 1`] = `Array []`; +exports[`when applied with uglifyOptions options matches snapshot for \`output\` option (object value with \`true\` value for \`beautify\`): warnings 1`] = `Array []`; + +exports[`when applied with uglifyOptions options matches snapshot for \`output\` option (object value with \`true\` value for \`comments\`): errors 1`] = `Array []`; + +exports[`when applied with uglifyOptions options matches snapshot for \`output\` option (object value with \`true\` value for \`comments\`): main.0c220ec66316af2c1b24.js 1`] = ` +"webpackJsonp([0],[ +/* 0 */ +/***/function(o,n){o.exports=function(){console.log(7)}}],[0]);" +`; + +exports[`when applied with uglifyOptions options matches snapshot for \`output\` option (object value with \`true\` value for \`comments\`): manifest.d6857f782c13a99b5917.js 1`] = ` +"/******/!function(r){// webpackBootstrap +/******/ // install a JSONP callback for chunk loading +/******/var n=window.webpackJsonp; +/******/window.webpackJsonp=function(e,u,c){ +/******/for( +/******/ // add \\"moreModules\\" to the modules object, +/******/ // then flag all \\"chunkIds\\" as loaded and fire callback +/******/var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`parse\` option (object value): main.0c220ec66316af2c1b24.js 1`] = `"webpackJsonp([0],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`; -exports[`when applied with uglifyOptions options matches snapshot for \`parse\` option (object value): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`safari10\` option (boolean \`false\` value): main.0c220ec66316af2c1b24.js 1`] = `"webpackJsonp([0],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`; -exports[`when applied with uglifyOptions options matches snapshot for \`safari10\` option (boolean \`false\` value): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`safari10\` option (boolean \`true\` value): main.0c220ec66316af2c1b24.js 1`] = `"webpackJsonp([0],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`; -exports[`when applied with uglifyOptions options matches snapshot for \`safari10\` option (boolean \`true\` value): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`toplevel\` option (boolean \`false\` value): main.0c220ec66316af2c1b24.js 1`] = `"webpackJsonp([0],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`; -exports[`when applied with uglifyOptions options matches snapshot for \`toplevel\` option (boolean \`false\` value): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[0]);"`; +exports[`when applied with uglifyOptions options matches snapshot for \`toplevel\` option (boolean \`true\` value): main.0c220ec66316af2c1b24.js 1`] = `"webpackJsonp([0],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`; -exports[`when applied with uglifyOptions options matches snapshot for \`toplevel\` option (boolean \`true\` value): manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a { - let eventBindings; - let eventBinding; - - beforeEach(() => { - const pluginEnvironment = new PluginEnvironment(); - const compilerEnv = pluginEnvironment.getEnvironmentStub(); - compilerEnv.context = ''; - - const plugin = new UglifyJsPlugin({ - sourceMap: true, - extractComments: { - condition: 'should be extracted', - filename(file) { - return file.replace(/(\.\w+)$/, '.license$1'); - }, - banner(licenseFile) { - return `License information can be found in ${licenseFile}`; - }, - }, - uglifyOptions: { - warnings: true, - mangle: false, - output: { - beautify: true, - }, - }, - }); - plugin.apply(compilerEnv); - eventBindings = pluginEnvironment.getEventBindings(); - }); - - it('matches snapshot', () => { - const compiler = createCompiler(); - new UglifyJsPlugin({ - sourceMap: true, - uglifyOptions: { - mangle: false, - output: { - beautify: true, - }, - warnings: true, - }, - extractComments: { - condition: 'should be extracted', - filename(file) { - return file.replace(/(\.\w+)$/, '.license$1'); - }, - banner(licenseFile) { - return `License information can be found in ${licenseFile}`; - }, - }, - }).apply(compiler); - - return compile(compiler).then((stats) => { - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); - - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); - - for (const file in stats.compilation.assets) { - if (Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)) { - expect(stats.compilation.assets[file].source()).toMatchSnapshot(file); - } - } - }); - }); - - it('binds one event handler', () => { - expect(eventBindings.length).toBe(1); - }); - - describe('compilation handler', () => { - beforeEach(() => { - [eventBinding] = eventBindings; - }); - - it('binds to compilation event', () => { - expect(eventBinding.name).toBe('compilation'); - }); - - describe('when called', () => { - let chunkPluginEnvironment; - let compilationEventBindings; - let compilationEventBinding; - let compilation; - - beforeEach(() => { - chunkPluginEnvironment = new PluginEnvironment(); - compilation = chunkPluginEnvironment.getEnvironmentStub(); - compilation.assets = { - sourceAndMap: () => { - return { - source: '/** @preserve Foo Bar */ function foo(longVariableName) { longVariableName = 1; }', - map: { - version: 3, - sources: ['test.js'], - names: ['foo', 'longVariableName'], - mappings: 'AAAA,QAASA,KAAIC,kBACTA,iBAAmB', - }, - }; - }, - 'test1.js': { - sourceAndMap: () => { - return { - source: 'invalid javascript', - map: { - version: 3, - sources: ['test1.js'], - names: [''], - mappings: 'AAAA', - }, - }; - }, - }, - 'test2.js': { - sourceAndMap: () => { - return { - source: 'function foo(x) { if (x) { return bar(); not_called1(); } }', - map: { - version: 3, - sources: ['test1.js'], - names: ['foo', 'x', 'bar', 'not_called1'], - mappings: 'AAAA,QAASA,KAAIC,GACT,GAAIA,EAAG,CACH,MAAOC,MACPC', - }, - }; - }, - }, - 'test3.js': { - sourceAndMap: () => { - return { - source: '/** @preserve Foo Bar */ function foo(longVariableName) { longVariableName = 1; }', - map: { - version: 3, - sources: ['test.js'], - names: ['foo', 'longVariableName'], - mappings: 'AAAA,QAASA,KAAIC,kBACTA,iBAAmB', - }, - }; - }, - }, - 'test4.js': { - sourceAndMap: () => { - return { - source: '/*! this comment should be extracted */ function foo(longVariableName) { /* this will not be extracted */ longVariableName = 1; } // another comment that should be extracted to a separate file\n function foo2(bar) { return bar; }', - map: { - version: 3, - sources: ['test.js'], - names: ['foo', 'longVariableName'], - mappings: 'AAAA,QAASA,KAAIC,kBACTA,iBAAmB', - }, - }; - }, - }, - }; - compilation.errors = []; - compilation.warnings = []; - - eventBinding.handler(compilation); - compilationEventBindings = chunkPluginEnvironment.getEventBindings(); - }); - - it('binds two event handler', () => { - expect(compilationEventBindings.length).toBe(2); - }); - - describe('build-module handler', () => { - beforeEach(() => { - [compilationEventBinding] = compilationEventBindings; - }); - - it('binds to build-module event', () => { - expect(compilationEventBinding.name).toBe('build-module'); - }); - - it('sets the useSourceMap flag', () => { - const obj = {}; - compilationEventBinding.handler(obj); - expect(obj.useSourceMap).toBeTruthy(); - }); - }); - - describe('optimize-chunk-assets handler', () => { - beforeEach(() => { - [compilationEventBinding] = compilationEventBindings; - }); - - it('binds to optimize-chunk-assets event', () => { - expect(compilationEventBindings[1].name).toBe('optimize-chunk-assets'); - }); - - it('outputs no errors for valid javascript', () => { - compilationEventBinding.handler([{ - files: ['test.js'], - }], () => { - expect(compilation.errors.length).toBe(0); - }); - }); - - it('does not preserve comments', () => { - compilationEventBinding.handler([{ - files: ['test.js'], - }], () => { - // eslint-disable-next-line no-underscore-dangle - expect(compilation.assets['test.js']._value).not.toBe(expect.stringContaining('/**')); - }); - }); - - it('outputs warnings for unreachable code', () => { - compilationEventBinding.handler([{ - files: ['test2.js'], - }], () => { - expect(compilation.warnings.length).toBe(1); - expect(compilation.warnings[0]).toBeInstanceOf(Error); - expect(compilation.warnings[0].message).toEqual(expect.stringContaining('Dropping unreachable code')); - }); - }); - }); - }); - }); -}); diff --git a/test/uglifyOptions-option.test.js b/test/uglifyOptions-option.test.js index 3519fb0e..15320827 100644 --- a/test/uglifyOptions-option.test.js +++ b/test/uglifyOptions-option.test.js @@ -9,11 +9,6 @@ describe('when applied with uglifyOptions options', () => { it('matches snapshot for `ecma` option (value is `5`)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/ecma-5/entry.js`, - output: { - path: `${__dirname}/dist-ecma-5`, - filename: '[name].js', - chunkFilename: '[id].[name].js', - }, }); new UglifyJsPlugin({ @@ -45,11 +40,6 @@ describe('when applied with uglifyOptions options', () => { it('matches snapshot for `ecma` option (value is `6`)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/ecma-6/entry.js`, - output: { - path: `${__dirname}/dist-ecma-6`, - filename: '[name].js', - chunkFilename: '[id].[name].js', - }, }); new UglifyJsPlugin({ @@ -81,11 +71,6 @@ describe('when applied with uglifyOptions options', () => { it('matches snapshot for `ecma` option (value is `7`)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/ecma-7/entry.js`, - output: { - path: `${__dirname}/dist-ecma-7`, - filename: '[name].js', - chunkFilename: '[id].[name].js', - }, }); new UglifyJsPlugin({ uglifyOptions: { @@ -116,11 +101,6 @@ describe('when applied with uglifyOptions options', () => { it('matches snapshot for `ecma` option (value is `8`)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/ecma-8/entry.js`, - output: { - path: `${__dirname}/dist-ecma-8`, - filename: '[name].js', - chunkFilename: '[id].[name].js', - }, }); new UglifyJsPlugin({ @@ -152,11 +132,6 @@ describe('when applied with uglifyOptions options', () => { it('matches snapshot for `warnings` option (boolean `true` value)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/unreachable-code.js`, - output: { - path: `${__dirname}/dist`, - filename: '[name].js', - chunkFilename: '[id].[name].js', - }, }); new UglifyJsPlugin({ @@ -183,11 +158,6 @@ describe('when applied with uglifyOptions options', () => { it('matches snapshot for `warnings` option (boolean `false` value)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/unreachable-code.js`, - output: { - path: `${__dirname}/dist`, - filename: '[name].js', - chunkFilename: '[id].[name].js', - }, }); new UglifyJsPlugin({ @@ -212,14 +182,7 @@ describe('when applied with uglifyOptions options', () => { }); it('matches snapshot for `parse` option (object value)', () => { - const compiler = createCompiler({ - entry: `${__dirname}/fixtures/import-export/entry.js`, - output: { - path: `${__dirname}/dist-import-export`, - filename: '[name].js', - chunkFilename: '[id].[name].js', - }, - }); + const compiler = createCompiler(); new UglifyJsPlugin({ uglifyOptions: { @@ -245,14 +208,7 @@ describe('when applied with uglifyOptions options', () => { }); it('matches snapshot for `compress` option (boolean `true` value)', () => { - const compiler = createCompiler({ - entry: `${__dirname}/fixtures/import-export/entry.js`, - output: { - path: `${__dirname}/dist-import-export`, - filename: '[name].js', - chunkFilename: '[id].[name].js', - }, - }); + const compiler = createCompiler(); new UglifyJsPlugin({ uglifyOptions: { @@ -276,14 +232,7 @@ describe('when applied with uglifyOptions options', () => { }); it('matches snapshot for `compress` option (boolean `false` value)', () => { - const compiler = createCompiler({ - entry: `${__dirname}/fixtures/import-export/entry.js`, - output: { - path: `${__dirname}/dist-import-export`, - filename: '[name].js', - chunkFilename: '[id].[name].js', - }, - }); + const compiler = createCompiler(); new UglifyJsPlugin({ uglifyOptions: { @@ -307,14 +256,7 @@ describe('when applied with uglifyOptions options', () => { }); it('matches snapshot for `compress` option (object value)', () => { - const compiler = createCompiler({ - entry: `${__dirname}/fixtures/import-export/entry.js`, - output: { - path: `${__dirname}/dist-import-export`, - filename: '[name].js', - chunkFilename: '[id].[name].js', - }, - }); + const compiler = createCompiler(); new UglifyJsPlugin({ uglifyOptions: { @@ -340,14 +282,7 @@ describe('when applied with uglifyOptions options', () => { }); it('matches snapshot for `mangle` option (boolean `true` value)', () => { - const compiler = createCompiler({ - entry: `${__dirname}/fixtures/import-export/entry.js`, - output: { - path: `${__dirname}/dist-import-export`, - filename: '[name].js', - chunkFilename: '[id].[name].js', - }, - }); + const compiler = createCompiler(); new UglifyJsPlugin({ uglifyOptions: { @@ -371,14 +306,7 @@ describe('when applied with uglifyOptions options', () => { }); it('matches snapshot for `mangle` option (boolean `false` value)', () => { - const compiler = createCompiler({ - entry: `${__dirname}/fixtures/import-export/entry.js`, - output: { - path: `${__dirname}/dist-import-export`, - filename: '[name].js', - chunkFilename: '[id].[name].js', - }, - }); + const compiler = createCompiler(); new UglifyJsPlugin({ uglifyOptions: { @@ -402,14 +330,7 @@ describe('when applied with uglifyOptions options', () => { }); it('matches snapshot for `mangle` option (object value)', () => { - const compiler = createCompiler({ - entry: `${__dirname}/fixtures/import-export/entry.js`, - output: { - path: `${__dirname}/dist-import-export`, - filename: '[name].js', - chunkFilename: '[id].[name].js', - }, - }); + const compiler = createCompiler(); new UglifyJsPlugin({ uglifyOptions: { @@ -434,15 +355,8 @@ describe('when applied with uglifyOptions options', () => { }); }); - it('matches snapshot for `output` option (object value)', () => { - const compiler = createCompiler({ - entry: `${__dirname}/fixtures/import-export/entry.js`, - output: { - path: `${__dirname}/dist-import-export`, - filename: '[name].js', - chunkFilename: '[id].[name].js', - }, - }); + it('matches snapshot for `output` option (object value with `true` value for `beautify`)', () => { + const compiler = createCompiler(); new UglifyJsPlugin({ uglifyOptions: { @@ -467,15 +381,34 @@ describe('when applied with uglifyOptions options', () => { }); }); - it('matches snapshot for `toplevel` option (boolean `true` value)', () => { - const compiler = createCompiler({ - entry: `${__dirname}/fixtures/import-export/entry.js`, - output: { - path: `${__dirname}/dist-import-export`, - filename: '[name].js', - chunkFilename: '[id].[name].js', + it('matches snapshot for `output` option (object value with `true` value for `comments`)', () => { + const compiler = createCompiler(); + + new UglifyJsPlugin({ + uglifyOptions: { + output: { + comments: true, + }, }, + }).apply(compiler); + + return compile(compiler).then((stats) => { + const errors = stats.compilation.errors.map(cleanErrorStack); + const warnings = stats.compilation.warnings.map(cleanErrorStack); + + expect(errors).toMatchSnapshot('errors'); + expect(warnings).toMatchSnapshot('warnings'); + + for (const file in stats.compilation.assets) { + if (Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)) { + expect(stats.compilation.assets[file].source()).toMatchSnapshot(file); + } + } }); + }); + + it('matches snapshot for `toplevel` option (boolean `true` value)', () => { + const compiler = createCompiler(); new UglifyJsPlugin({ uglifyOptions: { @@ -499,14 +432,7 @@ describe('when applied with uglifyOptions options', () => { }); it('matches snapshot for `toplevel` option (boolean `false` value)', () => { - const compiler = createCompiler({ - entry: `${__dirname}/fixtures/import-export/entry.js`, - output: { - path: `${__dirname}/dist-import-export`, - filename: '[name].js', - chunkFilename: '[id].[name].js', - }, - }); + const compiler = createCompiler(); new UglifyJsPlugin({ uglifyOptions: { @@ -530,14 +456,7 @@ describe('when applied with uglifyOptions options', () => { }); it('matches snapshot for `nameCache` option (empty object value)', () => { - const compiler = createCompiler({ - entry: `${__dirname}/fixtures/import-export/entry.js`, - output: { - path: `${__dirname}/dist-import-export`, - filename: '[name].js', - chunkFilename: '[id].[name].js', - }, - }); + const compiler = createCompiler(); new UglifyJsPlugin({ uglifyOptions: { @@ -561,14 +480,7 @@ describe('when applied with uglifyOptions options', () => { }); it('matches snapshot for `ie8` option (boolean `true` value)', () => { - const compiler = createCompiler({ - entry: `${__dirname}/fixtures/import-export/entry.js`, - output: { - path: `${__dirname}/dist-import-export`, - filename: '[name].js', - chunkFilename: '[id].[name].js', - }, - }); + const compiler = createCompiler(); new UglifyJsPlugin({ uglifyOptions: { @@ -592,14 +504,7 @@ describe('when applied with uglifyOptions options', () => { }); it('matches snapshot for `ie8` option (boolean `false` value)', () => { - const compiler = createCompiler({ - entry: `${__dirname}/fixtures/import-export/entry.js`, - output: { - path: `${__dirname}/dist-import-export`, - filename: '[name].js', - chunkFilename: '[id].[name].js', - }, - }); + const compiler = createCompiler(); new UglifyJsPlugin({ uglifyOptions: { @@ -623,14 +528,7 @@ describe('when applied with uglifyOptions options', () => { }); it('matches snapshot for `keep_classnames` option (boolean `true` value)', () => { - const compiler = createCompiler({ - entry: `${__dirname}/fixtures/import-export/entry.js`, - output: { - path: `${__dirname}/dist-import-export`, - filename: '[name].js', - chunkFilename: '[id].[name].js', - }, - }); + const compiler = createCompiler(); new UglifyJsPlugin({ uglifyOptions: { @@ -654,14 +552,7 @@ describe('when applied with uglifyOptions options', () => { }); it('matches snapshot for `keep_classnames` option (boolean `false` value)', () => { - const compiler = createCompiler({ - entry: `${__dirname}/fixtures/import-export/entry.js`, - output: { - path: `${__dirname}/dist-import-export`, - filename: '[name].js', - chunkFilename: '[id].[name].js', - }, - }); + const compiler = createCompiler(); new UglifyJsPlugin({ uglifyOptions: { @@ -685,14 +576,7 @@ describe('when applied with uglifyOptions options', () => { }); it('matches snapshot for `keep_fnames` option (boolean `true` value)', () => { - const compiler = createCompiler({ - entry: `${__dirname}/fixtures/import-export/entry.js`, - output: { - path: `${__dirname}/dist-import-export`, - filename: '[name].js', - chunkFilename: '[id].[name].js', - }, - }); + const compiler = createCompiler(); new UglifyJsPlugin({ uglifyOptions: { @@ -716,14 +600,7 @@ describe('when applied with uglifyOptions options', () => { }); it('matches snapshot for `keep_fnames` option (boolean `false` value)', () => { - const compiler = createCompiler({ - entry: `${__dirname}/fixtures/import-export/entry.js`, - output: { - path: `${__dirname}/dist-import-export`, - filename: '[name].js', - chunkFilename: '[id].[name].js', - }, - }); + const compiler = createCompiler(); new UglifyJsPlugin({ uglifyOptions: { @@ -747,14 +624,7 @@ describe('when applied with uglifyOptions options', () => { }); it('matches snapshot for `safari10` option (boolean `true` value)', () => { - const compiler = createCompiler({ - entry: `${__dirname}/fixtures/import-export/entry.js`, - output: { - path: `${__dirname}/dist-import-export`, - filename: '[name].js', - chunkFilename: '[id].[name].js', - }, - }); + const compiler = createCompiler(); new UglifyJsPlugin({ uglifyOptions: { @@ -778,14 +648,7 @@ describe('when applied with uglifyOptions options', () => { }); it('matches snapshot for `safari10` option (boolean `false` value)', () => { - const compiler = createCompiler({ - entry: `${__dirname}/fixtures/import-export/entry.js`, - output: { - path: `${__dirname}/dist-import-export`, - filename: '[name].js', - chunkFilename: '[id].[name].js', - }, - }); + const compiler = createCompiler(); new UglifyJsPlugin({ uglifyOptions: { @@ -811,11 +674,6 @@ describe('when applied with uglifyOptions options', () => { it('disable inline optimization by default (have a lot of problems)', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/inline-optimization.js`, - output: { - path: `${__dirname}/dist-inline-optimization`, - filename: '[name].js', - chunkFilename: '[id].[name].js', - }, }); new UglifyJsPlugin().apply(compiler);