Skip to content

Commit

Permalink
test: add e2e test for WEBPACK_SERVE env variable (#4125)
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Dec 21, 2021
1 parent f5a9d05 commit 37b73d5
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 25 deletions.
13 changes: 13 additions & 0 deletions test/e2e/__snapshots__/api.test.js.snap.webpack4
Expand Up @@ -110,6 +110,19 @@ exports[`API Server.getFreePort should return the port when the port is undefine

exports[`API Server.getFreePort should throw the error when the port isn't found 1`] = `"busy"`;

exports[`API WEBPACK_SERVE environment variable should be present: 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 WEBPACK_SERVE environment variable should be present: page errors 1`] = `Array []`;

exports[`API WEBPACK_SERVE environment variable should be present: response status 1`] = `200`;

exports[`API deprecated API should log warning when the "port" and "host" options from options different from arguments ('listen' method): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
Expand Down
13 changes: 13 additions & 0 deletions test/e2e/__snapshots__/api.test.js.snap.webpack5
Expand Up @@ -110,6 +110,19 @@ exports[`API Server.getFreePort should return the port when the port is undefine

exports[`API Server.getFreePort should throw the error when the port isn't found 1`] = `"busy"`;

exports[`API WEBPACK_SERVE environment variable should be present: 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 WEBPACK_SERVE environment variable should be present: page errors 1`] = `Array []`;

exports[`API WEBPACK_SERVE environment variable should be present: response status 1`] = `200`;

exports[`API deprecated API should log warning when the "port" and "host" options from options different from arguments ('listen' method): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
Expand Down
62 changes: 62 additions & 0 deletions test/e2e/api.test.js
Expand Up @@ -9,6 +9,68 @@ const runBrowser = require("../helpers/run-browser");
const port = require("../ports-map").api;

describe("API", () => {
describe("WEBPACK_SERVE environment variable", () => {
const OLD_ENV = process.env;
let server;
let page;
let browser;
let pageErrors;
let consoleMessages;

beforeEach(async () => {
// this is important - it clears the cache
jest.resetModules();

process.env = { ...OLD_ENV };

delete process.env.WEBPACK_SERVE;

({ page, browser } = await runBrowser());

pageErrors = [];
consoleMessages = [];
});

afterEach(async () => {
await browser.close();
await server.stop();
process.env = OLD_ENV;
});

it("should be present", async () => {
expect(process.env.WEBPACK_SERVE).toBeUndefined();

page
.on("console", (message) => {
consoleMessages.push(message);
})
.on("pageerror", (error) => {
pageErrors.push(error);
});

const WebpackDevServer = require("../../lib/Server");

const compiler = webpack(config);
server = new WebpackDevServer({ port }, compiler);

await server.start();

expect(process.env.WEBPACK_SERVE).toBe(true);

const response = await page.goto(`http://127.0.0.1:${port}/main`, {
waitUntil: "networkidle0",
});

expect(response.status()).toMatchSnapshot("response status");

expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
"console messages"
);

expect(pageErrors).toMatchSnapshot("page errors");
});
});

describe("latest async API", () => {
it(`should work with async API`, async () => {
const compiler = webpack(config);
Expand Down
25 changes: 0 additions & 25 deletions test/server/Server.test.js
Expand Up @@ -161,29 +161,4 @@ describe("Server", () => {
await server.stop();
});
});

describe("WEBPACK_SERVE environment variable", () => {
const OLD_ENV = process.env;

beforeEach(() => {
// this is important - it clears the cache
jest.resetModules();

process.env = { ...OLD_ENV };

delete process.env.WEBPACK_SERVE;
});

afterEach(() => {
process.env = OLD_ENV;
});

it("should be present", () => {
expect(process.env.WEBPACK_SERVE).toBeUndefined();

require("../../lib/Server");

expect(process.env.WEBPACK_SERVE).toBe(true);
});
});
});

0 comments on commit 37b73d5

Please sign in to comment.