From c4678bc467370e7dc74d06a8b898515e448d0da0 Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Tue, 17 Aug 2021 19:46:27 +0300 Subject: [PATCH] fix: legacy API (#3660) --- lib/Server.js | 2 +- .../__snapshots__/api.test.js.snap.webpack4 | 26 ++++++- .../__snapshots__/api.test.js.snap.webpack5 | 26 ++++++- test/e2e/api.test.js | 69 ++++++++++++++++++- 4 files changed, 117 insertions(+), 6 deletions(-) diff --git a/lib/Server.js b/lib/Server.js index c92bc8218a..35e8d8973c 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -25,7 +25,7 @@ class Server { "DEP_WEBPACK_DEV_SERVER_CONSTRUCTOR" )(); - [options, compiler] = [compiler, options]; + [options = {}, compiler] = [compiler, options]; } validate(schema, options, "webpack Dev Server"); diff --git a/test/e2e/__snapshots__/api.test.js.snap.webpack4 b/test/e2e/__snapshots__/api.test.js.snap.webpack4 index 8c31dd9873..153ebefc32 100644 --- a/test/e2e/__snapshots__/api.test.js.snap.webpack4 +++ b/test/e2e/__snapshots__/api.test.js.snap.webpack4 @@ -22,7 +22,7 @@ Array [ exports[`API should work with callback API: page errors 1`] = `Array []`; -exports[`API should work with deprecated API: console messages 1`] = ` +exports[`API should work with deprecated API ('listen' and \`close\` methods): console messages 1`] = ` Array [ "[HMR] Waiting for update signal from WDS...", "Hey.", @@ -31,4 +31,26 @@ Array [ ] `; -exports[`API should work with deprecated API: page errors 1`] = `Array []`; +exports[`API should work with deprecated API ('listen' and \`close\` methods): page errors 1`] = `Array []`; + +exports[`API should work with deprecated API (only compiler in constructor): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`API should work with deprecated API (only compiler in constructor): page errors 1`] = `Array []`; + +exports[`API should work with deprecated API (the order of the arguments in the constructor): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`API should work with deprecated API (the order of the arguments in the constructor): page errors 1`] = `Array []`; diff --git a/test/e2e/__snapshots__/api.test.js.snap.webpack5 b/test/e2e/__snapshots__/api.test.js.snap.webpack5 index 8c31dd9873..153ebefc32 100644 --- a/test/e2e/__snapshots__/api.test.js.snap.webpack5 +++ b/test/e2e/__snapshots__/api.test.js.snap.webpack5 @@ -22,7 +22,7 @@ Array [ exports[`API should work with callback API: page errors 1`] = `Array []`; -exports[`API should work with deprecated API: console messages 1`] = ` +exports[`API should work with deprecated API ('listen' and \`close\` methods): console messages 1`] = ` Array [ "[HMR] Waiting for update signal from WDS...", "Hey.", @@ -31,4 +31,26 @@ Array [ ] `; -exports[`API should work with deprecated API: page errors 1`] = `Array []`; +exports[`API should work with deprecated API ('listen' and \`close\` methods): page errors 1`] = `Array []`; + +exports[`API should work with deprecated API (only compiler in constructor): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`API should work with deprecated API (only compiler in constructor): page errors 1`] = `Array []`; + +exports[`API should work with deprecated API (the order of the arguments in the constructor): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`API should work with deprecated API (the order of the arguments in the constructor): page errors 1`] = `Array []`; diff --git a/test/e2e/api.test.js b/test/e2e/api.test.js index d693dbc382..3d9311dced 100644 --- a/test/e2e/api.test.js +++ b/test/e2e/api.test.js @@ -79,7 +79,7 @@ describe("API", () => { }); }); - it(`should work with deprecated API`, async () => { + it("should work with deprecated API ('listen' and `close` methods)", async () => { const compiler = webpack(config); const devServerOptions = { port }; const server = new Server(devServerOptions, compiler); @@ -125,4 +125,71 @@ describe("API", () => { }); }); }); + + it(`should work with deprecated API (the order of the arguments in the constructor)`, async () => { + const compiler = webpack(config); + const devServerOptions = { port }; + const server = new Server(compiler, devServerOptions); + + await server.start(); + + const { page, browser } = await runBrowser(); + + const pageErrors = []; + const consoleMessages = []; + + page + .on("console", (message) => { + consoleMessages.push(message); + }) + .on("pageerror", (error) => { + pageErrors.push(error); + }); + + await page.goto(`http://127.0.0.1:${port}/main`, { + waitUntil: "networkidle0", + }); + + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( + "console messages" + ); + expect(pageErrors).toMatchSnapshot("page errors"); + + await browser.close(); + await server.stop(); + }); + + it(`should work with deprecated API (only compiler in constructor)`, async () => { + const compiler = webpack(config); + const server = new Server(compiler); + + server.options.port = port; + + await server.start(); + + const { page, browser } = await runBrowser(); + + const pageErrors = []; + const consoleMessages = []; + + page + .on("console", (message) => { + consoleMessages.push(message); + }) + .on("pageerror", (error) => { + pageErrors.push(error); + }); + + await page.goto(`http://127.0.0.1:${port}/main`, { + waitUntil: "networkidle0", + }); + + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( + "console messages" + ); + expect(pageErrors).toMatchSnapshot("page errors"); + + await browser.close(); + await server.stop(); + }); });