Skip to content

Commit 0fba08c

Browse files
committed
test: add e2e tests for module federation
1 parent ae8c523 commit 0fba08c

File tree

4 files changed

+362
-104
lines changed

4 files changed

+362
-104
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Module federation should work with multi compiler config should use the last entry export: console messages 1`] = `Array []`;
4+
5+
exports[`Module federation should work with multi compiler config should use the last entry export: page errors 1`] = `Array []`;
6+
7+
exports[`Module federation should work with object multi-entry config should support the named entry export: console messages 1`] = `Array []`;
8+
9+
exports[`Module federation should work with object multi-entry config should support the named entry export: page errors 1`] = `Array []`;
10+
11+
exports[`Module federation should work with object multi-entry config should use the last entry export: console messages 1`] = `Array []`;
12+
13+
exports[`Module federation should work with object multi-entry config should use the last entry export: page errors 1`] = `Array []`;
14+
15+
exports[`Module federation should work with simple multi-entry config should use the last entry export: console messages 1`] = `Array []`;
16+
17+
exports[`Module federation should work with simple multi-entry config should use the last entry export: page errors 1`] = `Array []`;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Module federation should use plugin should contain hot script in main.js: console messages 1`] = `Array []`;
4+
5+
exports[`Module federation should use plugin should contain hot script in main.js: page errors 1`] = `Array []`;
6+
7+
exports[`Module federation should use plugin should contain hot script in remoteEntry.js: console messages 1`] = `Array []`;
8+
9+
exports[`Module federation should use plugin should contain hot script in remoteEntry.js: page errors 1`] = `Array []`;
10+
11+
exports[`Module federation should work with multi compiler config should use the last entry export: console messages 1`] = `Array []`;
12+
13+
exports[`Module federation should work with multi compiler config should use the last entry export: page errors 1`] = `Array []`;
14+
15+
exports[`Module federation should work with object multi-entry config should support the named entry export: console messages 1`] = `Array []`;
16+
17+
exports[`Module federation should work with object multi-entry config should support the named entry export: page errors 1`] = `Array []`;
18+
19+
exports[`Module federation should work with object multi-entry config should use the last entry export: console messages 1`] = `Array []`;
20+
21+
exports[`Module federation should work with object multi-entry config should use the last entry export: page errors 1`] = `Array []`;
22+
23+
exports[`Module federation should work with simple multi-entry config should use the last entry export: console messages 1`] = `Array []`;
24+
25+
exports[`Module federation should work with simple multi-entry config should use the last entry export: page errors 1`] = `Array []`;

test/e2e/module-federation.test.js

