Skip to content
This repository was archived by the owner on Dec 5, 2019. It is now read-only.

Commit 77d957a

Browse files
refactor: tests (#335)
* refactor: helper * refactor: move `empty-options` test to `UglifyJsPlugin` test * refactor: better name test * refactor: `uglifyOptions` options tests * refactor: use same test names * refactor: `cache` option tests * refactor: `uglifyOptions` options tests * refactor: `warningsFilter` option tests * refactor: remove `extractComments` duplicate test * refactor: move some tests from `all-options` to `UglifyHsPlugin` tests * refactor: remove duplicated tests
1 parent 817f67e commit 77d957a

28 files changed

+1377
-1864
lines changed

test/UglifyJsPlugin.test.js

Lines changed: 183 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,191 @@
1+
import { RawSource } from 'webpack-sources';
12
import UglifyJsPlugin from '../src/index';
2-
import { cleanErrorStack, compile, createCompiler } from './helpers';
3+
import { cleanErrorStack, compile, createCompiler, PluginEnvironment } from './helpers';
34

45
describe('UglifyJsPlugin', () => {
5-
it('export as function', () => {
6+
it('should exported as function', () => {
67
expect(typeof new UglifyJsPlugin().apply).toBe('function');
78
});
89

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', () => {
10189
/* eslint-disable no-new */
11190
expect(() => {
12191
new UglifyJsPlugin({ test: /foo/ });
@@ -180,7 +359,7 @@ describe('UglifyJsPlugin', () => {
180359
}).not.toThrow('Validation Error');
181360
});
182361

183-
it('contain errors when uglify has unknown option', () => {
362+
it('should contains error when uglify has unknown option', () => {
184363
const compiler = createCompiler();
185364
new UglifyJsPlugin({
186365
uglifyOptions: {

test/__snapshots__/UglifyJsPlugin.test.js.snap

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`UglifyJsPlugin contain errors when uglify has unknown option: errors 1`] = `
3+
exports[`UglifyJsPlugin should contains error when uglify has unknown option: errors 1`] = `
44
Array [
55
"Error: main.0c220ec66316af2c1b24.js from UglifyJs
66
DefaultsError: \`unknown\` is not a supported option",
@@ -9,7 +9,7 @@ DefaultsError: \`unknown\` is not a supported option",
99
]
1010
`;
1111

12-
exports[`UglifyJsPlugin contain errors when uglify has unknown option: main.0c220ec66316af2c1b24.js 1`] = `
12+
exports[`UglifyJsPlugin should contains error when uglify has unknown option: main.0c220ec66316af2c1b24.js 1`] = `
1313
"webpackJsonp([0],[
1414
/* 0 */
1515
/***/ (function(module, exports) {
@@ -29,7 +29,7 @@ module.exports = function Foo() {
2929
],[0]);"
3030
`;
3131

32-
exports[`UglifyJsPlugin contain errors when uglify has unknown option: manifest.d6857f782c13a99b5917.js 1`] = `
32+
exports[`UglifyJsPlugin should contains error when uglify has unknown option: manifest.d6857f782c13a99b5917.js 1`] = `
3333
"/******/ (function(modules) { // webpackBootstrap
3434
/******/ // install a JSONP callback for chunk loading
3535
/******/ var parentJsonpFunction = window[\\"webpackJsonp\\"];
@@ -133,16 +133,16 @@ exports[`UglifyJsPlugin contain errors when uglify has unknown option: manifest.
133133
/******/ ([]);"
134134
`;
135135

136-
exports[`UglifyJsPlugin contain errors when uglify has unknown option: warnings 1`] = `Array []`;
136+
exports[`UglifyJsPlugin should contains error when uglify has unknown option: warnings 1`] = `Array []`;
137137

138-
exports[`UglifyJsPlugin validation errors 1`] = `
138+
exports[`UglifyJsPlugin should handle validation errors 1`] = `
139139
"UglifyJs Plugin Invalid Options
140140
141141
options['doesntExist'] is an invalid additional property
142142
"
143143
`;
144144

145-
exports[`UglifyJsPlugin validation errors 2`] = `
145+
exports[`UglifyJsPlugin should handle validation errors 2`] = `
146146
"UglifyJs Plugin Invalid Options
147147
148148
options.cache should be boolean
@@ -151,7 +151,7 @@ options.cache should match exactly one schema in oneOf
151151
"
152152
`;
153153

154-
exports[`UglifyJsPlugin validation errors 3`] = `
154+
exports[`UglifyJsPlugin should handle validation errors 3`] = `
155155
"UglifyJs Plugin Invalid Options
156156
157157
options.parallel should be boolean
@@ -160,7 +160,7 @@ options.parallel should match exactly one schema in oneOf
160160
"
161161
`;
162162

163-
exports[`UglifyJsPlugin validation errors 4`] = `
163+
exports[`UglifyJsPlugin should handle validation errors 4`] = `
164164
"UglifyJs Plugin Invalid Options
165165
166166
options.parallel should be boolean
@@ -169,58 +169,66 @@ options.parallel should match exactly one schema in oneOf
169169
"
170170
`;
171171

172-
exports[`UglifyJsPlugin validation errors 5`] = `
172+
exports[`UglifyJsPlugin should handle validation errors 5`] = `
173173
"UglifyJs Plugin Invalid Options
174174
175175
options.sourceMap should be boolean
176176
"
177177
`;
178178

179-
exports[`UglifyJsPlugin validation errors 6`] = `
179+
exports[`UglifyJsPlugin should handle validation errors 6`] = `
180180
"UglifyJs Plugin Invalid Options
181181
182182
options.uglifyOptions should be object
183183
"
184184
`;
185185

186-
exports[`UglifyJsPlugin validation errors 7`] = `
186+
exports[`UglifyJsPlugin should handle validation errors 7`] = `
187187
"UglifyJs Plugin Invalid Options
188188
189189
options.uglifyOptions.ie8 should be boolean
190190
"
191191
`;
192192

193-
exports[`UglifyJsPlugin validation errors 8`] = `
193+
exports[`UglifyJsPlugin should handle validation errors 8`] = `
194194
"UglifyJs Plugin Invalid Options
195195
196196
options.uglifyOptions.ecma should be integer
197197
"
198198
`;
199199

200-
exports[`UglifyJsPlugin validation errors 9`] = `
200+
exports[`UglifyJsPlugin should handle validation errors 9`] = `
201201
"UglifyJs Plugin Invalid Options
202202
203203
options.uglifyOptions.ecma should be integer
204204
"
205205
`;
206206

207-
exports[`UglifyJsPlugin validation errors 10`] = `
207+
exports[`UglifyJsPlugin should handle validation errors 10`] = `
208208
"UglifyJs Plugin Invalid Options
209209
210210
options.uglifyOptions.ecma should be integer
211211
"
212212
`;
213213

214-
exports[`UglifyJsPlugin validation errors 11`] = `
214+
exports[`UglifyJsPlugin should handle validation errors 11`] = `
215215
"UglifyJs Plugin Invalid Options
216216
217217
options.uglifyOptions.ecma should be >= 5
218218
"
219219
`;
220220

221-
exports[`UglifyJsPlugin validation errors 12`] = `
221+
exports[`UglifyJsPlugin should handle validation errors 12`] = `
222222
"UglifyJs Plugin Invalid Options
223223
224224
options.uglifyOptions.ecma should be <= 8
225225
"
226226
`;
227+
228+
exports[`UglifyJsPlugin when applied with no options matches snapshot: errors 1`] = `Array []`;
229+
230+
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]);"`;
231+
232+
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<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}}([]);"`;
233+
234+
exports[`UglifyJsPlugin when applied with no options matches snapshot: warnings 1`] = `Array []`;

0 commit comments

Comments
 (0)