add test for prefetch, preload and script loading jsonp runtime

This commit is contained in:
Tobias Koppers 2018-04-16 17:43:45 +02:00
parent 014e65e27e
commit 57e09a67a1
8 changed files with 126 additions and 3 deletions

View File

@ -149,8 +149,18 @@ describe("ConfigTestCases", () => {
return test;
}
function _beforeEach(title, fn) {
return suite.beforeEach(title, fn);
}
function _afterEach(title, fn) {
return suite.afterEach(title, fn);
}
const globalContext = {
console: console
console: console,
setTimeout: setTimeout,
clearTimeout: clearTimeout
};
function _require(currentDirectory, module) {
@ -175,7 +185,7 @@ describe("ConfigTestCases", () => {
options.target === "webworker"
) {
fn = vm.runInNewContext(
"(function(require, module, exports, __dirname, __filename, it, window) {" +
"(function(require, module, exports, __dirname, __filename, it, beforeEach, afterEach, window) {" +
content +
"\n})",
globalContext,
@ -183,7 +193,7 @@ describe("ConfigTestCases", () => {
);
} else {
fn = vm.runInThisContext(
"(function(require, module, exports, __dirname, __filename, it) {" +
"(function(require, module, exports, __dirname, __filename, it, beforeEach, afterEach) {" +
content +
"\n})",
p
@ -200,6 +210,8 @@ describe("ConfigTestCases", () => {
path.dirname(p),
p,
_it,
_beforeEach,
_afterEach,
globalContext
);
return m.exports;

View File

@ -0,0 +1,5 @@
export default function() {
import(/* webpackPrefetch: true, webpackChunkName: "chunk1-a" */ "./chunk1-a");
import(/* webpackPreload: true, webpackChunkName: "chunk1-b" */ "./chunk1-b");
import(/* webpackPrefetch: 10, webpackChunkName: "chunk1-c" */ "./chunk1-c");
}

View File

@ -0,0 +1,57 @@
const should = require("should");
const FakeDocument = require("../../../helpers/FakeDocument");
let oldNonce;
let oldPublicPath;
beforeEach(() => {
oldNonce = __webpack_nonce__;
oldPublicPath = __webpack_public_path__;
global.document = new FakeDocument();
});
afterEach(() => {
delete global.document;
__webpack_nonce__ = oldNonce;
__webpack_public_path__ = oldPublicPath;
})
it("should prefetch and preload child chunks on chunk load", () => {
__webpack_nonce__ = "nonce";
__webpack_public_path__ = "/public/path/";
const promise = import(/* webpackChunkName: "chunk1" */ "./chunk1");
document.head._children.length.should.be.eql(1);
const script = document.head._children[0];
script._type.should.be.eql("script");
should(script.src).be.eql("/public/path/chunk1.js")
should(script.getAttribute("nonce")).be.eql("nonce")
should(script.crossOrigin).be.eql("anonymous");
should(script.onload).be.type("function");
__non_webpack_require__("./chunk1.js");
script.onload();
return promise.then((ex) => {
document.head._children.length.should.be.eql(4);
let link = document.head._children[1];
link._type.should.be.eql("link");
should(link.rel).be.eql("preload");
should(link.as).be.eql("script");
should(link.href).be.eql("/public/path/chunk1-b.js");
should(link.charset).be.eql("utf-8");
should(link.getAttribute("nonce")).be.eql("nonce");
should(link.crossOrigin).be.eql("anonymous");
link = document.head._children[2];
link._type.should.be.eql("link");
should(link.rel).be.eql("prefetch");
should(link.href).be.eql("/public/path/chunk1-c.js");
link = document.head._children[3];
link._type.should.be.eql("link");
should(link.rel).be.eql("prefetch");
should(link.href).be.eql("/public/path/chunk1-a.js");
});
})

View File

@ -0,0 +1,13 @@
module.exports = {
target: "web",
output: {
chunkFilename: "[name].js",
crossOriginLoading: "anonymous"
},
performance: {
hints: false
},
optimization: {
minimize: false
}
};

View File

@ -0,0 +1,36 @@
module.exports = class FakeDocument {
constructor() {
this.head = this.createElement("head");
}
createElement(type) {
return new FakeElement(type);
}
getElementsByTagName(name) {
if (name === "head") return [this.head];
throw new Error(
`FakeDocument.getElementsByTagName(${name}): not implemented`
);
}
};
class FakeElement {
constructor(type) {
this._type = type;
this._children = [];
this._attributes = Object.create(null);
}
appendChild(node) {
this._children.push(node);
}
setAttribute(name, value) {
this._attributes[name] = value;
}
getAttribute(name) {
return this._attributes[name];
}
}