|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const path = require("path"); |
| 4 | +const webpack = require("webpack"); |
| 5 | +const { test } = require("@playwright/test"); |
| 6 | +const { expect } = require("@playwright/test"); |
| 7 | +const { describe } = require("@playwright/test"); |
| 8 | +const { afterEach } = require("@playwright/test"); |
| 9 | +const { beforeEach } = require("@playwright/test"); |
| 10 | +const Server = require("../../lib/Server"); |
| 11 | +const config = require("../fixtures/client-config/webpack.config"); |
| 12 | +const port = require("../ports-map").app; |
| 13 | + |
| 14 | +const staticDirectory = path.resolve( |
| 15 | + __dirname, |
| 16 | + "../fixtures/static-config/public", |
| 17 | +); |
| 18 | + |
| 19 | +const apps = [ |
| 20 | + ["express", () => require("express")()], |
| 21 | + ["connect", () => require("connect")()], |
| 22 | + ["connect (async)", async () => require("express")()], |
| 23 | +]; |
| 24 | + |
| 25 | +const servers = ["http", "https", "spdy"]; |
| 26 | + |
| 27 | +describe("app option", () => { |
| 28 | + for (const [appName, app] of apps) { |
| 29 | + for (const server of servers) { |
| 30 | + let compiler; |
| 31 | + let devServer; |
| 32 | + let pageErrors; |
| 33 | + let consoleMessages; |
| 34 | + |
| 35 | + describe(`should work using "${appName}" application and "${server}" server`, () => { |
| 36 | + beforeEach(async () => { |
| 37 | + compiler = webpack(config); |
| 38 | + |
| 39 | + devServer = new Server( |
| 40 | + { |
| 41 | + static: { |
| 42 | + directory: staticDirectory, |
| 43 | + watch: false, |
| 44 | + }, |
| 45 | + app, |
| 46 | + server, |
| 47 | + port, |
| 48 | + }, |
| 49 | + compiler, |
| 50 | + ); |
| 51 | + |
| 52 | + await devServer.start(); |
| 53 | + |
| 54 | + pageErrors = []; |
| 55 | + consoleMessages = []; |
| 56 | + }); |
| 57 | + |
| 58 | + afterEach(async () => { |
| 59 | + await devServer.stop(); |
| 60 | + }); |
| 61 | + |
| 62 | + test("should handle GET request to index route (/)", async ({ |
| 63 | + browser, |
| 64 | + }) => { |
| 65 | + const context = await browser.newContext({ |
| 66 | + ignoreHTTPSErrors: true, |
| 67 | + }); |
| 68 | + const page = await context.newPage(); |
| 69 | + |
| 70 | + page |
| 71 | + .on("console", (message) => { |
| 72 | + consoleMessages.push(message); |
| 73 | + }) |
| 74 | + .on("pageerror", (error) => { |
| 75 | + pageErrors.push(error); |
| 76 | + }); |
| 77 | + |
| 78 | + const pageUrl = |
| 79 | + server === "https" || server === "spdy" || server === "http2" |
| 80 | + ? `https://127.0.0.1:${port}/` |
| 81 | + : `http://127.0.0.1:${port}/`; |
| 82 | + |
| 83 | + const response = await page.goto(pageUrl, { |
| 84 | + waitUntil: "networkidle0", |
| 85 | + }); |
| 86 | + |
| 87 | + const HTTPVersion = await page.evaluate( |
| 88 | + () => performance.getEntries()[0].nextHopProtocol, |
| 89 | + ); |
| 90 | + |
| 91 | + const isSpdy = server === "spdy"; |
| 92 | + |
| 93 | + if (isSpdy) { |
| 94 | + expect(HTTPVersion).toEqual("h2"); |
| 95 | + } else { |
| 96 | + expect(HTTPVersion).toEqual("http/1.1"); |
| 97 | + } |
| 98 | + |
| 99 | + expect(JSON.stringify(response.status())).toMatchSnapshot(); |
| 100 | + expect(JSON.stringify(await response.text())).toMatchSnapshot(); |
| 101 | + expect( |
| 102 | + JSON.stringify(consoleMessages.map((message) => message.text())), |
| 103 | + ).toMatchSnapshot(); |
| 104 | + expect(JSON.stringify(pageErrors)).toMatchSnapshot(); |
| 105 | + }); |
| 106 | + }); |
| 107 | + } |
| 108 | + } |
| 109 | +}); |
0 commit comments