forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigTestCases.test.js
284 lines (274 loc) · 8.51 KB
/
ConfigTestCases.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
"use strict";
/* globals describe expect it */
const path = require("path");
const fs = require("fs");
const vm = require("vm");
const mkdirp = require("mkdirp");
const rimraf = require("rimraf");
const checkArrayExpectation = require("./checkArrayExpectation");
const createLazyTestEnv = require("./helpers/createLazyTestEnv");
const FakeDocument = require("./helpers/FakeDocument");
const Stats = require("../lib/Stats");
const webpack = require("../lib/webpack");
const prepareOptions = require("./helpers/prepareOptions");
describe("ConfigTestCases", () => {
const casesPath = path.join(__dirname, "configCases");
let categories = fs.readdirSync(casesPath);
jest.setTimeout(20000);
categories = categories.map(cat => {
return {
name: cat,
tests: fs
.readdirSync(path.join(casesPath, cat))
.filter(folder => {
return folder.indexOf("_") < 0;
})
.sort()
.filter(testName => {
const testDirectory = path.join(casesPath, cat, testName);
const filterPath = path.join(testDirectory, "test.filter.js");
if (fs.existsSync(filterPath) && !require(filterPath)()) {
describe.skip(testName, () => {
it("filtered", () => {});
});
return false;
}
return true;
})
};
});
categories.forEach(category => {
describe(category.name, () => {
category.tests.forEach(testName => {
describe(testName, function() {
const testDirectory = path.join(casesPath, category.name, testName);
const outputDirectory = path.join(
__dirname,
"js",
"config",
category.name,
testName
);
it(
testName + " should compile",
() =>
new Promise((resolve, reject) => {
const done = err => {
if (err) return reject(err);
resolve();
};
rimraf.sync(outputDirectory);
mkdirp.sync(outputDirectory);
const options = prepareOptions(
require(path.join(testDirectory, "webpack.config.js")),
{ testPath: outputDirectory }
);
const optionsArr = [].concat(options);
optionsArr.forEach((options, idx) => {
if (!options.context) options.context = testDirectory;
if (!options.mode) options.mode = "production";
if (!options.optimization) options.optimization = {};
if (options.optimization.minimize === undefined)
options.optimization.minimize = false;
if (!options.entry) options.entry = "./index.js";
if (!options.target) options.target = "async-node";
if (!options.output) options.output = {};
if (!options.output.path)
options.output.path = outputDirectory;
if (typeof options.output.pathinfo === "undefined")
options.output.pathinfo = true;
if (!options.output.filename)
options.output.filename = "bundle" + idx + ".js";
});
let testConfig = {
findBundle: function(i, options) {
const ext = path.extname(options.output.filename);
if (
fs.existsSync(
path.join(options.output.path, "bundle" + i + ext)
)
) {
return "./bundle" + i + ext;
}
},
timeout: 30000
};
try {
// try to load a test file
testConfig = Object.assign(
testConfig,
require(path.join(testDirectory, "test.config.js"))
);
} catch (e) {
// ignored
}
if (testConfig.timeout) setDefaultTimeout(testConfig.timeout);
webpack(options, (err, stats) => {
if (err) {
const fakeStats = {
errors: [err.stack]
};
if (
checkArrayExpectation(
testDirectory,
fakeStats,
"error",
"Error",
done
)
)
return;
// Wait for uncaught errors to occur
return setTimeout(done, 200);
}
const statOptions = Stats.presetToOptions("verbose");
statOptions.colors = false;
mkdirp.sync(outputDirectory);
fs.writeFileSync(
path.join(outputDirectory, "stats.txt"),
stats.toString(statOptions),
"utf-8"
);
const jsonStats = stats.toJson({
errorDetails: true
});
if (
checkArrayExpectation(
testDirectory,
jsonStats,
"error",
"Error",
done
)
)
return;
if (
checkArrayExpectation(
testDirectory,
jsonStats,
"warning",
"Warning",
done
)
)
return;
const globalContext = {
console: console,
expect: expect,
setTimeout: setTimeout,
clearTimeout: clearTimeout,
document: new FakeDocument(),
location: {
href: "https://test.cases/path/index.html",
origin: "https://test.cases",
toString() {
return "https://test.cases/path/index.html";
}
}
};
function _require(currentDirectory, module) {
if (Array.isArray(module) || /^\.\.?\//.test(module)) {
let content;
let p;
if (Array.isArray(module)) {
p = path.join(currentDirectory, module[0]);
content = module
.map(arg => {
p = path.join(currentDirectory, arg);
return fs.readFileSync(p, "utf-8");
})
.join("\n");
} else {
p = path.join(currentDirectory, module);
content = fs.readFileSync(p, "utf-8");
}
const m = {
exports: {}
};
let runInNewContext = false;
const moduleScope = {
require: _require.bind(null, path.dirname(p)),
importScripts: _require.bind(null, path.dirname(p)),
module: m,
exports: m.exports,
__dirname: path.dirname(p),
__filename: p,
it: _it,
beforeEach: _beforeEach,
afterEach: _afterEach,
expect,
jest,
_globalAssign: { expect },
nsObj: m => {
Object.defineProperty(m, Symbol.toStringTag, {
value: "Module"
});
return m;
}
};
if (
options.target === "web" ||
options.target === "webworker"
) {
moduleScope.window = globalContext;
moduleScope.self = globalContext;
runInNewContext = true;
}
if (testConfig.moduleScope) {
testConfig.moduleScope(moduleScope);
}
const args = Object.keys(moduleScope).join(", ");
if (!runInNewContext)
content = `Object.assign(global, _globalAssign); ${content}`;
const code = `(function({${args}}) {${content}\n})`;
const fn = runInNewContext
? vm.runInNewContext(code, globalContext, p)
: vm.runInThisContext(code, p);
fn.call(m.exports, moduleScope);
return m.exports;
} else if (
testConfig.modules &&
module in testConfig.modules
) {
return testConfig.modules[module];
} else return require(module);
}
let filesCount = 0;
if (testConfig.noTests) return process.nextTick(done);
if (testConfig.beforeExecute) testConfig.beforeExecute();
for (let i = 0; i < optionsArr.length; i++) {
const bundlePath = testConfig.findBundle(i, optionsArr[i]);
if (bundlePath) {
filesCount++;
_require(outputDirectory, bundlePath);
}
}
// give a free pass to compilation that generated an error
if (
!jsonStats.errors.length &&
filesCount !== optionsArr.length
)
return done(
new Error(
"Should have found at least one bundle file per webpack config"
)
);
if (testConfig.afterExecute) testConfig.afterExecute();
if (getNumberOfTests() < filesCount)
return done(new Error("No tests exported by test case"));
done();
});
})
);
const {
it: _it,
beforeEach: _beforeEach,
afterEach: _afterEach,
setDefaultTimeout,
getNumberOfTests
} = createLazyTestEnv(jasmine.getEnv(), 10000);
});
});
});
});
});