|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const webpack = require("webpack"); |
| 4 | +const Server = require("../../lib/Server"); |
| 5 | +const config = require("../fixtures/client-config/webpack.config"); |
| 6 | +const compile = require("../helpers/compile"); |
| 7 | +const runBrowser = require("../helpers/run-browser"); |
| 8 | +const port = require("../ports-map")["api-plugin"]; |
| 9 | + |
| 10 | +describe("API (plugin)", () => { |
| 11 | + it("should work with plugin API", async () => { |
| 12 | + const compiler = webpack(config); |
| 13 | + const server = new Server({ port }); |
| 14 | + |
| 15 | + server.apply(compiler); |
| 16 | + |
| 17 | + await compile(compiler, port); |
| 18 | + |
| 19 | + const { page, browser } = await runBrowser(); |
| 20 | + |
| 21 | + const pageErrors = []; |
| 22 | + const consoleMessages = []; |
| 23 | + |
| 24 | + page |
| 25 | + .on("console", (message) => { |
| 26 | + consoleMessages.push(message); |
| 27 | + }) |
| 28 | + .on("pageerror", (error) => { |
| 29 | + pageErrors.push(error); |
| 30 | + }); |
| 31 | + |
| 32 | + await page.goto(`http://127.0.0.1:${port}/`, { |
| 33 | + waitUntil: "networkidle0", |
| 34 | + }); |
| 35 | + |
| 36 | + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( |
| 37 | + "console messages", |
| 38 | + ); |
| 39 | + expect(pageErrors).toMatchSnapshot("page errors"); |
| 40 | + |
| 41 | + await browser.close(); |
| 42 | + await new Promise((resolve) => { |
| 43 | + compiler.close(resolve); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + it("should not start the server multiple times on recompilation", async () => { |
| 48 | + const compiler = webpack(config); |
| 49 | + const server = new Server({ port }); |
| 50 | + const setupSpy = jest.spyOn(server, "setup"); |
| 51 | + const listenSpy = jest.spyOn(server, "listen"); |
| 52 | + |
| 53 | + server.apply(compiler); |
| 54 | + |
| 55 | + const { watching } = await compile(compiler, port); |
| 56 | + |
| 57 | + // Trigger a recompilation by invalidating |
| 58 | + await new Promise((resolve) => { |
| 59 | + watching.invalidate(() => { |
| 60 | + resolve(); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + // Wait for the recompilation to finish |
| 65 | + await new Promise((resolve) => { |
| 66 | + setTimeout(resolve, 2000); |
| 67 | + }); |
| 68 | + |
| 69 | + expect(setupSpy).toHaveBeenCalledTimes(1); |
| 70 | + expect(listenSpy).toHaveBeenCalledTimes(1); |
| 71 | + |
| 72 | + setupSpy.mockRestore(); |
| 73 | + listenSpy.mockRestore(); |
| 74 | + await new Promise((resolve) => { |
| 75 | + compiler.close(resolve); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it("should stop the server cleanly via compiler.close()", async () => { |
| 80 | + const compiler = webpack(config); |
| 81 | + const server = new Server({ port }); |
| 82 | + const stopSpy = jest.spyOn(server, "stop"); |
| 83 | + |
| 84 | + server.apply(compiler); |
| 85 | + |
| 86 | + await compile(compiler, port); |
| 87 | + |
| 88 | + await new Promise((resolve) => { |
| 89 | + compiler.close(resolve); |
| 90 | + }); |
| 91 | + |
| 92 | + expect(stopSpy).toHaveBeenCalledTimes(1); |
| 93 | + stopSpy.mockRestore(); |
| 94 | + }); |
| 95 | + |
| 96 | + describe("plugin in webpack config", () => { |
| 97 | + it("should work when added to webpack config plugins array", async () => { |
| 98 | + const server = new Server({ port }); |
| 99 | + const compiler = webpack({ |
| 100 | + ...config, |
| 101 | + plugins: [...config.plugins, server], |
| 102 | + }); |
| 103 | + |
| 104 | + await compile(compiler, port); |
| 105 | + |
| 106 | + const { page, browser } = await runBrowser(); |
| 107 | + |
| 108 | + try { |
| 109 | + const pageErrors = []; |
| 110 | + const consoleMessages = []; |
| 111 | + |
| 112 | + page |
| 113 | + .on("console", (message) => { |
| 114 | + consoleMessages.push(message); |
| 115 | + }) |
| 116 | + .on("pageerror", (error) => { |
| 117 | + pageErrors.push(error); |
| 118 | + }); |
| 119 | + |
| 120 | + await page.goto(`http://127.0.0.1:${port}/`, { |
| 121 | + waitUntil: "networkidle0", |
| 122 | + }); |
| 123 | + |
| 124 | + expect( |
| 125 | + consoleMessages.map((message) => message.text()), |
| 126 | + ).toMatchSnapshot("console messages"); |
| 127 | + expect(pageErrors).toMatchSnapshot("page errors"); |
| 128 | + } finally { |
| 129 | + await browser.close(); |
| 130 | + await new Promise((resolve) => { |
| 131 | + compiler.close(resolve); |
| 132 | + }); |
| 133 | + } |
| 134 | + }); |
| 135 | + |
| 136 | + it("should work with output.clean: true", async () => { |
| 137 | + const server = new Server({ port }); |
| 138 | + const compiler = webpack({ |
| 139 | + ...config, |
| 140 | + output: { |
| 141 | + ...config.output, |
| 142 | + clean: true, |
| 143 | + }, |
| 144 | + plugins: [...config.plugins, server], |
| 145 | + }); |
| 146 | + |
| 147 | + await compile(compiler, port); |
| 148 | + |
| 149 | + const { page, browser } = await runBrowser(); |
| 150 | + |
| 151 | + try { |
| 152 | + const pageErrors = []; |
| 153 | + const consoleMessages = []; |
| 154 | + |
| 155 | + page |
| 156 | + .on("console", (message) => { |
| 157 | + consoleMessages.push(message); |
| 158 | + }) |
| 159 | + .on("pageerror", (error) => { |
| 160 | + pageErrors.push(error); |
| 161 | + }); |
| 162 | + |
| 163 | + const response = await page.goto(`http://127.0.0.1:${port}/`, { |
| 164 | + waitUntil: "networkidle0", |
| 165 | + }); |
| 166 | + |
| 167 | + expect(response.status()).toBe(200); |
| 168 | + expect( |
| 169 | + consoleMessages.map((message) => message.text()), |
| 170 | + ).toMatchSnapshot("console messages"); |
| 171 | + expect(pageErrors).toMatchSnapshot("page errors"); |
| 172 | + } finally { |
| 173 | + await browser.close(); |
| 174 | + await new Promise((resolve) => { |
| 175 | + compiler.close(resolve); |
| 176 | + }); |
| 177 | + } |
| 178 | + }); |
| 179 | + }); |
| 180 | +}); |
0 commit comments