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

test: add test for MultiCompiler usage #74

Merged
merged 7 commits into from
Jul 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"dependencies": {
"source-map": "^0.5.6",
"uglify-es": "^3.0.21",
"uglify-es": "^3.0.24",
"webpack-sources": "^1.0.1"
},
"devDependencies": {
Expand Down
6 changes: 3 additions & 3 deletions test/__snapshots__/es2015.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ exports[`es6: main.96c5ba3d7f23066ac123.js 1`] = `
`;

exports[`es6: manifest.a458e5044f91a97906ab.js 1`] = `
"!(modules => {
"(modules => {
function __webpack_require__(moduleId) {
if (installedModules[moduleId]) return installedModules[moduleId].exports;
var module = installedModules[moduleId] = {
Expand Down Expand Up @@ -217,7 +217,7 @@ exports[`es7: main.96c5ba3d7f23066ac123.js 1`] = `
`;

exports[`es7: manifest.a458e5044f91a97906ab.js 1`] = `
"!(modules => {
"(modules => {
function __webpack_require__(moduleId) {
if (installedModules[moduleId]) return installedModules[moduleId].exports;
var module = installedModules[moduleId] = {
Expand Down Expand Up @@ -313,7 +313,7 @@ exports[`es8: main.96c5ba3d7f23066ac123.js 1`] = `
`;

exports[`es8: manifest.a458e5044f91a97906ab.js 1`] = `
"!(modules => {
"(modules => {
function __webpack_require__(moduleId) {
if (installedModules[moduleId]) return installedModules[moduleId].exports;
var module = installedModules[moduleId] = {
Expand Down
116 changes: 116 additions & 0 deletions test/__snapshots__/supports-multicompiler.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`compiler plugin count 1`] = `
Object {
"after-emit": 1,
"after-resolvers": 2,
"before-run": 1,
"compilation": 32,
"done": 1,
"entry-option": 1,
"invalid": 1,
"make": 1,
"this-compilation": 1,
}
`;

exports[`errors 1`] = `Array []`;

exports[`errors 2`] = `Array []`;

exports[`errors 3`] = `Array []`;

exports[`main.4e5a10269ba18219d140.js 1`] = `
"/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = \\"\\";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports) {

// foo
/* @preserve*/
// bar
const a = 2 + 2;

module.exports = function Foo() {
const b = 2 + 2;
console.log(b + 1 + 2);
};


/***/ })
/******/ ]);"
`;

exports[`main.4e5a10269ba18219d140.js 2`] = `"!function(n){function t(r){if(e[r])return e[r].exports;var o=e[r]={i:r,l:!1,exports:{}};return n[r].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var e={};t.m=n,t.c=e,t.d=function(n,e,r){t.o(n,e)||Object.defineProperty(n,e,{configurable:!1,enumerable:!0,get:r})},t.n=function(n){var e=n&&n.__esModule?function(){return n.default}:function(){return n};return t.d(e,\\"a\\",e),e},t.o=function(n,t){return Object.prototype.hasOwnProperty.call(n,t)},t.p=\\"\\",t(t.s=0)}([function(n,t){n.exports=function(){console.log(7)}}]);"`;

exports[`main.db8335aef59d7a30f804.js 1`] = `"!function(t){function e(n){if(r[n])return r[n].exports;var o=r[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,e),o.l=!0,o.exports}var r={};e.m=t,e.c=r,e.d=function(t,r,n){e.o(t,r)||Object.defineProperty(t,r,{configurable:!1,enumerable:!0,get:n})},e.n=function(t){var r=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(r,\\"a\\",r),r},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p=\\"\\",e(e.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[`warnings 1`] = `Array []`;

exports[`warnings 2`] = `Array []`;

exports[`warnings 3`] = `Array []`;
30 changes: 19 additions & 11 deletions test/helpers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import MemoryFileSystem from 'memory-fs'; // eslint-disable-line import/no-extraneous-dependencies
import webpack from 'webpack';

