webpack/test/ConfigTestCases.test.js

141 lines
5.2 KiB
JavaScript
Raw Normal View History

2017-01-18 17:08:00 +08:00
"use strict";
/* globals describe it */
2017-01-18 17:08:00 +08:00
const should = require("should");
const path = require("path");
const fs = require("fs");
const vm = require("vm");
const Test = require("mocha/lib/test");
const checkArrayExpectation = require("./checkArrayExpectation");
2014-06-18 04:34:30 +08:00
2017-01-18 17:08:00 +08:00
const Stats = require("../lib/Stats");
const webpack = require("../lib/webpack");
const prepareOptions = require("../lib/prepareOptions");
2014-06-18 04:34:30 +08:00
2017-01-18 17:08:00 +08:00
describe("ConfigTestCases", () => {
const casesPath = path.join(__dirname, "configCases");
let categories = fs.readdirSync(casesPath);
2017-01-18 17:08:00 +08:00
categories = categories.map((cat) => {
2014-06-18 04:34:30 +08:00
return {
name: cat,
2017-01-18 17:08:00 +08:00
tests: fs.readdirSync(path.join(casesPath, cat)).filter((folder) => {
2014-06-18 04:34:30 +08:00
return folder.indexOf("_") < 0;
2017-01-18 17:08:00 +08:00
}).sort().filter((testName) => {
const testDirectory = path.join(casesPath, cat, testName);
const filterPath = path.join(testDirectory, "test.filter.js");
if(fs.existsSync(filterPath) && !require(filterPath)()) {
2017-01-18 17:08:00 +08:00
describe.skip(testName, () => it("filtered"));
return false;
}
return true;
})
};
});
2017-01-18 17:08:00 +08:00
categories.forEach((category) => {
describe(category.name, () => {
category.tests.forEach((testName) => {
const suite = describe(testName, () => {});
2014-06-18 04:34:30 +08:00
it(testName + " should compile", function(done) {
2017-01-18 17:08:00 +08:00
const testDirectory = path.join(casesPath, category.name, testName);
const outputDirectory = path.join(__dirname, "js", "config", category.name, testName);
const options = prepareOptions(require(path.join(testDirectory, "webpack.config.js")));
2017-01-18 17:08:00 +08:00
const optionsArr = [].concat(options);
optionsArr.forEach((options, idx) => {
2014-06-19 05:02:33 +08:00
if(!options.context) options.context = testDirectory;
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;
2016-09-07 15:46:27 +08:00
if(typeof options.output.pathinfo === "undefined") options.output.pathinfo = true;
2014-06-19 05:02:33 +08:00
if(!options.output.filename) options.output.filename = "bundle" + idx + ".js";
if(!options.output.chunkFilename) options.output.chunkFilename = "[id].bundle" + idx + ".js";
2014-06-19 05:02:33 +08:00
});
let testConfig = {
findBundle: function(i, options) {
if(fs.existsSync(path.join(options.output.path, "bundle" + i + ".js"))) {
return "./bundle" + i + ".js";
}
},
timeout: 30000
};
try {
// try to load a test file
testConfig = Object.assign(testConfig, require(path.join(testDirectory, "test.config.js")));
} catch(e) {}
this.timeout(testConfig.timeout);
2017-01-18 17:08:00 +08:00
webpack(options, (err, stats) => {
2014-06-18 04:34:30 +08:00
if(err) return done(err);
2017-01-18 17:08:00 +08:00
const statOptions = Stats.presetToOptions("verbose");
2016-09-07 15:46:27 +08:00
statOptions.colors = false;
fs.writeFileSync(path.join(outputDirectory, "stats.txt"), stats.toString(statOptions), "utf-8");
2017-01-18 17:08:00 +08:00
const jsonStats = stats.toJson({
2014-06-18 04:34:30 +08:00
errorDetails: true
});
if(checkArrayExpectation(testDirectory, jsonStats, "error", "Error", done)) return;
if(checkArrayExpectation(testDirectory, jsonStats, "warning", "Warning", done)) return;
2017-01-18 17:08:00 +08:00
let exportedTests = 0;
2015-08-09 18:42:43 +08:00
2014-06-18 04:34:30 +08:00
function _it(title, fn) {
2017-01-18 17:08:00 +08:00
const test = new Test(title, fn);
2014-06-18 04:34:30 +08:00
suite.addTest(test);
2014-08-05 00:08:52 +08:00
exportedTests++;
2014-06-18 04:34:30 +08:00
return test;
}
2015-08-09 18:42:43 +08:00
2017-01-18 17:08:00 +08:00
const globalContext = {
2016-06-16 05:25:40 +08:00
console: console
};
2016-09-21 23:26:25 +08:00
function _require(currentDirectory, module) {
2016-06-16 05:25:40 +08:00
if(Array.isArray(module) || /^\.\.?\//.test(module)) {
2017-01-18 17:08:00 +08:00
let fn;
let content;
let p;
2016-06-16 05:25:40 +08:00
if(Array.isArray(module)) {
2017-01-18 17:08:00 +08:00
p = path.join(currentDirectory, module[0]);
content = module.map((arg) => {
p = path.join(currentDirectory, arg);
2016-06-16 05:25:40 +08:00
return fs.readFileSync(p, "utf-8");
}).join("\n");
} else {
2017-01-18 17:08:00 +08:00
p = path.join(currentDirectory, module);
2016-06-16 05:25:40 +08:00
content = fs.readFileSync(p, "utf-8");
}
if(options.target === "web" || options.target === "webworker") {
2016-06-16 05:25:40 +08:00
fn = vm.runInNewContext("(function(require, module, exports, __dirname, __filename, it, window) {" + content + "\n})", globalContext, p);
} else {
2016-06-16 05:25:40 +08:00
fn = vm.runInThisContext("(function(require, module, exports, __dirname, __filename, it) {" + content + "\n})", p);
}
2017-01-18 17:08:00 +08:00
const m = {
2015-08-09 18:42:43 +08:00
exports: {}
};
2017-01-18 17:08:00 +08:00
fn.call(m.exports, _require.bind(null, path.dirname(p)), m, m.exports, path.dirname(p), p, _it, globalContext);
return m.exports;
} else if(testConfig.modules && module in testConfig.modules) {
return testConfig.modules[module];
2014-06-18 04:34:30 +08:00
} else return require(module);
}
2017-01-18 17:08:00 +08:00
let filesCount = 0;
if(testConfig.noTests) return process.nextTick(done);
2017-01-18 17:08:00 +08:00
for(let i = 0; i < optionsArr.length; i++) {
const bundlePath = testConfig.findBundle(i, optionsArr[i]);
2015-08-09 18:42:43 +08:00
if(bundlePath) {
2014-08-05 00:08:52 +08:00
filesCount++;
2016-09-21 23:26:25 +08:00
_require(outputDirectory, bundlePath);
2014-08-05 00:08:52 +08:00
}
}
2014-08-22 19:51:24 +08:00
// 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"));
2014-08-05 00:08:52 +08:00
if(exportedTests < filesCount) return done(new Error("No tests exported by test case"));
process.nextTick(done);
2014-06-18 04:34:30 +08:00
});
});
});
});
});
2014-08-22 19:51:24 +08:00
});