2017-01-18 18:29:38 +08:00
|
|
|
"use strict";
|
|
|
|
|
2015-04-27 04:47:47 +08:00
|
|
|
/*globals describe it */
|
2017-01-18 18:29:38 +08:00
|
|
|
const should = require("should");
|
|
|
|
const path = require("path");
|
2013-10-11 16:42:25 +08:00
|
|
|
|
2017-01-18 18:29:38 +08:00
|
|
|
const webpack = require("../lib/webpack");
|
2013-10-11 16:42:25 +08:00
|
|
|
|
2017-01-18 18:29:38 +08:00
|
|
|
const base = path.join(__dirname, "fixtures", "errors");
|
2013-10-11 16:42:25 +08:00
|
|
|
|
2017-01-18 18:29:38 +08:00
|
|
|
describe("Errors", () => {
|
2015-04-27 04:47:47 +08:00
|
|
|
function customOutputFilesystem(c) {
|
2017-01-18 18:29:38 +08:00
|
|
|
const files = {};
|
2013-10-11 16:42:25 +08:00
|
|
|
c.outputFileSystem = {
|
|
|
|
join: path.join.bind(path),
|
|
|
|
mkdirp: function(path, callback) {
|
|
|
|
callback();
|
|
|
|
},
|
|
|
|
writeFile: function(name, content, callback) {
|
2018-02-22 01:01:53 +08:00
|
|
|
files[name] = content.toString("utf8");
|
2013-10-11 16:42:25 +08:00
|
|
|
callback();
|
|
|
|
}
|
|
|
|
};
|
2015-04-27 04:47:47 +08:00
|
|
|
return files;
|
|
|
|
}
|
2015-08-09 18:42:43 +08:00
|
|
|
|
2015-04-27 04:47:47 +08:00
|
|
|
function getErrors(options, callback) {
|
|
|
|
options.context = base;
|
2017-01-18 18:29:38 +08:00
|
|
|
const c = webpack(options);
|
2015-04-27 04:47:47 +08:00
|
|
|
customOutputFilesystem(c);
|
2017-01-18 18:29:38 +08:00
|
|
|
c.run((err, stats) => {
|
2013-10-11 16:42:25 +08:00
|
|
|
if(err) throw err;
|
2013-10-16 14:57:37 +08:00
|
|
|
should.strictEqual(typeof stats, "object");
|
2015-08-09 18:42:43 +08:00
|
|
|
stats = stats.toJson({
|
|
|
|
errorDetails: false
|
|
|
|
});
|
2013-10-16 14:57:37 +08:00
|
|
|
should.strictEqual(typeof stats, "object");
|
2013-10-11 16:42:25 +08:00
|
|
|
stats.should.have.property("errors");
|
|
|
|
stats.should.have.property("warnings");
|
2017-01-02 08:44:24 +08:00
|
|
|
Array.isArray(stats.errors).should.be.ok(); // eslint-disable-line no-unused-expressions
|
|
|
|
Array.isArray(stats.warnings).should.be.ok(); // eslint-disable-line no-unused-expressions
|
2013-10-11 16:42:25 +08:00
|
|
|
callback(stats.errors, stats.warnings);
|
|
|
|
});
|
|
|
|
}
|
2018-02-22 01:12:32 +08:00
|
|
|
|
2018-02-22 01:01:53 +08:00
|
|
|
function getErrorsPromise(options, callback) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
getErrors(options, (errors, warnings) => {
|
|
|
|
callback(errors, warnings);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
2018-02-22 01:06:02 +08:00
|
|
|
}
|
2017-01-18 18:29:38 +08:00
|
|
|
it("should throw an error if file doesn't exist", (done) => {
|
2013-10-11 16:42:25 +08:00
|
|
|
getErrors({
|
2017-11-21 17:41:01 +08:00
|
|
|
mode: "development",
|
2013-10-11 16:42:25 +08:00
|
|
|
entry: "./missingFile"
|
2017-01-18 18:29:38 +08:00
|
|
|
}, (errors, warnings) => {
|
2013-10-11 16:42:25 +08:00
|
|
|
errors.length.should.be.eql(2);
|
|
|
|
warnings.length.should.be.eql(0);
|
2014-05-26 17:36:59 +08:00
|
|
|
errors.sort();
|
2017-01-18 18:29:38 +08:00
|
|
|
let lines = errors[0].split("\n");
|
2013-10-11 16:42:25 +08:00
|
|
|
lines[0].should.match(/missingFile.js/);
|
|
|
|
lines[1].should.match(/^Module not found/);
|
2014-05-26 17:36:59 +08:00
|
|
|
lines[1].should.match(/\.\/dir\/missing2/);
|
|
|
|
lines[2].should.match(/missingFile.js 12:9/);
|
2015-04-27 04:47:47 +08:00
|
|
|
lines = errors[1].split("\n");
|
2013-10-11 16:42:25 +08:00
|
|
|
lines[0].should.match(/missingFile.js/);
|
|
|
|
lines[1].should.match(/^Module not found/);
|
2014-05-26 17:36:59 +08:00
|
|
|
lines[1].should.match(/\.\/missing/);
|
|
|
|
lines[2].should.match(/missingFile.js 4:0/);
|
2013-10-11 16:42:25 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2017-01-18 18:29:38 +08:00
|
|
|
it("should report require.extensions as unsupported", (done) => {
|
2013-10-11 16:42:25 +08:00
|
|
|
getErrors({
|
2017-11-21 17:41:01 +08:00
|
|
|
mode: "development",
|
2013-10-11 16:42:25 +08:00
|
|
|
entry: "./require.extensions"
|
2017-01-18 18:29:38 +08:00
|
|
|
}, (errors, warnings) => {
|
2013-10-11 16:42:25 +08:00
|
|
|
errors.length.should.be.eql(0);
|
|
|
|
warnings.length.should.be.eql(1);
|
2017-01-18 18:29:38 +08:00
|
|
|
const lines = warnings[0].split("\n");
|
2014-02-04 01:12:19 +08:00
|
|
|
lines[0].should.match(/require.extensions\.js/);
|
2013-10-11 16:42:25 +08:00
|
|
|
lines[1].should.match(/require.extensions is not supported by webpack/);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2017-01-18 18:29:38 +08:00
|
|
|
it("should warn about case-sensitive module names", (done) => {
|
2014-03-31 17:33:17 +08:00
|
|
|
getErrors({
|
2017-11-21 17:41:01 +08:00
|
|
|
mode: "development",
|
2014-03-31 17:33:17 +08:00
|
|
|
entry: "./case-sensitive"
|
2017-01-18 18:29:38 +08:00
|
|
|
}, (errors, warnings) => {
|
2014-03-31 17:39:01 +08:00
|
|
|
if(errors.length === 0) {
|
2016-05-05 21:19:54 +08:00
|
|
|
warnings.length.should.be.eql(1);
|
2017-01-18 18:29:38 +08:00
|
|
|
const lines = warnings[0].split("\n");
|
2016-06-08 05:48:04 +08:00
|
|
|
lines[4].should.match(/FILE\.js/);
|
2016-06-05 01:51:22 +08:00
|
|
|
lines[5].should.match(/Used by/);
|
|
|
|
lines[6].should.match(/case-sensitive/);
|
2016-06-08 05:48:04 +08:00
|
|
|
lines[7].should.match(/file\.js/);
|
2016-06-05 01:51:22 +08:00
|
|
|
lines[8].should.match(/Used by/);
|
|
|
|
lines[9].should.match(/case-sensitive/);
|
2014-03-31 17:39:01 +08:00
|
|
|
} else {
|
|
|
|
errors.length.should.be.eql(1);
|
|
|
|
warnings.length.should.be.eql(0);
|
|
|
|
}
|
2014-03-31 17:33:17 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2017-11-21 17:41:01 +08:00
|
|
|
it("should warn when not using mode", (done) => {
|
|
|
|
getErrors({
|
|
|
|
entry: "./entry-point",
|
|
|
|
}, (errors, warnings) => {
|
|
|
|
errors.length.should.be.eql(0);
|
|
|
|
warnings.length.should.be.eql(1);
|
|
|
|
let lines = warnings[0].split("\n");
|
|
|
|
lines[0].should.match(/configuration/);
|
|
|
|
lines[1].should.match(/mode/);
|
|
|
|
lines[1].should.match(/development/);
|
|
|
|
lines[1].should.match(/production/);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2017-01-18 18:29:38 +08:00
|
|
|
it("should not warn if the NoEmitOnErrorsPlugin is used over the NoErrorsPlugin", (done) => {
|
2016-12-30 16:52:37 +08:00
|
|
|
getErrors({
|
2017-11-21 17:41:01 +08:00
|
|
|
mode: "production",
|
|
|
|
entry: "./no-errors-deprecate"
|
2017-01-18 18:29:38 +08:00
|
|
|
}, (errors, warnings) => {
|
2016-12-30 16:52:37 +08:00
|
|
|
errors.length.should.be.eql(0);
|
|
|
|
warnings.length.should.be.eql(0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2017-01-18 18:29:38 +08:00
|
|
|
it("should not not emit if NoEmitOnErrorsPlugin is used and there is an error", (done) => {
|
2016-12-30 16:52:37 +08:00
|
|
|
getErrors({
|
2017-11-21 17:41:01 +08:00
|
|
|
mode: "production",
|
|
|
|
entry: "./missingFile"
|
2017-01-18 18:29:38 +08:00
|
|
|
}, (errors, warnings) => {
|
2016-12-30 16:52:37 +08:00
|
|
|
errors.length.should.be.eql(2);
|
|
|
|
warnings.length.should.be.eql(0);
|
|
|
|
errors.sort();
|
2017-01-18 18:29:38 +08:00
|
|
|
let lines = errors[0].split("\n");
|
2016-12-30 16:52:37 +08:00
|
|
|
lines[0].should.match(/missingFile.js/);
|
|
|
|
lines[1].should.match(/^Module not found/);
|
|
|
|
lines[1].should.match(/\.\/dir\/missing2/);
|
|
|
|
lines[2].should.match(/missingFile.js 12:9/);
|
|
|
|
lines = errors[1].split("\n");
|
|
|
|
lines[0].should.match(/missingFile.js/);
|
|
|
|
lines[1].should.match(/^Module not found/);
|
|
|
|
lines[1].should.match(/\.\/missing/);
|
|
|
|
lines[2].should.match(/missingFile.js 4:0/);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2017-01-18 18:29:38 +08:00
|
|
|
it("should throw an error when trying to use [chunkhash] when it's invalid", (done) => {
|
2015-06-27 17:34:17 +08:00
|
|
|
getErrors({
|
2017-11-21 17:41:01 +08:00
|
|
|
mode: "development",
|
2015-06-27 17:34:17 +08:00
|
|
|
entry: {
|
|
|
|
a: "./entry-point",
|
|
|
|
b: "./entry-point",
|
|
|
|
c: "./entry-point"
|
|
|
|
},
|
|
|
|
output: {
|
|
|
|
filename: "[chunkhash].js"
|
|
|
|
},
|
|
|
|
plugins: [
|
|
|
|
new webpack.HotModuleReplacementPlugin()
|
|
|
|
]
|
2017-01-18 18:29:38 +08:00
|
|
|
}, (errors, warnings) => {
|
2015-06-27 17:34:17 +08:00
|
|
|
errors.length.should.be.eql(3);
|
|
|
|
warnings.length.should.be.eql(0);
|
2017-01-18 18:29:38 +08:00
|
|
|
errors.forEach((error) => {
|
|
|
|
const lines = error.split("\n");
|
2015-06-27 17:34:17 +08:00
|
|
|
lines[0].should.match(/chunk (a|b|c)/);
|
|
|
|
lines[2].should.match(/\[chunkhash\].js/);
|
|
|
|
lines[2].should.match(/use \[hash\] instead/);
|
|
|
|
});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2018-02-22 08:10:56 +08:00
|
|
|
it("should show loader name when emit/throw an error or warning from a loader", () => {
|
2018-02-22 01:01:53 +08:00
|
|
|
return Promise.all([
|
|
|
|
getErrorsPromise({
|
|
|
|
mode: "development",
|
|
|
|
entry: "./not-a-json.js",
|
|
|
|
module: {
|
|
|
|
rules: [{
|
|
|
|
test: /not-a-json\.js$/,
|
|
|
|
use: [
|
|
|
|
"json-loader",
|
|
|
|
{
|
|
|
|
loader: path.resolve(base, "./emit-error-loader")
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}]
|
|
|
|
}
|
2018-02-21 17:43:02 +08:00
|
|
|
|
2018-02-22 01:01:53 +08:00
|
|
|
}, (errors, warnings) => {
|
|
|
|
warnings.length.should.be.eql(1);
|
2018-02-22 20:57:20 +08:00
|
|
|
warnings[0].split("\n")[1].should.match(/^Module Warning \(@ emit-error-loader\): [^\s]+/);
|
2018-02-22 01:01:53 +08:00
|
|
|
errors.length.should.be.eql(2);
|
2018-02-22 20:57:20 +08:00
|
|
|
errors[0].split("\n")[1].should.match(/^Module Error \(@ emit-error-loader\): [^\s]+/);
|
|
|
|
errors[1].split("\n")[1].should.match(/^Module build failed \(@ json-loader\): [^\s]+/);
|
2018-02-22 01:01:53 +08:00
|
|
|
}),
|
|
|
|
getErrorsPromise({
|
|
|
|
mode: "development",
|
|
|
|
entry: "./entry-point.js",
|
|
|
|
module: {
|
|
|
|
rules: [{
|
|
|
|
test: /entry-point\.js$/,
|
|
|
|
use: path.resolve(base, "./async-error-loader")
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
|
|
|
|
}, (errors, warnings) => {
|
|
|
|
errors.length.should.be.eql(1);
|
2018-02-22 20:57:20 +08:00
|
|
|
errors[0].split("\n")[1].should.match(/^Module build failed \(@ async-error-loader\): [^\s]+/);
|
2018-02-22 01:01:53 +08:00
|
|
|
}),
|
|
|
|
getErrorsPromise({
|
|
|
|
mode: "development",
|
|
|
|
entry: "./entry-point.js",
|
|
|
|
module: {
|
|
|
|
rules: [{
|
|
|
|
test: /entry-point\.js$/,
|
|
|
|
use: path.resolve(base, "./throw-error-loader")
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
|
|
|
|
}, (errors, warnings) => {
|
|
|
|
errors.length.should.be.eql(1);
|
2018-02-22 20:57:20 +08:00
|
|
|
errors[0].split("\n")[1].should.match(/^Module build failed \(@ throw-error-loader\): [^\s]+/);
|
|
|
|
}),
|
|
|
|
getErrorsPromise({
|
|
|
|
mode: "development",
|
|
|
|
entry: "./entry-point.js",
|
|
|
|
module: {
|
|
|
|
rules: [{
|
|
|
|
test: /entry-point\.js$/,
|
|
|
|
use: path.resolve(base, "./irregular-error-loader")
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
|
|
|
|
}, (errors, warnings) => {
|
|
|
|
warnings.length.should.be.eql(2);
|
|
|
|
warnings[0].split("\n")[1].should.match(/^Module Warning \(@ irregular-error-loader\): [^\s]+/);
|
|
|
|
warnings[1].split("\n")[1].should.match(/^Module Warning \(@ irregular-error-loader\): [^\s]+/);
|
|
|
|
|
|
|
|
errors.length.should.be.eql(3);
|
|
|
|
errors[0].split("\n")[1].should.match(/^Module Error \(@ irregular-error-loader\): [^\s]+/);
|
|
|
|
errors[1].split("\n")[1].should.match(/^Module Error \(@ irregular-error-loader\): [^\s]+/);
|
|
|
|
errors[2].split("\n")[1].should.match(/^Module build failed \(@ irregular-error-loader\): [^\s]+/);
|
2018-02-22 01:01:53 +08:00
|
|
|
}),
|
|
|
|
|
|
|
|
]);
|
2018-02-21 17:43:02 +08:00
|
|
|
});
|
2015-06-27 17:34:17 +08:00
|
|
|
});
|