|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const path = require("path"); |
| 4 | +const fs = require("graceful-fs"); |
| 5 | +const webpack = require("webpack"); |
| 6 | +const { test } = require("@playwright/test"); |
| 7 | +const { expect } = require("@playwright/test"); |
| 8 | +const { describe } = require("@playwright/test"); |
| 9 | +const Server = require("../../lib/Server"); |
| 10 | +const HTMLGeneratorPlugin = require("../helpers/html-generator-plugin"); |
| 11 | +const config = require("../fixtures/client-config/webpack.config"); |
| 12 | +const port = require("../ports-map").logging; |
| 13 | + |
| 14 | +describe("logging", () => { |
| 15 | + const webSocketServers = [ |
| 16 | + { webSocketServer: "ws" }, |
| 17 | + { webSocketServer: "sockjs" }, |
| 18 | + ]; |
| 19 | + |
| 20 | + const cases = [ |
| 21 | + { |
| 22 | + title: "should work and log message about live reloading is enabled", |
| 23 | + devServerOptions: { |
| 24 | + hot: false, |
| 25 | + }, |
| 26 | + }, |
| 27 | + { |
| 28 | + title: "should work and log messages about hot", |
| 29 | + devServerOptions: { |
| 30 | + hot: true, |
| 31 | + }, |
| 32 | + }, |
| 33 | + { |
| 34 | + title: "should work and log messages about hot is enabled", |
| 35 | + devServerOptions: { |
| 36 | + liveReload: false, |
| 37 | + }, |
| 38 | + }, |
| 39 | + { |
| 40 | + title: |
| 41 | + "should work and do not log messages about hot and live reloading is enabled", |
| 42 | + devServerOptions: { |
| 43 | + liveReload: false, |
| 44 | + hot: false, |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + title: |
| 49 | + "should work and log messages about hot and live reloading is enabled", |
| 50 | + devServerOptions: { |
| 51 | + liveReload: true, |
| 52 | + hot: true, |
| 53 | + }, |
| 54 | + }, |
| 55 | + { |
| 56 | + title: "should work and log warnings by default", |
| 57 | + webpackOptions: { |
| 58 | + plugins: [ |
| 59 | + { |
| 60 | + apply(compiler) { |
| 61 | + compiler.hooks.thisCompilation.tap( |
| 62 | + "warnings-webpack-plugin", |
| 63 | + (compilation) => { |
| 64 | + compilation.warnings.push( |
| 65 | + new Error("Warning from compilation"), |
| 66 | + ); |
| 67 | + }, |
| 68 | + ); |
| 69 | + }, |
| 70 | + }, |
| 71 | + new HTMLGeneratorPlugin(), |
| 72 | + ], |
| 73 | + }, |
| 74 | + }, |
| 75 | + { |
| 76 | + title: "should work and log errors by default", |
| 77 | + webpackOptions: { |
| 78 | + plugins: [ |
| 79 | + { |
| 80 | + apply(compiler) { |
| 81 | + compiler.hooks.thisCompilation.tap( |
| 82 | + "warnings-webpack-plugin", |
| 83 | + (compilation) => { |
| 84 | + compilation.errors.push(new Error("Error from compilation")); |
| 85 | + }, |
| 86 | + ); |
| 87 | + }, |
| 88 | + }, |
| 89 | + new HTMLGeneratorPlugin(), |
| 90 | + ], |
| 91 | + }, |
| 92 | + }, |
| 93 | + { |
| 94 | + title: 'should work when the "client.logging" is "info"', |
| 95 | + devServerOptions: { |
| 96 | + client: { |
| 97 | + logging: "info", |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + { |
| 102 | + title: 'should work when the "client.logging" is "log"', |
| 103 | + devServerOptions: { |
| 104 | + client: { |
| 105 | + logging: "log", |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + { |
| 110 | + title: 'should work when the "client.logging" is "verbose"', |
| 111 | + devServerOptions: { |
| 112 | + client: { |
| 113 | + logging: "verbose", |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + { |
| 118 | + title: 'should work when the "client.logging" is "none"', |
| 119 | + devServerOptions: { |
| 120 | + client: { |
| 121 | + logging: "none", |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + { |
| 126 | + title: "should work and log only error", |
| 127 | + webpackOptions: { |
| 128 | + plugins: [ |
| 129 | + { |
| 130 | + apply(compiler) { |
| 131 | + compiler.hooks.thisCompilation.tap( |
| 132 | + "warnings-webpack-plugin", |
| 133 | + (compilation) => { |
| 134 | + compilation.warnings.push( |
| 135 | + new Error("Warning from compilation"), |
| 136 | + ); |
| 137 | + compilation.errors.push(new Error("Error from compilation")); |
| 138 | + }, |
| 139 | + ); |
| 140 | + }, |
| 141 | + }, |
| 142 | + new HTMLGeneratorPlugin(), |
| 143 | + ], |
| 144 | + }, |
| 145 | + devServerOptions: { |
| 146 | + client: { |
| 147 | + logging: "error", |
| 148 | + }, |
| 149 | + }, |
| 150 | + }, |
| 151 | + { |
| 152 | + title: "should work and log warning and errors", |
| 153 | + webpackOptions: { |
| 154 | + plugins: [ |
| 155 | + { |
| 156 | + apply(compiler) { |
| 157 | + compiler.hooks.thisCompilation.tap( |
| 158 | + "warnings-webpack-plugin", |
| 159 | + (compilation) => { |
| 160 | + compilation.warnings.push( |
| 161 | + new Error("Warning from compilation"), |
| 162 | + ); |
| 163 | + compilation.errors.push(new Error("Error from compilation")); |
| 164 | + }, |
| 165 | + ); |
| 166 | + }, |
| 167 | + }, |
| 168 | + new HTMLGeneratorPlugin(), |
| 169 | + ], |
| 170 | + }, |
| 171 | + devServerOptions: { |
| 172 | + client: { |
| 173 | + logging: "warn", |
| 174 | + }, |
| 175 | + }, |
| 176 | + }, |
| 177 | + { |
| 178 | + title: "should work and log static changes", |
| 179 | + devServerOptions: { |
| 180 | + static: path.resolve(__dirname, "../fixtures/client-config/static"), |
| 181 | + }, |
| 182 | + }, |
| 183 | + ]; |
| 184 | + |
| 185 | + webSocketServers.forEach((webSocketServer) => { |
| 186 | + cases.forEach((testCase) => { |
| 187 | + test(`${testCase.title} (${ |
| 188 | + webSocketServer.webSocketServer || "default" |
| 189 | + })`, async ({ page }) => { |
| 190 | + const compiler = webpack({ ...config, ...testCase.webpackOptions }); |
| 191 | + const devServerOptions = { |
| 192 | + port, |
| 193 | + ...testCase.devServerOptions, |
| 194 | + }; |
| 195 | + const server = new Server(devServerOptions, compiler); |
| 196 | + |
| 197 | + await server.start(); |
| 198 | + |
| 199 | + try { |
| 200 | + const consoleMessages = []; |
| 201 | + |
| 202 | + page.on("console", (message) => { |
| 203 | + consoleMessages.push(message); |
| 204 | + }); |
| 205 | + |
| 206 | + await page.goto(`http://localhost:${port}/`, { |
| 207 | + waitUntil: "networkidle0", |
| 208 | + }); |
| 209 | + |
| 210 | + if (testCase.devServerOptions && testCase.devServerOptions.static) { |
| 211 | + fs.writeFileSync( |
| 212 | + path.join(testCase.devServerOptions.static, "./foo.txt"), |
| 213 | + "Text", |
| 214 | + ); |
| 215 | + |
| 216 | + await page.waitForNavigation({ |
| 217 | + waitUntil: "networkidle0", |
| 218 | + }); |
| 219 | + } |
| 220 | + |
| 221 | + expect( |
| 222 | + JSON.stringify( |
| 223 | + consoleMessages.map((message) => |
| 224 | + message |
| 225 | + .text() |
| 226 | + .replace(/\\/g, "/") |
| 227 | + .replace( |
| 228 | + new RegExp(process.cwd().replace(/\\/g, "/"), "g"), |
| 229 | + "<cwd>", |
| 230 | + ), |
| 231 | + ), |
| 232 | + ), |
| 233 | + ).toMatchSnapshot(); |
| 234 | + } catch (error) { |
| 235 | + throw error; |
| 236 | + } finally { |
| 237 | + await server.stop(); |
| 238 | + } |
| 239 | + }); |
| 240 | + }); |
| 241 | + }); |
| 242 | +}); |
0 commit comments