Skip to content

Commit 57e09a6

Browse files
committed
add test for prefetch, preload and script loading jsonp runtime
1 parent 014e65e commit 57e09a6

File tree

8 files changed

+126
-3
lines changed

8 files changed

+126
-3
lines changed

test/ConfigTestCases.test.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,18 @@ describe("ConfigTestCases", () => {
149149
return test;
150150
}
151151

152+
function _beforeEach(title, fn) {
153+
return suite.beforeEach(title, fn);
154+
}
155+
156+
function _afterEach(title, fn) {
157+
return suite.afterEach(title, fn);
158+
}
159+
152160
const globalContext = {
153-
console: console
161+
console: console,
162+
setTimeout: setTimeout,
163+
clearTimeout: clearTimeout
154164
};
155165

156166
function _require(currentDirectory, module) {
@@ -175,15 +185,15 @@ describe("ConfigTestCases", () => {
175185
options.target === "webworker"
176186
) {
177187
fn = vm.runInNewContext(
178-
"(function(require, module, exports, __dirname, __filename, it, window) {" +
188+
"(function(require, module, exports, __dirname, __filename, it, beforeEach, afterEach, window) {" +
179189
content +
180190
"\n})",
181191
globalContext,
182192
p
183193
);
184194
} else {
185195
fn = vm.runInThisContext(
186-
"(function(require, module, exports, __dirname, __filename, it) {" +
196+
"(function(require, module, exports, __dirname, __filename, it, beforeEach, afterEach) {" +
187197
content +
188198
"\n})",
189199
p
@@ -200,6 +210,8 @@ describe("ConfigTestCases", () => {
200210
path.dirname(p),
201211
p,
202212
_it,
213+
_beforeEach,
214+
_afterEach,
203215
globalContext
204216
);
205217
return m.exports;

test/configCases/web/prefetch-preload/chunk1-a.js

Whitespace-only changes.

test/configCases/web/prefetch-preload/chunk1-b.js

Whitespace-only changes.

test/configCases/web/prefetch-preload/chunk1-c.js

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default function() {
2+
import(/* webpackPrefetch: true, webpackChunkName: "chunk1-a" */ "./chunk1-a");
3+
import(/* webpackPreload: true, webpackChunkName: "chunk1-b" */ "./chunk1-b");
4+
import(/* webpackPrefetch: 10, webpackChunkName: "chunk1-c" */ "./chunk1-c");
5+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const should = require("should");
2+
const FakeDocument = require("../../../helpers/FakeDocument");
3+
4+
let oldNonce;
5+
let oldPublicPath;
6+
7+
beforeEach(() => {
8+
oldNonce = __webpack_nonce__;
9+
oldPublicPath = __webpack_public_path__;
10+
global.document = new FakeDocument();
11+
});
12+
13+
afterEach(() => {
14+
delete global.document;
15+
__webpack_nonce__ = oldNonce;
16+
__webpack_public_path__ = oldPublicPath;
17+
})
18+
19+
it("should prefetch and preload child chunks on chunk load", () => {
20+
__webpack_nonce__ = "nonce";
21+
__webpack_public_path__ = "/public/path/";
22+
23+
const promise = import(/* webpackChunkName: "chunk1" */ "./chunk1");
24+
document.head._children.length.should.be.eql(1);
25+
const script = document.head._children[0];
26+
script._type.should.be.eql("script");
27+
should(script.src).be.eql("/public/path/chunk1.js")
28+
should(script.getAttribute("nonce")).be.eql("nonce")
29+
should(script.crossOrigin).be.eql("anonymous");
30+
should(script.onload).be.type("function");
31+
32+
__non_webpack_require__("./chunk1.js");
33+
script.onload();
34+
35+
return promise.then((ex) => {
36+
document.head._children.length.should.be.eql(4);
37+
38+
let link = document.head._children[1];
39+
link._type.should.be.eql("link");
40+
should(link.rel).be.eql("preload");
41+
should(link.as).be.eql("script");
42+
should(link.href).be.eql("/public/path/chunk1-b.js");
43+
should(link.charset).be.eql("utf-8");
44+
should(link.getAttribute("nonce")).be.eql("nonce");
45+
should(link.crossOrigin).be.eql("anonymous");
46+
47+
link = document.head._children[2];
48+
link._type.should.be.eql("link");
49+
should(link.rel).be.eql("prefetch");
50+
should(link.href).be.eql("/public/path/chunk1-c.js");
51+
52+
link = document.head._children[3];
53+
link._type.should.be.eql("link");
54+
should(link.rel).be.eql("prefetch");
55+
should(link.href).be.eql("/public/path/chunk1-a.js");
56+
});
57+
})
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
target: "web",
3+
output: {
4+
chunkFilename: "[name].js",
5+
crossOriginLoading: "anonymous"
6+
},
7+
performance: {
8+
hints: false
9+
},
10+
optimization: {
11+
minimize: false
12+
}
13+
};

test/helpers/FakeDocument.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module.exports = class FakeDocument {
2+
constructor() {
3+
this.head = this.createElement("head");
4+
}
5+
6+
createElement(type) {
7+
return new FakeElement(type);
8+
}
9+
10+
getElementsByTagName(name) {
11+
if (name === "head") return [this.head];
12+
throw new Error(
13+
`FakeDocument.getElementsByTagName(${name}): not implemented`
14+
);
15+
}
16+
};
17+
18+
class FakeElement {
19+
constructor(type) {
20+
this._type = type;
21+
this._children = [];
22+
this._attributes = Object.create(null);
23+
}
24+
25+
appendChild(node) {
26+
this._children.push(node);
27+
}
28+
29+
setAttribute(name, value) {
30+
this._attributes[name] = value;
31+
}
32+
33+
getAttribute(name) {
34+
return this._attributes[name];
35+
}
36+
}

0 commit comments

Comments
 (0)