|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const webpack = require("webpack"); |
| 4 | +const { test } = require("@playwright/test"); |
| 5 | +const { expect } = require("@playwright/test"); |
| 6 | +const { describe } = require("@playwright/test"); |
| 7 | +const { afterEach } = require("@playwright/test"); |
| 8 | +const { beforeEach } = require("@playwright/test"); |
| 9 | +const Server = require("../../lib/Server"); |
| 10 | +const config = require("../fixtures/simple-config-other/webpack.config"); |
| 11 | +const port = require("../ports-map")["client-option"]; |
| 12 | + |
| 13 | +describe("client option", () => { |
| 14 | + describe("default behaviour", () => { |
| 15 | + let compiler; |
| 16 | + let server; |
| 17 | + let pageErrors; |
| 18 | + let consoleMessages; |
| 19 | + |
| 20 | + beforeEach(async () => { |
| 21 | + compiler = webpack(config); |
| 22 | + |
| 23 | + server = new Server( |
| 24 | + { |
| 25 | + client: { |
| 26 | + webSocketTransport: "sockjs", |
| 27 | + }, |
| 28 | + webSocketServer: "sockjs", |
| 29 | + port, |
| 30 | + }, |
| 31 | + compiler, |
| 32 | + ); |
| 33 | + |
| 34 | + await server.start(); |
| 35 | + |
| 36 | + pageErrors = []; |
| 37 | + consoleMessages = []; |
| 38 | + }); |
| 39 | + |
| 40 | + afterEach(async () => { |
| 41 | + await server.stop(); |
| 42 | + }); |
| 43 | + |
| 44 | + test("responds with a 200 status code for /ws path", async ({ page }) => { |
| 45 | + page |
| 46 | + .on("console", (message) => { |
| 47 | + consoleMessages.push(message); |
| 48 | + }) |
| 49 | + .on("pageerror", (error) => { |
| 50 | + pageErrors.push(error); |
| 51 | + }); |
| 52 | + |
| 53 | + const response = await page.goto(`http://127.0.0.1:${port}/ws`, { |
| 54 | + waitUntil: "networkidle0", |
| 55 | + }); |
| 56 | + |
| 57 | + // overlay should be true by default |
| 58 | + expect(server.options.client.overlay).toBe(true); |
| 59 | + |
| 60 | + expect(JSON.stringify(response.status())).toMatchSnapshot(); |
| 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 respect path option", () => { |
| 71 | + let compiler; |
| 72 | + let server; |
| 73 | + let pageErrors; |
| 74 | + let consoleMessages; |
| 75 | + |
| 76 | + beforeEach(async () => { |
| 77 | + compiler = webpack(config); |
| 78 | + |
| 79 | + server = new Server( |
| 80 | + { |
| 81 | + client: { |
| 82 | + webSocketTransport: "sockjs", |
| 83 | + }, |
| 84 | + webSocketServer: { |
| 85 | + type: "sockjs", |
| 86 | + options: { |
| 87 | + host: "localhost", |
| 88 | + port, |
| 89 | + path: "/foo/test/bar", |
| 90 | + }, |
| 91 | + }, |
| 92 | + port, |
| 93 | + }, |
| 94 | + compiler, |
| 95 | + ); |
| 96 | + |
| 97 | + await server.start(); |
| 98 | + |
| 99 | + pageErrors = []; |
| 100 | + consoleMessages = []; |
| 101 | + }); |
| 102 | + |
| 103 | + afterEach(async () => { |
| 104 | + await server.stop(); |
| 105 | + }); |
| 106 | + |
| 107 | + test("responds with a 200 status code for /foo/test/bar path", async ({ |
| 108 | + page, |
| 109 | + }) => { |
| 110 | + page |
| 111 | + .on("console", (message) => { |
| 112 | + consoleMessages.push(message); |
| 113 | + }) |
| 114 | + .on("pageerror", (error) => { |
| 115 | + pageErrors.push(error); |
| 116 | + }); |
| 117 | + |
| 118 | + const response = await page.goto( |
| 119 | + `http://127.0.0.1:${port}/foo/test/bar`, |
| 120 | + { |
| 121 | + waitUntil: "networkidle0", |
| 122 | + }, |
| 123 | + ); |
| 124 | + |
| 125 | + expect(JSON.stringify(response.status())).toMatchSnapshot(); |
| 126 | + |
| 127 | + expect( |
| 128 | + JSON.stringify(consoleMessages.map((message) => message.text())), |
| 129 | + ).toMatchSnapshot(); |
| 130 | + |
| 131 | + expect(JSON.stringify(pageErrors)).toMatchSnapshot(); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + describe("configure client entry", () => { |
| 136 | + let compiler; |
| 137 | + let server; |
| 138 | + let pageErrors; |
| 139 | + let consoleMessages; |
| 140 | + |
| 141 | + beforeEach(async () => { |
| 142 | + compiler = webpack(config); |
| 143 | + |
| 144 | + server = new Server( |
| 145 | + { |
| 146 | + client: false, |
| 147 | + port, |
| 148 | + }, |
| 149 | + compiler, |
| 150 | + ); |
| 151 | + |
| 152 | + await server.start(); |
| 153 | + |
| 154 | + pageErrors = []; |
| 155 | + consoleMessages = []; |
| 156 | + }); |
| 157 | + |
| 158 | + afterEach(async () => { |
| 159 | + await server.stop(); |
| 160 | + }); |
| 161 | + |
| 162 | + test("should disable client entry", async ({ page }) => { |
| 163 | + page |
| 164 | + .on("console", (message) => { |
| 165 | + consoleMessages.push(message); |
| 166 | + }) |
| 167 | + .on("pageerror", (error) => { |
| 168 | + pageErrors.push(error); |
| 169 | + }); |
| 170 | + |
| 171 | + const response = await page.goto(`http://127.0.0.1:${port}/main.js`, { |
| 172 | + waitUntil: "networkidle0", |
| 173 | + }); |
| 174 | + |
| 175 | + expect(JSON.stringify(response.status())).toMatchSnapshot(); |
| 176 | + |
| 177 | + expect(await response.text()).not.toMatch(/client\/index\.js/); |
| 178 | + |
| 179 | + expect( |
| 180 | + JSON.stringify(consoleMessages.map((message) => message.text())), |
| 181 | + ).toMatchSnapshot(); |
| 182 | + |
| 183 | + expect(JSON.stringify(pageErrors)).toMatchSnapshot(); |
| 184 | + }); |
| 185 | + }); |
| 186 | + |
| 187 | + describe("webSocketTransport", () => { |
| 188 | + const clientModes = [ |
| 189 | + { |
| 190 | + title: 'as a string ("sockjs")', |
| 191 | + client: { |
| 192 | + webSocketTransport: "sockjs", |
| 193 | + }, |
| 194 | + webSocketServer: "sockjs", |
| 195 | + shouldThrow: false, |
| 196 | + }, |
| 197 | + { |
| 198 | + title: 'as a string ("ws")', |
| 199 | + client: { |
| 200 | + webSocketTransport: "ws", |
| 201 | + }, |
| 202 | + webSocketServer: "ws", |
| 203 | + shouldThrow: false, |
| 204 | + }, |
| 205 | + { |
| 206 | + title: 'as a path ("sockjs")', |
| 207 | + client: { |
| 208 | + webSocketTransport: require.resolve( |
| 209 | + "../../client-src/clients/SockJSClient", |
| 210 | + ), |
| 211 | + }, |
| 212 | + webSocketServer: "sockjs", |
| 213 | + shouldThrow: false, |
| 214 | + }, |
| 215 | + { |
| 216 | + title: 'as a path ("ws")', |
| 217 | + client: { |
| 218 | + webSocketTransport: require.resolve( |
| 219 | + "../../client-src/clients/WebSocketClient", |
| 220 | + ), |
| 221 | + }, |
| 222 | + webSocketServer: "ws", |
| 223 | + shouldThrow: false, |
| 224 | + }, |
| 225 | + { |
| 226 | + title: "as a nonexistent path (sockjs)", |
| 227 | + client: { |
| 228 | + webSocketTransport: "/bad/path/to/implementation", |
| 229 | + }, |
| 230 | + webSocketServer: "sockjs", |
| 231 | + shouldThrow: true, |
| 232 | + }, |
| 233 | + { |
| 234 | + title: "as a nonexistent path (ws)", |
| 235 | + client: { |
| 236 | + webSocketTransport: "/bad/path/to/implementation", |
| 237 | + }, |
| 238 | + webSocketServer: "ws", |
| 239 | + shouldThrow: true, |
| 240 | + }, |
| 241 | + ]; |
| 242 | + |
| 243 | + describe("passed to server", () => { |
| 244 | + clientModes.forEach((data) => { |
| 245 | + test(`${data.title} ${ |
| 246 | + data.shouldThrow ? "should throw" : "should not throw" |
| 247 | + }`, async () => { |
| 248 | + const compiler = webpack(config); |
| 249 | + |
| 250 | + const server = new Server( |
| 251 | + { |
| 252 | + client: data.client, |
| 253 | + port, |
| 254 | + }, |
| 255 | + compiler, |
| 256 | + ); |
| 257 | + |
| 258 | + let thrownError; |
| 259 | + |
| 260 | + try { |
| 261 | + await server.start(); |
| 262 | + } catch (error) { |
| 263 | + thrownError = error; |
| 264 | + } |
| 265 | + |
| 266 | + if (data.shouldThrow) { |
| 267 | + expect(thrownError.message).toMatch( |
| 268 | + /client\.webSocketTransport must be a string/, |
| 269 | + ); |
| 270 | + } |
| 271 | + |
| 272 | + await server.stop(); |
| 273 | + }); |
| 274 | + }); |
| 275 | + }); |
| 276 | + }); |
| 277 | +}); |
0 commit comments