Skip to content

Commit 358c457

Browse files
committed
added first HMR test case
1 parent b339a7e commit 358c457

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-0
lines changed

test/HotTestCases.test.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
var should = require("should");
2+
var path = require("path");
3+
var fs = require("fs");
4+
var vm = require("vm");
5+
var Test = require("mocha/lib/test");
6+
var checkArrayExpectation = require("./checkArrayExpectation");
7+
8+
var webpack = require("../lib/webpack");
9+
10+
describe("HotTestCases", function() {
11+
var casesPath = path.join(__dirname, "hotCases");
12+
var categories = fs.readdirSync(casesPath).filter(function(dir) {
13+
return fs.statSync(path.join(casesPath, dir)).isDirectory();
14+
});
15+
categories = categories.map(function(cat) {
16+
return {
17+
name: cat,
18+
tests: fs.readdirSync(path.join(casesPath, cat)).filter(function(folder) {
19+
return folder.indexOf("_") < 0;
20+
})
21+
};
22+
});
23+
categories.forEach(function(category) {
24+
describe(category.name, function() {
25+
category.tests.forEach(function(testName) {
26+
var suite = describe(testName, function() {});
27+
it(testName + " should compile", function(done) {
28+
this.timeout(10000);
29+
var testDirectory = path.join(casesPath, category.name, testName);
30+
var outputDirectory = path.join(__dirname, "js", "hot-cases", category.name, testName);
31+
var recordsPath = path.join(outputDirectory, "records.json");
32+
if(fs.exists(recordsPath))
33+
fs.unlinkSync(recordsPath);
34+
var options = {
35+
context: testDirectory,
36+
entry: "./index.js",
37+
output: {
38+
path: outputDirectory,
39+
filename: "bundle.js"
40+
},
41+
module: {
42+
loaders: [
43+
{
44+
test: /\.js$/,
45+
loader: path.join(__dirname, "hotCases", "fake-update-loader.js")
46+
}
47+
]
48+
},
49+
target: "async-node",
50+
plugins: [
51+
new webpack.HotModuleReplacementPlugin()
52+
],
53+
recordsPath: recordsPath,
54+
updateIndex: 0
55+
}
56+
var compiler = webpack(options);
57+
compiler.run(function(err, stats) {
58+
if(err) return done(err);
59+
var jsonStats = stats.toJson({
60+
errorDetails: true
61+
});
62+
if(checkArrayExpectation(testDirectory, jsonStats, "error", "Error", done)) return;
63+
if(checkArrayExpectation(testDirectory, jsonStats, "warning", "Warning", done)) return;
64+
var exportedTests = 0;
65+
66+
function _it(title, fn) {
67+
var test = new Test(title, fn);
68+
suite.addTest(test);
69+
exportedTests++;
70+
return test;
71+
}
72+
73+
function _next(callback) {
74+
options.updateIndex++;
75+
compiler.run(function(err, stats) {
76+
if(err) return done(err);
77+
var jsonStats = stats.toJson({
78+
errorDetails: true
79+
});
80+
if(checkArrayExpectation(testDirectory, jsonStats, "error", "Error", done)) return;
81+
if(checkArrayExpectation(testDirectory, jsonStats, "warning", "Warning", done)) return;
82+
if(callback) callback();
83+
})
84+
}
85+
86+
function _require(module) {
87+
if(module.substr(0, 2) === "./") {
88+
var p = path.join(outputDirectory, module);
89+
var fn = vm.runInThisContext("(function(require, module, exports, __dirname, __filename, it, NEXT) {" + fs.readFileSync(p, "utf-8") + "\n})", p);
90+
var module = {
91+
exports: {}
92+
};
93+
fn.call(module.exports, _require, module, module.exports, outputDirectory, p, _it, _next);
94+
return module.exports;
95+
} else return require(module);
96+
}
97+
_require("./bundle.js");
98+
if(exportedTests < 1) return done(new Error("No tests exported by test case"));
99+
process.nextTick(done);
100+
});
101+
});
102+
});
103+
});
104+
});
105+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = 1;
2+
---
3+
module.exports = 2;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var value = require("./file");
2+
3+
it("should accept a dependencies and require a new value", function(done) {
4+
value.should.be.eql(1);
5+
module.hot.accept("./file", function() {
6+
value = require("./file");
7+
value.should.be.eql(2);
8+
outside();
9+
done();
10+
});
11+
NEXT(require("../../update")(done));
12+
});
13+
14+
function outside() {
15+
value.should.be.eql(2);
16+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = function(source) {
2+
var idx = this.options.updateIndex;
3+
var items = source.split(/---+\r?\n/g);
4+
return items[idx] || items[items.length - 1];
5+
}

test/hotCases/update.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = function(done) {
2+
return function() {
3+
module.hot.check(true, function(err) {
4+
if(err) done(err);
5+
});
6+
}
7+
};

0 commit comments

Comments
 (0)