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