webpack/test/RuleSet.unittest.js

421 lines
9.7 KiB
JavaScript
Raw Normal View History

"use strict";
2016-09-14 18:04:42 +08:00
2017-01-18 22:59:46 +08:00
const should = require("should");
const RuleSet = require("../lib/RuleSet");
2016-09-14 18:04:42 +08:00
function match(ruleSet, resource) {
2017-01-18 22:59:46 +08:00
const result = ruleSet.exec({
2016-09-14 18:04:42 +08:00
resource: resource
});
2017-01-18 22:59:46 +08:00
return result.filter((r) => {
2016-09-14 18:04:42 +08:00
return r.type === "use";
2017-01-18 22:59:46 +08:00
}).map(r => r.value).map(r => {
2016-09-14 18:04:42 +08:00
if(!r.options)
return r.loader;
if(typeof r.options === "string")
return r.loader + "?" + r.options;
return r.loader + "?" + JSON.stringify(r.options);
});
}
2016-09-14 18:04:42 +08:00
2017-01-18 22:59:46 +08:00
describe("RuleSet", () => {
it("should create RuleSet with a blank array", () => {
const loader = new RuleSet([]);
2018-01-24 20:17:21 +08:00
expect(loader.rules).toEqual([]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should create RuleSet and match with empty array", () => {
const loader = new RuleSet([]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "something")).toEqual([]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should not match with loaders array", () => {
const loader = new RuleSet([{
2016-09-14 18:04:42 +08:00
test: /\.css$/,
loader: "css"
2016-09-14 18:04:42 +08:00
}]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "something")).toEqual([]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should match with regex", () => {
const loader = new RuleSet([{
2016-09-14 18:04:42 +08:00
test: /\.css$/,
loader: "css"
2016-09-14 18:04:42 +08:00
}]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should match with string", () => {
const loader = new RuleSet([{
test: "style.css",
loader: "css"
2016-09-14 18:04:42 +08:00
}]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should match with function", () => {
const loader = new RuleSet([{
2016-09-14 18:04:42 +08:00
test: function(str) {
return str === "style.css";
2016-09-14 18:04:42 +08:00
},
loader: "css"
2016-09-14 18:04:42 +08:00
}]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should throw if invalid test", () => {
2018-01-24 20:17:21 +08:00
expect(() => {
2017-01-18 22:59:46 +08:00
const loader = new RuleSet([{
2016-09-14 18:04:42 +08:00
test: {
invalid: "test"
2016-09-14 18:04:42 +08:00
},
loader: "css"
2016-09-14 18:04:42 +08:00
}]);
2018-01-24 20:17:21 +08:00
match(loader, "style.css");
}).toThrow(/Unexcepted property invalid in condition/);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should accept multiple test array that all match", () => {
const loader = new RuleSet([{
2016-09-14 18:04:42 +08:00
test: [
/style.css/,
/yle.css/
],
loader: "css"
2016-09-14 18:04:42 +08:00
}]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should accept multiple test array that not all match", () => {
const loader = new RuleSet([{
2016-09-14 18:04:42 +08:00
test: [
/style.css/,
/something.css/
],
loader: "css"
2016-09-14 18:04:42 +08:00
}]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should not match if include does not match", () => {
const loader = new RuleSet([{
2016-09-14 18:04:42 +08:00
test: /\.css$/,
include: /output.css/,
loader: "css"
2016-09-14 18:04:42 +08:00
}]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual([]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should match if include matches", () => {
const loader = new RuleSet([{
2016-09-14 18:04:42 +08:00
test: /\.css$/,
include: /style.css/,
loader: "css"
2016-09-14 18:04:42 +08:00
}]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should not match if exclude matches", () => {
const loader = new RuleSet([{
2016-09-14 18:04:42 +08:00
test: /\.css$/,
exclude: /style.css/,
loader: "css"
2016-09-14 18:04:42 +08:00
}]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual([]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should match if exclude does not match", () => {
const loader = new RuleSet([{
2016-09-14 18:04:42 +08:00
test: /\.css$/,
exclude: /output.css/,
loader: "css"
2016-09-14 18:04:42 +08:00
}]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should work if a loader is applied to all files", () => {
const loader = new RuleSet([{
loader: "css"
2016-09-14 18:04:42 +08:00
}]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css"]);
expect(match(loader, "scripts.js")).toEqual(["css"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should work with using loader as string", () => {
const loader = new RuleSet([{
2016-09-14 18:04:42 +08:00
test: /\.css$/,
loader: "css"
2016-09-14 18:04:42 +08:00
}]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should work with using loader as array", () => {
const loader = new RuleSet([{
2016-09-14 18:04:42 +08:00
test: /\.css$/,
loader: ["css"]
2016-09-14 18:04:42 +08:00
}]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should work with using loaders as string", () => {
const loader = new RuleSet([{
2016-09-14 18:04:42 +08:00
test: /\.css$/,
loaders: "css"
2016-09-14 18:04:42 +08:00
}]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should work with using loaders as array", () => {
const loader = new RuleSet([{
2016-09-14 18:04:42 +08:00
test: /\.css$/,
loaders: ["css"]
2016-09-14 18:04:42 +08:00
}]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should throw if using loaders with non-string or array", () => {
2018-01-24 20:17:21 +08:00
expect(() => {
2017-01-18 22:59:46 +08:00
const loader = new RuleSet([{
2016-09-14 18:04:42 +08:00
test: /\.css$/,
loaders: {
someObj: true
}
}]);
2018-01-24 20:17:21 +08:00
match(loader, "style.css");
}).toThrow(/No loader specified/);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should work with using loader with inline query", () => {
const loader = new RuleSet([{
2016-09-14 18:04:42 +08:00
test: /\.css$/,
loader: "css?modules=1"
2016-09-14 18:04:42 +08:00
}]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css?modules=1"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should work with using loader with string query", () => {
const loader = new RuleSet([{
2016-09-14 18:04:42 +08:00
test: /\.css$/,
loader: "css",
query: "modules=1"
2016-09-14 18:04:42 +08:00
}]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css?modules=1"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should work with using loader with object query", () => {
const loader = new RuleSet([{
2016-09-14 18:04:42 +08:00
test: /\.css$/,
loader: "css",
2016-09-14 18:04:42 +08:00
query: {
modules: 1
}
}]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css?{\"modules\":1}"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should work with using array loaders with basic object notation", () => {
const loader = new RuleSet([{
2016-09-14 18:04:42 +08:00
test: /\.css$/,
loaders: [{
loader: "css"
2016-09-14 18:04:42 +08:00
}]
}]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should throw if using array loaders with object notation without specifying a loader", () => {
2018-01-24 20:17:21 +08:00
expect(() => {
2017-01-18 22:59:46 +08:00
const loader = new RuleSet([{
2016-09-14 18:04:42 +08:00
test: /\.css$/,
loaders: [{
stuff: 1
}]
}]);
match(loader, "style.css");
2018-01-24 20:17:21 +08:00
}).toThrow(/No loader specified/);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should work with using array loaders with object notation", () => {
const loader = new RuleSet([{
2016-09-14 18:04:42 +08:00
test: /\.css$/,
loaders: [{
loader: "css",
query: "modules=1"
2016-09-14 18:04:42 +08:00
}]
}]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css?modules=1"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should work with using multiple array loaders with object notation", () => {
const loader = new RuleSet([{
2016-09-14 18:04:42 +08:00
test: /\.css$/,
loaders: [{
loader: "style",
query: "filesize=1000"
2016-09-14 18:04:42 +08:00
}, {
loader: "css",
query: "modules=1"
2016-09-14 18:04:42 +08:00
}]
}]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["style?filesize=1000", "css?modules=1"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should work with using string multiple loaders", () => {
const loader = new RuleSet([{
2016-09-14 18:04:42 +08:00
test: /\.css$/,
loaders: "style?filesize=1000!css?modules=1"
2016-09-14 18:04:42 +08:00
}]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["style?filesize=1000", "css?modules=1"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should throw if using array loaders with a single legacy", () => {
2018-01-24 20:17:21 +08:00
expect(() => {
2017-01-18 22:59:46 +08:00
const loader = new RuleSet([{
2016-09-14 18:04:42 +08:00
test: /\.css$/,
loaders: ["style-loader", "css-loader"],
query: "modules=1"
2016-09-14 18:04:42 +08:00
}]);
2018-01-24 20:17:21 +08:00
match(loader, "style.css");
}).toThrow(/options\/query cannot be used with loaders/);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should work when using array loaders", () => {
const loader = new RuleSet([{
2016-09-14 18:04:42 +08:00
test: /\.css$/,
loaders: ["style-loader", "css-loader"]
2016-09-14 18:04:42 +08:00
}]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["style-loader", "css-loader"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should work when using an array of functions returning a loader", () => {
const loader = new RuleSet([{
test: /\.css$/,
use: [
function(data) {
2016-12-23 02:20:50 +08:00
return {
loader: "style-loader"
};
},
function(data) {
2016-12-23 02:20:50 +08:00
return {
loader: "css-loader"
};
},
]
}]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["style-loader", "css-loader"]);
});
2017-01-18 22:59:46 +08:00
it("should work when using an array of either functions or strings returning a loader", () => {
const loader = new RuleSet([{
test: /\.css$/,
use: [
"style-loader",
function(data) {
2016-12-23 02:20:50 +08:00
return {
loader: "css-loader"
};
},
]
}]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["style-loader", "css-loader"]);
});
2017-01-18 22:59:46 +08:00
it("should work when using an array of functions returning either a loader obejct or loader name string", () => {
const loader = new RuleSet([{
test: /\.css$/,
use: [
function(data) {
2017-11-15 21:08:11 +08:00
return "style-loader";
},
function(data) {
2016-12-23 02:20:50 +08:00
return {
loader: "css-loader"
};
},
]
}]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["style-loader", "css-loader"]);
});
2017-01-18 22:59:46 +08:00
it("should throw if using array loaders with invalid type", () => {
2018-01-24 20:17:21 +08:00
expect(() => {
2017-01-18 22:59:46 +08:00
const loader = new RuleSet([{
2016-09-14 18:04:42 +08:00
test: /\.css$/,
loaders: ["style-loader", "css-loader", 5],
2016-09-14 18:04:42 +08:00
}]);
2018-01-24 20:17:21 +08:00
match(loader, "style.css");
}).toThrow(/No loader specified/);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
describe("when exclude array holds an undefined item", () => {
function errorHasContext(err) {
2018-01-24 20:17:21 +08:00
return /Expected condition but got falsy value/.test(err) &&
/test/.test(err) &&
/include/.test(err) &&
/exclude/.test(err) &&
/node_modules/.test(err) &&
2018-01-24 20:17:21 +08:00
/undefined/.test(err);
}
2017-01-18 22:59:46 +08:00
it("should throw with context", () => {
2018-01-24 20:17:21 +08:00
try {
2017-01-18 22:59:46 +08:00
const loader = new RuleSet([{
test: /\.css$/,
loader: "css",
include: [
"src",
],
exclude: [
"node_modules",
undefined,
],
}]);
2018-01-24 20:17:21 +08:00
match(loader, "style.css");
throw new Error("unreachable");
} catch(e) {
expect(errorHasContext(e.message)).toBe(true);
}
});
2017-01-18 22:59:46 +08:00
it("in resource should throw with context", () => {
2018-01-24 20:17:21 +08:00
try {
2017-01-18 22:59:46 +08:00
const loader = new RuleSet([{
resource: {
test: /\.css$/,
include: [
"src",
],
exclude: [
"node_modules",
undefined,
],
},
}]);
2018-01-24 20:17:21 +08:00
match(loader, "style.css");
throw new Error("unreachable");
} catch(e) {
expect(errorHasContext(e.message)).toBe(true);
}
});
2017-01-18 22:59:46 +08:00
it("in issuer should throw with context", () => {
2018-01-24 20:17:21 +08:00
try {
2017-01-18 22:59:46 +08:00
const loader = new RuleSet([{
issuer: {
test: /\.css$/,
include: [
"src",
],
exclude: [
"node_modules",
undefined,
],
},
}]);
2018-01-24 20:17:21 +08:00
match(loader, "style.css");
throw new Error("unreachable");
} catch(e) {
expect(errorHasContext(e.message)).toBe(true);
}
});
});
2016-09-14 18:04:42 +08:00
});