|
| 1 | +import { RawSource } from 'webpack-sources'; |
1 | 2 | import UglifyJsPlugin from '../src/index'; |
2 | | -import { cleanErrorStack, compile, createCompiler } from './helpers'; |
| 3 | +import { cleanErrorStack, compile, createCompiler, PluginEnvironment } from './helpers'; |
3 | 4 |
|
4 | 5 | describe('UglifyJsPlugin', () => { |
5 | | - it('export as function', () => { |
| 6 | + it('should exported as function', () => { |
6 | 7 | expect(typeof new UglifyJsPlugin().apply).toBe('function'); |
7 | 8 | }); |
8 | 9 |
|
9 | | - it('validation errors', () => { |
| 10 | + describe('when applied with no options', () => { |
| 11 | + let eventBindings; |
| 12 | + let eventBinding; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + const pluginEnvironment = new PluginEnvironment(); |
| 16 | + const compilerEnv = pluginEnvironment.getEnvironmentStub(); |
| 17 | + compilerEnv.context = ''; |
| 18 | + |
| 19 | + const plugin = new UglifyJsPlugin(); |
| 20 | + plugin.apply(compilerEnv); |
| 21 | + eventBindings = pluginEnvironment.getEventBindings(); |
| 22 | + }); |
| 23 | + |
| 24 | + it('binds one event handler', () => { |
| 25 | + expect(eventBindings.length).toBe(1); |
| 26 | + }); |
| 27 | + |
| 28 | + describe('compilation handler', () => { |
| 29 | + beforeEach(() => { |
| 30 | + [eventBinding] = eventBindings; |
| 31 | + }); |
| 32 | + |
| 33 | + it('binds to compilation event', () => { |
| 34 | + expect(eventBinding.name).toBe('compilation'); |
| 35 | + }); |
| 36 | + |
| 37 | + describe('when called', () => { |
| 38 | + let chunkPluginEnvironment; |
| 39 | + let compilationEventBindings; |
| 40 | + let compilationEventBinding; |
| 41 | + let compilation; |
| 42 | + let callback; |
| 43 | + |
| 44 | + beforeEach(() => { |
| 45 | + chunkPluginEnvironment = new PluginEnvironment(); |
| 46 | + compilation = chunkPluginEnvironment.getEnvironmentStub(); |
| 47 | + compilation.assets = { |
| 48 | + 'test.js': {}, |
| 49 | + 'test1.js': '', |
| 50 | + 'test2.js': { |
| 51 | + source: () => 'invalid javascript', |
| 52 | + }, |
| 53 | + 'test3.js': { |
| 54 | + source: () => '/** @preserve Foo Bar */ function foo(longVariableName) { longVariableName = 1; }', |
| 55 | + }, |
| 56 | + }; |
| 57 | + compilation.warnings = []; |
| 58 | + compilation.errors = []; |
| 59 | + |
| 60 | + eventBinding.handler(compilation); |
| 61 | + compilationEventBindings = chunkPluginEnvironment.getEventBindings(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('binds one event handler', () => { |
| 65 | + expect(compilationEventBindings.length).toBe(1); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('optimize-chunk-assets handler', () => { |
| 69 | + beforeEach(() => { |
| 70 | + [compilationEventBinding] = compilationEventBindings; |
| 71 | + }); |
| 72 | + |
| 73 | + it('binds to optimize-chunk-assets event', () => { |
| 74 | + expect(compilationEventBinding.name).toEqual('optimize-chunk-assets'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('only calls callback once', () => { |
| 78 | + callback = jest.fn(); |
| 79 | + compilationEventBinding.handler([''], () => { |
| 80 | + callback(); |
| 81 | + expect(callback.mock.calls.length).toBe(1); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + it('default only parses filenames ending with .js', () => { |
| 86 | + compilationEventBinding.handler([{ |
| 87 | + files: ['test', 'test.js'], |
| 88 | + }], () => { |
| 89 | + expect(Object.keys(compilation.assets).length).toBe(4); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + it('empty asset', () => { |
| 94 | + compilationEventBinding.handler([{ |
| 95 | + files: ['test.js'], |
| 96 | + }], () => { |
| 97 | + expect(compilation.assets['test.js']).toEqual({}); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + it('outputs stack trace errors for invalid asset', () => { |
| 102 | + compilationEventBinding.handler([{ |
| 103 | + files: ['test1.js'], |
| 104 | + }], () => { |
| 105 | + expect(compilation.errors.length).toBe(1); |
| 106 | + expect(compilation.errors[0]).toBeInstanceOf(Error); |
| 107 | + expect(compilation.errors[0].message).toEqual(expect.stringContaining('asset.source is not a function')); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + it('outputs parsing errors for invalid javascript', () => { |
| 112 | + compilationEventBinding.handler([{ |
| 113 | + files: ['test2.js'], |
| 114 | + }], () => { |
| 115 | + expect(compilation.errors.length).toBe(1); |
| 116 | + expect(compilation.errors[0]).toBeInstanceOf(Error); |
| 117 | + expect(compilation.errors[0].message).toEqual(expect.stringContaining('Unexpected token')); |
| 118 | + expect(compilation.errors[0].message).toEqual(expect.stringContaining('[test2.js:1,8]')); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + it('outputs no errors for valid javascript', () => { |
| 123 | + compilationEventBinding.handler([{ |
| 124 | + files: ['test3.js'], |
| 125 | + }], () => { |
| 126 | + expect(compilation.errors.length).toBe(0); |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + it('outputs RawSource for valid javascript', () => { |
| 131 | + compilationEventBinding.handler([{ |
| 132 | + files: ['test3.js'], |
| 133 | + }], () => { |
| 134 | + expect(compilation.assets['test3.js']).toBeInstanceOf(RawSource); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + it('outputs mangled javascript', () => { |
| 139 | + compilationEventBinding.handler([{ |
| 140 | + files: ['test3.js'], |
| 141 | + }], () => { |
| 142 | + // eslint-disable-next-line no-underscore-dangle |
| 143 | + expect(compilation.assets['test3.js']._value).not.toEqual(expect.stringContaining('longVariableName')); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + it('compresses and does not output beautified javascript', () => { |
| 148 | + compilationEventBinding.handler([{ |
| 149 | + files: ['test3.js'], |
| 150 | + }], () => { |
| 151 | + // eslint-disable-next-line no-underscore-dangle |
| 152 | + expect(compilation.assets['test3.js']._value).not.toEqual(expect.stringContaining('\n')); |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + it('preserves comments', () => { |
| 157 | + compilationEventBinding.handler([{ |
| 158 | + files: ['test3.js'], |
| 159 | + }], () => { |
| 160 | + // eslint-disable-next-line no-underscore-dangle |
| 161 | + expect(compilation.assets['test3.js']._value).toEqual(expect.stringContaining('/**')); |
| 162 | + }); |
| 163 | + }); |
| 164 | + }); |
| 165 | + }); |
| 166 | + }); |
| 167 | + |
| 168 | + it('matches snapshot', () => { |
| 169 | + const compiler = createCompiler(); |
| 170 | + new UglifyJsPlugin().apply(compiler); |
| 171 | + |
| 172 | + return compile(compiler).then((stats) => { |
| 173 | + const errors = stats.compilation.errors.map(cleanErrorStack); |
| 174 | + const warnings = stats.compilation.warnings.map(cleanErrorStack); |
| 175 | + |
| 176 | + expect(errors).toMatchSnapshot('errors'); |
| 177 | + expect(warnings).toMatchSnapshot('warnings'); |
| 178 | + |
| 179 | + for (const file in stats.compilation.assets) { |
| 180 | + if (Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)) { |
| 181 | + expect(stats.compilation.assets[file].source()).toMatchSnapshot(file); |
| 182 | + } |
| 183 | + } |
| 184 | + }); |
| 185 | + }); |
| 186 | + }); |
| 187 | + |
| 188 | + it('should handle validation errors', () => { |
10 | 189 | /* eslint-disable no-new */ |
11 | 190 | expect(() => { |
12 | 191 | new UglifyJsPlugin({ test: /foo/ }); |
@@ -180,7 +359,7 @@ describe('UglifyJsPlugin', () => { |
180 | 359 | }).not.toThrow('Validation Error'); |
181 | 360 | }); |
182 | 361 |
|
183 | | - it('contain errors when uglify has unknown option', () => { |
| 362 | + it('should contains error when uglify has unknown option', () => { |
184 | 363 | const compiler = createCompiler(); |
185 | 364 | new UglifyJsPlugin({ |
186 | 365 | uglifyOptions: { |
|
0 commit comments