Skip to content

Commit

Permalink
test: add e2e tests for module federation (#3658)
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 authored Aug 17, 2021
1 parent 493ccbd commit 75bafbf
Show file tree
Hide file tree
Showing 4 changed files with 367 additions and 104 deletions.
17 changes: 17 additions & 0 deletions test/e2e/__snapshots__/module-federation.test.js.snap.webpack4
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Module federation should work with multi compiler config should use the last entry export: console messages 1`] = `Array []`;

exports[`Module federation should work with multi compiler config should use the last entry export: page errors 1`] = `Array []`;

exports[`Module federation should work with object multi-entry config should support the named entry export: console messages 1`] = `Array []`;

exports[`Module federation should work with object multi-entry config should support the named entry export: page errors 1`] = `Array []`;

exports[`Module federation should work with object multi-entry config should use the last entry export: console messages 1`] = `Array []`;

exports[`Module federation should work with object multi-entry config should use the last entry export: page errors 1`] = `Array []`;

exports[`Module federation should work with simple multi-entry config should use the last entry export: console messages 1`] = `Array []`;

exports[`Module federation should work with simple multi-entry config should use the last entry export: page errors 1`] = `Array []`;
25 changes: 25 additions & 0 deletions test/e2e/__snapshots__/module-federation.test.js.snap.webpack5
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Module federation should use plugin should contain hot script in main.js: console messages 1`] = `Array []`;

exports[`Module federation should use plugin should contain hot script in main.js: page errors 1`] = `Array []`;

exports[`Module federation should use plugin should contain hot script in remoteEntry.js: console messages 1`] = `Array []`;

exports[`Module federation should use plugin should contain hot script in remoteEntry.js: page errors 1`] = `Array []`;

exports[`Module federation should work with multi compiler config should use the last entry export: console messages 1`] = `Array []`;

exports[`Module federation should work with multi compiler config should use the last entry export: page errors 1`] = `Array []`;

exports[`Module federation should work with object multi-entry config should support the named entry export: console messages 1`] = `Array []`;

exports[`Module federation should work with object multi-entry config should support the named entry export: page errors 1`] = `Array []`;

exports[`Module federation should work with object multi-entry config should use the last entry export: console messages 1`] = `Array []`;

exports[`Module federation should work with object multi-entry config should use the last entry export: page errors 1`] = `Array []`;

exports[`Module federation should work with simple multi-entry config should use the last entry export: console messages 1`] = `Array []`;

exports[`Module federation should work with simple multi-entry config should use the last entry export: page errors 1`] = `Array []`;
325 changes: 325 additions & 0 deletions test/e2e/module-federation.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,325 @@
"use strict";

const webpack = require("webpack");
const requireFromString = require("require-from-string");
const Server = require("../../lib/Server");
const simpleConfig = require("../fixtures/module-federation-config/webpack.config");
const objectEntryConfig = require("../fixtures/module-federation-config/webpack.object-entry.config");
const multiConfig = require("../fixtures/module-federation-config/webpack.multi.config");
const runBrowser = require("../helpers/run-browser");
const isWebpack5 = require("../helpers/isWebpack5");
const port = require("../ports-map")["universal-compiler"];

const describeOnlyWebpack5 = isWebpack5 ? describe : describe.skip;

let pluginConfig;

if (isWebpack5) {
pluginConfig = require("../fixtures/module-federation-config/webpack.plugin");
}

