Skip to content

Commit ee42520

Browse files
committed
FakeDocument: fix hanging rejection in tests from unimplemented parts
1 parent 2be0965 commit ee42520

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

‎test/configCases/split-chunks/runtime-chunk/a.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ const FakeDocument = require("../../../helpers/FakeDocument");
22

33
beforeEach(() => {
44
global.document = new FakeDocument();
5+
global.window = {};
56
});
67

78
afterEach(() => {
89
delete global.document;
10+
delete global.window;
911
});
1012

1113
it("should be able to load the split chunk on demand", () => {

‎test/configCases/web/prefetch-preload/index.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ beforeEach(() => {
77
oldNonce = __webpack_nonce__;
88
oldPublicPath = __webpack_public_path__;
99
global.document = new FakeDocument();
10+
global.window = {};
1011
});
1112

1213
afterEach(() => {
1314
delete global.document;
15+
delete global.window;
1416
__webpack_nonce__ = oldNonce;
1517
__webpack_public_path__ = oldPublicPath;
1618
})
@@ -26,7 +28,7 @@ it("should prefetch and preload child chunks on chunk load", () => {
2628
expect(script.src).toBe("/public/path/chunk1.js")
2729
expect(script.getAttribute("nonce")).toBe("nonce")
2830
expect(script.crossOrigin).toBe("anonymous");
29-
expect(script.onload).toBe("function");
31+
expect(script.onload).toBeTypeOf("function");
3032

3133
let link = document.head._children[1];
3234
expect(link._type).toBe("link");

‎test/helpers/FakeDocument.js‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,33 @@ class FakeElement {
3333
getAttribute(name) {
3434
return this._attributes[name];
3535
}
36+
37+
get onload() {
38+
return this._onload;
39+
}
40+
41+
set onload(script) {
42+
if (typeof script === "function") {
43+
script();
44+
}
45+
this._onload = script;
46+
}
47+
48+
get src() {
49+
return this._src;
50+
}
51+
52+
set src(src) {
53+
// eslint-disable-next-line no-undef
54+
const publicPath = __webpack_public_path__;
55+
eval(`
56+
const path = require('path');
57+
const fs = require('fs');
58+
const content = fs.readFileSync(
59+
path.join(__dirname, '${src}'.replace('${publicPath}', '')), "utf-8"
60+
)
61+
eval(content);
62+
`);
63+
this._src = src;
64+
}
3665
}

0 commit comments

Comments
 (0)