Skip to content

test: add e2e test for onListening option #3711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions test/e2e/__snapshots__/on-listening.test.js.snap.webpack4
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`onListening option should handle GET request to /listening/some/path route: console messages 1`] = `Array []`;

exports[`onListening option should handle GET request to /listening/some/path route: page errors 1`] = `Array []`;

exports[`onListening option should handle GET request to /listening/some/path route: response headers content-type 1`] = `"text/html; charset=utf-8"`;

exports[`onListening option should handle GET request to /listening/some/path route: response status 1`] = `200`;

exports[`onListening option should handle GET request to /listening/some/path route: response text 1`] = `"listening"`;

exports[`onListening option should handle POST request to /listening/some/path route: console messages 1`] = `Array []`;

exports[`onListening option should handle POST request to /listening/some/path route: page errors 1`] = `Array []`;

exports[`onListening option should handle POST request to /listening/some/path route: response headers content-type 1`] = `"text/html; charset=utf-8"`;

exports[`onListening option should handle POST request to /listening/some/path route: response status 1`] = `200`;

exports[`onListening option should handle POST request to /listening/some/path route: response text 1`] = `"listening POST"`;
21 changes: 21 additions & 0 deletions test/e2e/__snapshots__/on-listening.test.js.snap.webpack5
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`onListening option should handle GET request to /listening/some/path route: console messages 1`] = `Array []`;

exports[`onListening option should handle GET request to /listening/some/path route: page errors 1`] = `Array []`;

exports[`onListening option should handle GET request to /listening/some/path route: response headers content-type 1`] = `"text/html; charset=utf-8"`;

exports[`onListening option should handle GET request to /listening/some/path route: response status 1`] = `200`;

exports[`onListening option should handle GET request to /listening/some/path route: response text 1`] = `"listening"`;

exports[`onListening option should handle POST request to /listening/some/path route: console messages 1`] = `Array []`;

exports[`onListening option should handle POST request to /listening/some/path route: page errors 1`] = `Array []`;

exports[`onListening option should handle POST request to /listening/some/path route: response headers content-type 1`] = `"text/html; charset=utf-8"`;

exports[`onListening option should handle POST request to /listening/some/path route: response status 1`] = `200`;

exports[`onListening option should handle POST request to /listening/some/path route: response text 1`] = `"listening POST"`;
125 changes: 125 additions & 0 deletions test/e2e/on-listening.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
"use strict";

const webpack = require("webpack");
const Server = require("../../lib/Server");
const config = require("../fixtures/client-config/webpack.config");
const runBrowser = require("../helpers/run-browser");
const port = require("../ports-map")["on-listening-option"];

describe("onListening option", () => {
let compiler;
let server;
let page;
let browser;
let pageErrors;
let consoleMessages;
let onListeningIsRunning = false;

beforeEach(async () => {
compiler = webpack(config);
server = new Server(
{
onAfterSetupMiddleware: (devServer) => {
if (!devServer) {
throw new Error("webpack-dev-server is not defined");
}

onListeningIsRunning = true;

devServer.app.get("/listening/some/path", (_, response) => {
response.send("listening");
});

devServer.app.post("/listening/some/path", (_, response) => {
response.send("listening POST");
});
},
port,
},
compiler
);

await server.start();

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

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

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

it("should handle GET request to /listening/some/path route", async () => {
page
.on("console", (message) => {
consoleMessages.push(message);
})
.on("pageerror", (error) => {
pageErrors.push(error);
});

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

expect(onListeningIsRunning).toBe(true);

expect(response.headers()["content-type"]).toMatchSnapshot(
"response headers content-type"
);

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

expect(await response.text()).toMatchSnapshot("response text");

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

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

it("should handle POST request to /listening/some/path route", async () => {
await page.setRequestInterception(true);

page
.on("console", (message) => {
consoleMessages.push(message);
})
.on("pageerror", (error) => {
pageErrors.push(error);
})
.on("request", (interceptedRequest) => {
interceptedRequest.continue({ method: "POST" });
});

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

expect(onListeningIsRunning).toBe(true);

expect(response.headers()["content-type"]).toMatchSnapshot(
"response headers content-type"
);

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

expect(await response.text()).toMatchSnapshot("response text");

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

expect(pageErrors).toMatchSnapshot("page errors");
});
});
39 changes: 0 additions & 39 deletions test/server/onListening-option.test.js

This file was deleted.