forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHotTestCases.test.js
More file actions
103 lines (98 loc) · 3.34 KB
/
HotTestCases.test.js
File metadata and controls
103 lines (98 loc) · 3.34 KB
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
var should = require("should");
var path = require("path");
var fs = require("fs");
var vm = require("vm");
var Test = require("mocha/lib/test");
var checkArrayExpectation = require("./checkArrayExpectation");
var webpack = require("../lib/webpack");
describe("HotTestCases", function() {
var casesPath = path.join(__dirname, "hotCases");
var categories = fs.readdirSync(casesPath).filter(function(dir) {
return fs.statSync(path.join(casesPath, dir)).isDirectory();
});
categories = categories.map(function(cat) {
return {
name: cat,
tests: fs.readdirSync(path.join(casesPath, cat)).filter(function(folder) {
return folder.indexOf("_") < 0;
})
};
});
categories.forEach(function(category) {
describe(category.name, function() {
category.tests.forEach(function(testName) {
var suite = describe(testName, function() {});
it(testName + " should compile", function(done) {
this.timeout(10000);
var testDirectory = path.join(casesPath, category.name, testName);
var outputDirectory = path.join(__dirname, "js", "hot-cases", category.name, testName);
var recordsPath = path.join(outputDirectory, "records.json");
if(fs.exists(recordsPath))
fs.unlinkSync(recordsPath);
var options = {
context: testDirectory,
entry: "./index.js",
output: {
path: outputDirectory,
filename: "bundle.js"
},
module: {
loaders: [{
test: /\.js$/,
loader: path.join(__dirname, "hotCases", "fake-update-loader.js")
}]
},
target: "async-node",
plugins: [
new webpack.HotModuleReplacementPlugin()
],
recordsPath: recordsPath,
updateIndex: 0
}
var compiler = webpack(options);
compiler.run(function(err, stats) {
if(err) return done(err);
var jsonStats = stats.toJson({
errorDetails: true
});
if(checkArrayExpectation(testDirectory, jsonStats, "error", "Error", done)) return;
if(checkArrayExpectation(testDirectory, jsonStats, "warning", "Warning", done)) return;
var exportedTests = 0;
function _it(title, fn) {
var test = new Test(title, fn);
suite.addTest(test);
exportedTests++;
return test;
}
function _next(callback) {
options.updateIndex++;
compiler.run(function(err, stats) {
if(err) return done(err);
var jsonStats = stats.toJson({
errorDetails: true
});
if(checkArrayExpectation(testDirectory, jsonStats, "error", "Error", done)) return;
if(checkArrayExpectation(testDirectory, jsonStats, "warning", "Warning", done)) return;
if(callback) callback();
})
}
function _require(module) {
if(module.substr(0, 2) === "./") {
var p = path.join(outputDirectory, module);
var fn = vm.runInThisContext("(function(require, module, exports, __dirname, __filename, it, NEXT) {" + fs.readFileSync(p, "utf-8") + "\n})", p);
var module = {
exports: {}
};
fn.call(module.exports, _require, module, module.exports, outputDirectory, p, _it, _next);
return module.exports;
} else return require(module);
}
_require("./bundle.js");
if(exportedTests < 1) return done(new Error("No tests exported by test case"));
process.nextTick(done);
});
});
});
});
});
});