Skip to content

Commit d24da75

Browse files
committed
add two test cases
1 parent e107cc5 commit d24da75

1 file changed

Lines changed: 116 additions & 1 deletion

File tree

test/UglifyJsPlugin.test.js

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
/* globals describe, it, beforeEach*/
12
"use strict";
2-
const should = require("should");
3+
require("should");
34
const sinon = require("sinon");
45
const UglifyJsPlugin = require("../lib/optimize/UglifyJsPlugin");
56
const PluginEnvironment = require("./helpers/PluginEnvironment");
@@ -409,6 +410,120 @@ describe("UglifyJsPlugin", function() {
409410
compilation.assets["test3.js"].should.be.instanceof(SourceMapSource);
410411
});
411412
});
413+
414+
describe("with warningsFilter set", function() {
415+
let compilationEventBindings, compilation;
416+
417+
describe("and the filter returns true", function() {
418+
beforeEach(function() {
419+
const pluginEnvironment = new PluginEnvironment();
420+
const compilerEnv = pluginEnvironment.getEnvironmentStub();
421+
compilerEnv.context = "";
422+
423+
const plugin = new UglifyJsPlugin({
424+
warningsFilter: function() {
425+
return true;
426+
},
427+
sourceMap: true,
428+
compress: {
429+
warnings: true,
430+
},
431+
mangle: false,
432+
beautify: true,
433+
comments: false
434+
});
435+
plugin.apply(compilerEnv);
436+
const eventBindings = pluginEnvironment.getEventBindings();
437+
438+
const chunkPluginEnvironment = new PluginEnvironment();
439+
compilation = chunkPluginEnvironment.getEnvironmentStub();
440+
compilation.assets = {
441+
"test2.js": {
442+
source: function() {
443+
return "function foo(x) { if (x) { return bar(); not_called1(); } }";
444+
},
445+
map: function() {
446+
return {
447+
version: 3,
448+
sources: ["test1.js"],
449+
names: ["foo", "x", "bar", "not_called1"],
450+
mappings: "AAAA,QAASA,KAAIC,GACT,GAAIA,EAAG,CACH,MAAOC,MACPC"
451+
};
452+
}
453+
},
454+
};
455+
compilation.errors = [];
456+
compilation.warnings = [];
457+
458+
eventBindings[0].handler(compilation);
459+
compilationEventBindings = chunkPluginEnvironment.getEventBindings();
460+
});
461+
462+
it("should get all warnings", function() {
463+
compilationEventBindings[1].handler([{
464+
files: ["test2.js"]
465+
}], function() {
466+
compilation.warnings.length.should.be.exactly(1);
467+
compilation.warnings[0].should.be.an.Error;
468+
compilation.warnings[0].message.should.containEql("Dropping unreachable code");
469+
});
470+
});
471+
});
472+
473+
describe("and the filter returns false", function() {
474+
beforeEach(function() {
475+
const pluginEnvironment = new PluginEnvironment();
476+
const compilerEnv = pluginEnvironment.getEnvironmentStub();
477+
compilerEnv.context = "";
478+
479+
const plugin = new UglifyJsPlugin({
480+
warningsFilter: function() {
481+
return false;
482+
},
483+
sourceMap: true,
484+
compress: {
485+
warnings: true,
486+
},
487+
mangle: false,
488+
beautify: true,
489+
comments: false
490+
});
491+
plugin.apply(compilerEnv);
492+
const eventBindings = pluginEnvironment.getEventBindings();
493+
494+
const chunkPluginEnvironment = new PluginEnvironment();
495+
compilation = chunkPluginEnvironment.getEnvironmentStub();
496+
compilation.assets = {
497+
"test2.js": {
498+
source: function() {
499+
return "function foo(x) { if (x) { return bar(); not_called1(); } }";
500+
},
501+
map: function() {
502+
return {
503+
version: 3,
504+
sources: ["test1.js"],
505+
names: ["foo", "x", "bar", "not_called1"],
506+
mappings: "AAAA,QAASA,KAAIC,GACT,GAAIA,EAAG,CACH,MAAOC,MACPC"
507+
};
508+
}
509+
},
510+
};
511+
compilation.errors = [];
512+
compilation.warnings = [];
513+
514+
eventBindings[0].handler(compilation);
515+
compilationEventBindings = chunkPluginEnvironment.getEventBindings();
516+
});
517+
518+
it("should get no warnings", function() {
519+
compilationEventBindings[1].handler([{
520+
files: ["test2.js"]
521+
}], function() {
522+
compilation.warnings.length.should.be.exactly(0);
523+
});
524+
});
525+
});
526+
});
412527
});
413528
});
414529
});

0 commit comments

Comments
 (0)