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

Commit

Permalink
refactor: tests (#335)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
evilebottnawi committed Jul 30, 2018
1 parent 817f67e commit 77d957a
Show file tree
Hide file tree
Showing 28 changed files with 1,377 additions and 1,864 deletions.
187 changes: 183 additions & 4 deletions 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.warnings = [];
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('empty asset', () => {
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/ });
Expand Down Expand Up @@ -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: {
Expand Down
40 changes: 24 additions & 16 deletions 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",
Expand All @@ -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) {
Expand All @@ -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\\"];
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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<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[`UglifyJsPlugin when applied with no options matches snapshot: warnings 1`] = `Array []`;

0 comments on commit 77d957a

Please sign in to comment.