webpack/test/HotTestCases.test.js

119 lines
3.9 KiB
JavaScript
Raw Normal View History

2017-01-18 18:44:11 +08:00
"use strict";
2015-11-01 23:11:35 +08:00
2017-01-18 18:44:11 +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 ExtractTextPlugin = require("extract-text-webpack-plugin");
const checkArrayExpectation = require("./checkArrayExpectation");
2015-11-01 23:11:35 +08:00
2017-01-18 18:44:11 +08:00
const webpack = require("../lib/webpack");
describe("HotTestCases", () => {
const casesPath = path.join(__dirname, "hotCases");
let categories = fs.readdirSync(casesPath).filter((dir) =>
fs.statSync(path.join(casesPath, dir)).isDirectory());
categories = categories.map((cat) => {
2015-11-01 23:11:35 +08:00
return {
name: cat,
2017-01-18 18:44:11 +08:00
tests: fs.readdirSync(path.join(casesPath, cat)).filter((folder) => folder.indexOf("_") < 0)
2015-11-01 23:11:35 +08:00
};
});
2017-01-18 18:44:11 +08:00
categories.forEach((category) => {
describe(category.name, () => {
category.tests.forEach((testName) => {
const suite = describe(testName, function() {
2015-11-01 23:11:35 +08:00
this.timeout(10000);
2015-11-21 03:14:49 +08:00
});
2017-01-18 18:44:11 +08:00
it(testName + " should compile", (done) => {
const testDirectory = path.join(casesPath, category.name, testName);
const outputDirectory = path.join(__dirname, "js", "hot-cases", category.name, testName);
const recordsPath = path.join(outputDirectory, "records.json");
2016-06-04 23:30:10 +08:00
if(fs.existsSync(recordsPath))
2015-11-01 23:11:35 +08:00
fs.unlinkSync(recordsPath);
2017-01-18 18:44:11 +08:00
const fakeUpdateLoaderOptions = {
options: {
updateIndex: 0
}
};
2017-01-18 18:44:11 +08:00
const options = {
2015-11-01 23:11:35 +08:00
context: testDirectory,
entry: "./index.js",
output: {
path: outputDirectory,
filename: "bundle.js"
},
module: {
2015-11-02 01:25:42 +08:00
loaders: [{
test: /\.js$/,
loader: path.join(__dirname, "hotCases", "fake-update-loader.js"),
enforce: "pre"
}, {
test: /\.css$/,
2017-02-15 21:38:19 +08:00
use: ExtractTextPlugin.extract({
fallback: "style-loader",
loader: "css-loader"
})
2015-11-02 01:25:42 +08:00
}]
2015-11-01 23:11:35 +08:00
},
target: "async-node",
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(),
new webpack.LoaderOptionsPlugin(fakeUpdateLoaderOptions),
new ExtractTextPlugin("bundle.css")
2015-11-01 23:11:35 +08:00
],
recordsPath: recordsPath
2017-01-18 18:44:11 +08:00
};
const compiler = webpack(options);
compiler.run((err, stats) => {
2015-11-01 23:11:35 +08:00
if(err) return done(err);
2017-01-18 18:44:11 +08:00
const jsonStats = stats.toJson({
2015-11-01 23:11:35 +08:00
errorDetails: true
});
if(checkArrayExpectation(testDirectory, jsonStats, "error", "Error", done)) return;
if(checkArrayExpectation(testDirectory, jsonStats, "warning", "Warning", done)) return;
2017-01-18 18:44:11 +08:00
let exportedTests = 0;
2015-11-01 23:11:35 +08:00
function _it(title, fn) {
2017-01-18 18:44:11 +08:00
const test = new Test(title, fn);
2015-11-01 23:11:35 +08:00
suite.addTest(test);
exportedTests++;
return test;
}
function _next(callback) {
fakeUpdateLoaderOptions.options.updateIndex++;
2017-01-18 18:44:11 +08:00
compiler.run((err, stats) => {
2015-11-01 23:11:35 +08:00
if(err) return done(err);
2017-01-18 18:44:11 +08:00
const jsonStats = stats.toJson({
2015-11-01 23:11:35 +08:00
errorDetails: true
});
if(checkArrayExpectation(testDirectory, jsonStats, "error", "errors" + fakeUpdateLoaderOptions.options.updateIndex, "Error", done)) return;
if(checkArrayExpectation(testDirectory, jsonStats, "warning", "warnings" + fakeUpdateLoaderOptions.options.updateIndex, "Warning", done)) return;
if(callback) callback(jsonStats);
2017-01-18 18:44:11 +08:00
});
2015-11-01 23:11:35 +08:00
}
function _require(module) {
if(module.substr(0, 2) === "./") {
2017-01-18 18:44:11 +08:00
const p = path.join(outputDirectory, module);
const fn = vm.runInThisContext("(function(require, module, exports, __dirname, __filename, it, NEXT, STATS) {" + fs.readFileSync(p, "utf-8") + "\n})", p);
const m = {
2015-11-01 23:11:35 +08:00
exports: {}
};
2017-01-18 18:44:11 +08:00
fn.call(m.exports, _require, m, m.exports, outputDirectory, p, _it, _next, jsonStats);
return m.exports;
2015-11-01 23:11:35 +08:00
} else return require(module);
}
_require("./bundle.js");
if(exportedTests < 1) return done(new Error("No tests exported by test case"));
process.nextTick(done);
});
});
});
});
});
});