Skip to content

Commit 1895b76

Browse files
committed
Add Tests checking chunkhash of runtime chunk only changes if needed
1 parent dc7ebeb commit 1895b76

File tree

12 files changed

+114
-3
lines changed

12 files changed

+114
-3
lines changed

test/WatchTestCases.test.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ describe("WatchTestCases", () => {
155155
return test;
156156
}
157157

158+
const globalContext = {
159+
console: console
160+
};
161+
158162
function _require(currentDirectory, module) {
159163
if(Array.isArray(module) || /^\.\.?\//.test(module)) {
160164
let fn;
@@ -170,11 +174,15 @@ describe("WatchTestCases", () => {
170174
p = path.join(currentDirectory, module);
171175
content = fs.readFileSync(p, "utf-8");
172176
}
173-
fn = vm.runInThisContext("(function(require, module, exports, __dirname, __filename, it, WATCH_STEP, STATS_JSON, STATE) {" + content + "\n})", p);
177+
if(options.target === "web" || options.target === "webworker") {
178+
fn = vm.runInNewContext("(function(require, module, exports, __dirname, __filename, it, WATCH_STEP, STATS_JSON, STATE, window) {" + content + "\n})", globalContext, p);
179+
} else {
180+
fn = vm.runInThisContext("(function(require, module, exports, __dirname, __filename, it, WATCH_STEP, STATS_JSON, STATE) {" + content + "\n})", p);
181+
}
174182
const m = {
175183
exports: {}
176184
};
177-
fn.call(m.exports, _require.bind(null, path.dirname(p)), m, m.exports, path.dirname(p), p, _it, run.name, jsonStats, state);
185+
fn.call(m.exports, _require.bind(null, path.dirname(p)), m, m.exports, path.dirname(p), p, _it, run.name, jsonStats, state, globalContext);
178186
return module.exports;
179187
} else if(testConfig.modules && module in testConfig.modules) {
180188
return testConfig.modules[module];
@@ -188,7 +196,7 @@ describe("WatchTestCases", () => {
188196
} catch(e) {}
189197

190198
if(testConfig.noTests) return process.nextTick(done);
191-
_require(outputDirectory, "./bundle.js");
199+
_require(outputDirectory, testConfig.bundlePath || "./bundle.js");
192200

193201
if(exportedTests < 1) return done(new Error("No tests exported by test case"));
194202
runIdx++;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "Normal";
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
it("should change chunkhash of main chunk", function () {
2+
const mainChunk = STATS_JSON.chunks.find((chunk) => chunk.names.indexOf("main") !== -1);
3+
(!mainChunk).should.be.false("Main chunk not found");
4+
switch (WATCH_STEP) {
5+
case "0":
6+
STATE.hash = mainChunk.hash;
7+
break;
8+
case "1":
9+
mainChunk.hash.should.be.not.eql(STATE.hash);
10+
break;
11+
}
12+
});
13+
14+
it("should load additional chunk", function (done) {
15+
const step = WATCH_STEP;
16+
import(/* webpackChunkName: "dynamic" */ './dynamic')
17+
.then((dynamic) => {
18+
switch (step) {
19+
case "0":
20+
dynamic.should.be.eql("Normal");
21+
break;
22+
case "1":
23+
dynamic.should.be.eql("Changed");
24+
break;
25+
}
26+
done();
27+
});
28+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "Changed";
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
output: {
3+
chunkFilename: "[name].[chunkhash].js"
4+
}
5+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "Normal";
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require("should");
2+
3+
import * as both from './dynamic-and-static'
4+
import * as staticModule from './static'
5+
6+
it("should not change chunkhash of manifest chunk", function () {
7+
const manifestChunk = STATS_JSON.chunks.find((chunk) => chunk.names.indexOf("manifest") !== -1);
8+
(!manifestChunk).should.be.false("Main chunk not found");
9+
switch (WATCH_STEP) {
10+
case "0":
11+
STATE.hash = manifestChunk.hash;
12+
staticModule.should.be.eql("Normal");
13+
both.should.be.eql("Normal");
14+
break;
15+
case "1":
16+
manifestChunk.hash.should.be.eql(STATE.hash);
17+
staticModule.should.be.eql("Changed");
18+
both.should.be.eql("Normal");
19+
break;
20+
case "2":
21+
manifestChunk.hash.should.be.eql(STATE.hash);
22+
staticModule.should.be.eql("Changed");
23+
both.should.be.eql("Changed");
24+
break;
25+
}
26+
});
27+
28+
it("should load additional chunk", function (done) {
29+
const step = WATCH_STEP;
30+
import(/* webpackChunkName: "dynamic-and-static" */ './dynamic-and-static')
31+
.then((dynamic) => {
32+
switch (step) {
33+
case "0":
34+
case "1":
35+
dynamic.should.be.eql("Normal");
36+
break;
37+
case "2":
38+
dynamic.should.be.eql("Changed");
39+
break;
40+
}
41+
done();
42+
})
43+
.catch(done);;
44+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "Normal";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "Changed";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "Changed";

0 commit comments

Comments
 (0)