Skip to content

Commit 3855b77

Browse files
authored
Merge pull request webpack#4201 from fedebertolini/test/environment-plugin
Test - improve EnvironmentPlugin code coverage
2 parents b7d0155 + 0787748 commit 3855b77

3 files changed

Lines changed: 71 additions & 15 deletions

File tree

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
1-
module.exports = [
2-
[/(aaa)/, /resolve 'bbb'/],
3-
[/(aaa)/, /resolve 'ccc'/],
4-
[/(aaa)/, /resolve 'ddd'/],
5-
[/(bbbccc)/, /resolve 'aaa'/],
6-
[/(bbbccc)/, /resolve 'ddd'/],
7-
[/(ddd)/, /resolve 'aaa'/],
8-
[/(ddd)/, /resolve 'bbb'/],
9-
[/(ddd)/, /resolve 'ccc'/],
10-
[/(ddd)/, /resolve 'ddd'/],
11-
];
1+
const variables = ['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff', 'ggg', 'hhh'];
2+
const modules = [{
3+
name: 'aaa',
4+
variables: ['aaa']
5+
}, {
6+
name: 'bbbccc',
7+
variables: ['bbb', 'ccc']
8+
}, {
9+
name: 'ddd',
10+
variables: []
11+
}, {
12+
name: 'eeefff',
13+
variables: ['eee', 'fff']
14+
}, {
15+
name: 'ggghhh',
16+
variables: ['ggg', 'hhh']
17+
}];
18+
19+
// build an array of regular expressions of expected errors
20+
const regex = [];
21+
modules.forEach(module => {
22+
variables.forEach(variable => {
23+
if (module.variables.indexOf(variable) === -1) {
24+
// the module doesn't include the env variable, an error is expected when requiring the variable
25+
regex.push([
26+
new RegExp(`(${module.name})`),
27+
new RegExp(`Can't resolve '${variable}'`),
28+
]);
29+
}
30+
});
31+
});
32+
33+
module.exports = regex;
Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
1-
it("should import a single process.env var", function() {
1+
it("should import a single process.env var", () => {
22
if(process.env.AAA !== "aaa")
33
require.include("aaa");
44
});
55

6-
it("should import multiple process.env vars", function() {
6+
it("should import multiple process.env vars", () => {
77
if(process.env.BBB !== "bbb")
88
require.include("bbb");
99
if(process.env.CCC !== "ccc")
1010
require.include("ccc");
1111
});
1212

13-
it("should warn when a process.env variable is undefined", function() {
13+
it("should warn when a process.env variable is undefined", () => {
1414
if(process.env.DDD !== "ddd")
1515
require.include("ddd");
1616
});
17+
18+
it("should import an array of process.env vars", () => {
19+
if(process.env.EEE !== "eee")
20+
require.include("eee");
21+
if(process.env.FFF !== "fff")
22+
require.include("fff");
23+
});
24+
25+
it("should import multiple process.env var with default values", () => {
26+
if(process.env.GGG !== "ggg")
27+
require.include("ggg");
28+
if(process.env.HHH !== "hhh")
29+
require.include("hhh");
30+
});

test/configCases/plugins/environment-plugin/webpack.config.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
var EnvironmentPlugin = require("../../../../lib/EnvironmentPlugin");
1+
const EnvironmentPlugin = require("../../../../lib/EnvironmentPlugin");
2+
23
process.env.AAA = "aaa";
34
process.env.BBB = "bbb";
45
process.env.CCC = "ccc";
6+
process.env.EEE = "eee";
7+
process.env.FFF = "fff";
8+
process.env.GGG = "ggg";
9+
510
module.exports = [{
611
name: "aaa",
712
module: { unknownContextRegExp: /$^/, unknownContextCritical: false },
@@ -20,4 +25,19 @@ module.exports = [{
2025
plugins: [
2126
new EnvironmentPlugin("DDD")
2227
]
28+
}, {
29+
name: "eeefff",
30+
module: { unknownContextRegExp: /$^/, unknownContextCritical: false },
31+
plugins: [
32+
new EnvironmentPlugin(["EEE", "FFF"])
33+
]
34+
}, {
35+
name: "ggghhh",
36+
module: { unknownContextRegExp: /$^/, unknownContextCritical: false },
37+
plugins: [
38+
new EnvironmentPlugin({
39+
GGG: 'ggg-default',
40+
HHH: 'hhh'
41+
})
42+
]
2343
}];

0 commit comments

Comments
 (0)