Lines changed: 320 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,320 @@
1+
"use strict";
2+
3+
const webpack = require("webpack");
4+
const requireFromString = require("require-from-string");
5+
const Server = require("../../lib/Server");
6+
const simpleConfig = require("../fixtures/module-federation-config/webpack.config");
7+
const objectEntryConfig = require("../fixtures/module-federation-config/webpack.object-entry.config");
8+
const multiConfig = require("../fixtures/module-federation-config/webpack.multi.config");
9+
const pluginConfig = require("../fixtures/module-federation-config/webpack.plugin");
10+
const runBrowser = require("../helpers/run-browser");
11+
const isWebpack5 = require("../helpers/isWebpack5");
12+
const port = require("../ports-map")["universal-compiler"];
13+
14+
const describeOnlyWebpack5 = isWebpack5 ? describe : describe.skip;
15+
16+
describe("Module federation", () => {
17+
describe("should work with simple multi-entry config", () => {
18+
let compiler;
19+
let server;
20+
let page;
21+
let browser;
22+
let pageErrors;
23+
let consoleMessages;
24+
25+
beforeEach(async () => {
26+
compiler = webpack(simpleConfig);
27+
server = new Server({ port }, compiler);
28+
29+
await server.start();
30+
31+
({ page, browser } = await runBrowser());
32+
33+
pageErrors = [];
34+
consoleMessages = [];
35+
});
36+
37+
afterEach(async () => {
38+
await browser.close();
39+
await server.stop();
40+
});
41+
42+
it("should use the last entry export", async () => {
43+
page
44+
.on("console", (message) => {
45+
consoleMessages.push(message);
46+
})
47+
.on("pageerror", (error) => {
48+
pageErrors.push(error);
49+
});
50+
51+
await page.goto(`http://127.0.0.1:${port}/main.js`, {
52+
waitUntil: "networkidle0",
53+
});
54+
55+
const bodyHandle = await page.$("body");
56+
const textContent = await page.evaluate(
57+
(body) => body.textContent,
58+
bodyHandle
59+
);
60+
61+
expect(textContent).toContain("entry1");
62+
63+
let exports;
64+
65+
expect(() => {
66+
exports = requireFromString(textContent);
67+
}).not.toThrow();
68+
69+
expect(exports).toEqual("entry2");
70+
71+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
72+
"console messages"
73+
);
74+
75+
expect(pageErrors).toMatchSnapshot("page errors");
76+
});
77+
});
78+
79+
describe("should work with object multi-entry config", () => {
80+
let compiler;
81+
let server;
82+
let page;
83+
let browser;
84+
let pageErrors;
85+
let consoleMessages;
86+
87+
beforeEach(async () => {
88+
compiler = webpack(objectEntryConfig);
89+
server = new Server({ port }, compiler);
90+
91+
await server.start();
92+
93+
({ page, browser } = await runBrowser());
94+
95+
pageErrors = [];
96+
consoleMessages = [];
97+
});
98+
99+
afterEach(async () => {
100+
await browser.close();
101+
await server.stop();
102+
});
103+
104+
it("should use the last entry export", async () => {
105+
page
106+
.on("console", (message) => {
107+
consoleMessages.push(message);
108+
})
109+
.on("pageerror", (error) => {
110+
pageErrors.push(error);
111+
});
112+
113+
await page.goto(`http://127.0.0.1:${port}/main.js`, {
114+
waitUntil: "networkidle0",
115+
});
116+
117+
const bodyHandle = await page.$("body");
118+
const textContent = await page.evaluate(
119+
(body) => body.textContent,
120+
bodyHandle
121+
);
122+
123+
expect(textContent).toContain("entry1");
124+
125+
let exports;
126+
127+
expect(() => {
128+
exports = requireFromString(textContent);
129+
}).not.toThrow();
130+
131+
expect(exports).toEqual("entry2");
132+
133+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
134+
"console messages"
135+
);
136+
137+
expect(pageErrors).toMatchSnapshot("page errors");
138+
});
139+
140+
it("should support the named entry export", async () => {
141+
page
142+
.on("console", (message) => {
143+
consoleMessages.push(message);
144+
})
145+
.on("pageerror", (error) => {
146+
pageErrors.push(error);
147+
});
148+
149+
await page.goto(`http://127.0.0.1:${port}/foo.js`, {
150+
waitUntil: "networkidle0",
151+
});
152+
153+
const bodyHandle = await page.$("body");
154+
const textContent = await page.evaluate(
155+
(body) => body.textContent,
156+
bodyHandle
157+
);
158+
159+
expect(textContent).not.toContain("entry2");
160+
161+
let exports;
162+
163+
expect(() => {
164+
exports = requireFromString(textContent);
165+
}).not.toThrow();
166+
167+
expect(exports).toEqual("entry1");
168+
169+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
170+
"console messages"
171+
);
172+
173+
expect(pageErrors).toMatchSnapshot("page errors");
174+
});
175+
});
176+
177+
describe("should work with multi compiler config", () => {
178+
let compiler;
179+
let server;
180+
let page;
181+
let browser;
182+
let pageErrors;
183+
let consoleMessages;
184+
185+
beforeEach(async () => {
186+
compiler = webpack(multiConfig);
187+
server = new Server({ port }, compiler);
188+
189+
await server.start();
190+
191+
({ page, browser } = await runBrowser());
192+
193+
pageErrors = [];
194+
consoleMessages = [];
195+
});
196+
197+
afterEach(async () => {
198+
await browser.close();
199+
await server.stop();
200+
});
201+
202+
it("should use the last entry export", async () => {
203+
page
204+
.on("console", (message) => {
205+
consoleMessages.push(message);
206+
})
207+
.on("pageerror", (error) => {
208+
pageErrors.push(error);
209+
});
210+
211+
await page.goto(`http://127.0.0.1:${port}/main.js`, {
212+
waitUntil: "networkidle0",
213+
});
214+
215+
const bodyHandle = await page.$("body");
216+
const textContent = await page.evaluate(
217+
(body) => body.textContent,
218+
bodyHandle
219+
);
220+
221+
expect(textContent).toContain("entry1");
222+
223+
let exports;
224+
225+
expect(() => {
226+
exports = requireFromString(textContent);
227+
}).not.toThrow();
228+
229+
expect(exports).toEqual("entry2");
230+
231+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
232+
"console messages"
233+
);
234+
235+
expect(pageErrors).toMatchSnapshot("page errors");
236+
});
237+
});
238+
239+
describeOnlyWebpack5("should use plugin", () => {
240+
let compiler;
241+
let server;
242+
let page;
243+
let browser;
244+
let pageErrors;
245+
let consoleMessages;
246+
247+
beforeEach(async () => {
248+
compiler = webpack(pluginConfig);
249+
server = new Server({ port }, compiler);
250+
251+
await server.start();
252+
253+
({ page, browser } = await runBrowser());
254+
255+
pageErrors = [];
256+
consoleMessages = [];
257+
});
258+
259+
afterEach(async () => {
260+
await browser.close();
261+
await server.stop();
262+
});
263+
264+
it("should contain hot script in remoteEntry.js", async () => {
265+
page
266+
.on("console", (message) => {
267+
consoleMessages.push(message);
268+
})
269+
.on("pageerror", (error) => {
270+
pageErrors.push(error);
271+
});
272+
273+
await page.goto(`http://127.0.0.1:${port}/remoteEntry.js`, {
274+
waitUntil: "networkidle0",
275+
});
276+
277+
const bodyHandle = await page.$("body");
278+
const remoteEntryTextContent = await page.evaluate(
279+
(body) => body.textContent,
280+
bodyHandle
281+
);
282+
283+
expect(remoteEntryTextContent).toMatch(/webpack\/hot\/dev-server\.js/);
284+
285+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
286+
"console messages"
287+
);
288+
289+
expect(pageErrors).toMatchSnapshot("page errors");
290+
});
291+
292+
it("should contain hot script in main.js", async () => {
293+
page
294+
.on("console", (message) => {
295+
consoleMessages.push(message);
296+
})
297+
.on("pageerror", (error) => {
298+
pageErrors.push(error);
299+
});
300+
301+
await page.goto(`http://127.0.0.1:${port}/main.js`, {
302+
waitUntil: "networkidle0",
303+
});
304+
305+
const bodyHandle = await page.$("body");
306+
const mainEntryTextContent = await page.evaluate(
307+
(body) => body.textContent,
308+
bodyHandle
309+
);
310+
311+
expect(mainEntryTextContent).toMatch(/webpack\/hot\/dev-server\.js/);
312+
313+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
314+
"console messages"
315+
);
316+
317+
expect(pageErrors).toMatchSnapshot("page errors");
318+
});
319+
});
320+
});

0 commit comments

Comments
 (0)