describe("Module federation", () => {
describe("should work with simple multi-entry config", () => {
let compiler;
let server;
let page;
let browser;
let pageErrors;
let consoleMessages;

beforeEach(async () => {
compiler = webpack(simpleConfig);
server = new Server({ port }, compiler);

await server.start();

({ page, browser } = await runBrowser());

pageErrors = [];
consoleMessages = [];
});

afterEach(async () => {
await browser.close();
await server.stop();
});

it("should use the last entry export", async () => {
page
.on("console", (message) => {
consoleMessages.push(message);
})
.on("pageerror", (error) => {
pageErrors.push(error);
});

await page.goto(`http://127.0.0.1:${port}/main.js`, {
waitUntil: "networkidle0",
});

const bodyHandle = await page.$("body");
const textContent = await page.evaluate(
(body) => body.textContent,
bodyHandle
);

expect(textContent).toContain("entry1");

let exports;

expect(() => {
exports = requireFromString(textContent);
}).not.toThrow();

expect(exports).toEqual("entry2");

expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
"console messages"
);

expect(pageErrors).toMatchSnapshot("page errors");
});
});

describe("should work with object multi-entry config", () => {
let compiler;
let server;
let page;
let browser;
let pageErrors;
let consoleMessages;

beforeEach(async () => {
compiler = webpack(objectEntryConfig);
server = new Server({ port }, compiler);

await server.start();

({ page, browser } = await runBrowser());

pageErrors = [];
consoleMessages = [];
});

afterEach(async () => {
await browser.close();
await server.stop();
});

it("should use the last entry export", async () => {
page
.on("console", (message) => {
consoleMessages.push(message);
})
.on("pageerror", (error) => {
pageErrors.push(error);
});

await page.goto(`http://127.0.0.1:${port}/main.js`, {
waitUntil: "networkidle0",
});

const bodyHandle = await page.$("body");
const textContent = await page.evaluate(
(body) => body.textContent,
bodyHandle
);

expect(textContent).toContain("entry1");

let exports;

expect(() => {
exports = requireFromString(textContent);
}).not.toThrow();

expect(exports).toEqual("entry2");

expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
"console messages"
);

expect(pageErrors).toMatchSnapshot("page errors");
});

it("should support the named entry export", async () => {
page
.on("console", (message) => {
consoleMessages.push(message);
})
.on("pageerror", (error) => {
pageErrors.push(error);
});

await page.goto(`http://127.0.0.1:${port}/foo.js`, {
waitUntil: "networkidle0",
});

const bodyHandle = await page.$("body");
const textContent = await page.evaluate(
(body) => body.textContent,
bodyHandle
);

expect(textContent).not.toContain("entry2");

let exports;

expect(() => {
exports = requireFromString(textContent);
}).not.toThrow();

expect(exports).toEqual("entry1");

expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
"console messages"
);

expect(pageErrors).toMatchSnapshot("page errors");
});
});

describe("should work with multi compiler config", () => {
let compiler;
let server;
let page;
let browser;
let pageErrors;
let consoleMessages;

beforeEach(async () => {
compiler = webpack(multiConfig);
server = new Server({ port }, compiler);

await server.start();

({ page, browser } = await runBrowser());

pageErrors = [];
consoleMessages = [];
});

afterEach(async () => {
await browser.close();
await server.stop();
});

it("should use the last entry export", async () => {
page
.on("console", (message) => {
consoleMessages.push(message);
})
.on("pageerror", (error) => {
pageErrors.push(error);
});

await page.goto(`http://127.0.0.1:${port}/main.js`, {
waitUntil: "networkidle0",
});

const bodyHandle = await page.$("body");
const textContent = await page.evaluate(
(body) => body.textContent,
bodyHandle
);

expect(textContent).toContain("entry1");

let exports;

expect(() => {
exports = requireFromString(textContent);
}).not.toThrow();

expect(exports).toEqual("entry2");

expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
"console messages"
);

expect(pageErrors).toMatchSnapshot("page errors");
});
});

describeOnlyWebpack5("should use plugin", () => {
let compiler;
let server;
let page;
let browser;
let pageErrors;
let consoleMessages;

beforeEach(async () => {
compiler = webpack(pluginConfig);
server = new Server({ port }, compiler);

await server.start();

({ page, browser } = await runBrowser());

pageErrors = [];
consoleMessages = [];
});

afterEach(async () => {
await browser.close();
await server.stop();
});

it("should contain hot script in remoteEntry.js", async () => {
page
.on("console", (message) => {
consoleMessages.push(message);
})
.on("pageerror", (error) => {
pageErrors.push(error);
});

await page.goto(`http://127.0.0.1:${port}/remoteEntry.js`, {
waitUntil: "networkidle0",
});

const bodyHandle = await page.$("body");
const remoteEntryTextContent = await page.evaluate(
(body) => body.textContent,
bodyHandle
);

expect(remoteEntryTextContent).toMatch(/webpack\/hot\/dev-server\.js/);

expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
"console messages"
);

expect(pageErrors).toMatchSnapshot("page errors");
});

it("should contain hot script in main.js", async () => {
page
.on("console", (message) => {
consoleMessages.push(message);
})
.on("pageerror", (error) => {
pageErrors.push(error);
});

await page.goto(`http://127.0.0.1:${port}/main.js`, {
waitUntil: "networkidle0",
});

const bodyHandle = await page.$("body");
const mainEntryTextContent = await page.evaluate(
(body) => body.textContent,
bodyHandle
);

expect(mainEntryTextContent).toMatch(/webpack\/hot\/dev-server\.js/);

expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
"console messages"
);

expect(pageErrors).toMatchSnapshot("page errors");
});
});
});
Loading

0 comments on commit 75bafbf

Please sign in to comment.