exports.PluginEnvironment = class PluginEnvironment {
export class PluginEnvironment {
constructor() {
this.events = [];
}
Expand All @@ -20,19 +20,19 @@ exports.PluginEnvironment = class PluginEnvironment {
getEventBindings() {
return this.events;
}
};
}

exports.compile = function compile(compiler) {
export function compile(compiler) {
return new Promise((resolve, reject) => {
compiler.run((err, stats) => { // eslint-disable-line consistent-return
if (err) return reject(err);
resolve(stats);
});
});
};
}

exports.createCompiler = function createCompiler(options = {}) {
const compiler = webpack({
export function createCompiler(options = {}) {
const compiler = webpack(Array.isArray(options) ? options : {
bail: true,
cache: false,
entry: `${__dirname}/fixtures/entry.js`,
Expand All @@ -50,15 +50,23 @@ exports.createCompiler = function createCompiler(options = {}) {
});
compiler.outputFileSystem = new MemoryFileSystem();
return compiler;
};
}

exports.removeCWD = function removeCWD(str) {
export function countPlugins({ _plugins }) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this used?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the usecase here ? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Kovensky @michael-ciniawsky Was for the test I hadn't added at first to this pr. But, I have since added it: https://github.com/webpack-contrib/uglifyjs-webpack-plugin/pull/74/files#diff-0570d5214a7cbff246a7ad90d119f15c

return Object.keys(_plugins).reduce((aggregate, name) => {
const eventLength = Array.isArray(_plugins[name]) ? _plugins[name].length : 0;
aggregate[name] = eventLength; // eslint-disable-line no-param-reassign
return aggregate;
}, {});
}

export function removeCWD(str) {
return str.split(`${process.cwd()}/`).join('');
};
}

exports.cleanErrorStack = function cleanErrorStack(error) {
export function cleanErrorStack(error) {
const str = exports.removeCWD(error.toString())
.split('\n').slice(0, 2).join('\n');
return str;
};
}

84 changes: 84 additions & 0 deletions test/supports-multicompiler.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import MultiCompiler from 'webpack/lib/MultiCompiler';
import MultiStats from 'webpack/lib/MultiStats';
import UglifyJsPlugin from '../src/index';
import {
cleanErrorStack,
createCompiler,
countPlugins,
compile,
} from './helpers';

describe('when using MultiCompiler with empty options', () => {
it('matches snapshot', () => {
const multiCompiler = createCompiler([
{
bail: true,
cache: false,
entry: `${__dirname}/fixtures/entry.js`,
output: {
path: `${__dirname}/dist`,
filename: '[name].[chunkhash].js',
chunkFilename: '[id].[name].[chunkhash].js',
},
},
{
bail: true,
cache: false,
entry: `${__dirname}/fixtures/entry.js`,
output: {
path: `${__dirname}/dist`,
filename: '[name].[chunkhash].js',
chunkFilename: '[id].[name].[chunkhash].js',
},
plugins: [new UglifyJsPlugin()],
},
{
bail: true,
cache: false,
entry: `${__dirname}/fixtures/es2015/entry.js`,
output: {
path: `${__dirname}/dist-MultiCompiler`,
filename: '[name].[chunkhash].js',
chunkFilename: '[id].[name].[chunkhash].js',
},
plugins: [new UglifyJsPlugin()],
},
]);

const emptyPluginCount = countPlugins(multiCompiler.compilers[0]);
const expectedPluginCount = countPlugins(multiCompiler.compilers[1]);

expect(emptyPluginCount).not.toEqual(expectedPluginCount);
expect(multiCompiler).toBeInstanceOf(MultiCompiler);

multiCompiler.compilers.slice(2).forEach((compiler) => {
const pluginCount = countPlugins(compiler);
expect(pluginCount).not.toEqual(emptyPluginCount);
expect(pluginCount).toEqual(expectedPluginCount);
expect(pluginCount).toMatchSnapshot('compiler plugin count');
});

expect(multiCompiler).toBeInstanceOf(MultiCompiler);

return compile(multiCompiler).then((multiStats) => {
expect(multiStats).toBeInstanceOf(MultiStats);

multiStats.stats.forEach((stats) => {
const errors = stats.compilation.errors.map(cleanErrorStack);
const warnings = stats.compilation.warnings.map(cleanErrorStack);

expect(errors.length).toEqual(0);
expect(warnings.length).toEqual(0);

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);
}
}
});
});
});
});