Skip to content

Commit db4e75c

Browse files
authored
test: add e2e test for onListening option (#3711)
1 parent 46b459a commit db4e75c

File tree

4 files changed

+167
-39
lines changed

4 files changed

+167
-39
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`onListening option should handle GET request to /listening/some/path route: console messages 1`] = `Array []`;
4+
5+
exports[`onListening option should handle GET request to /listening/some/path route: page errors 1`] = `Array []`;
6+
7+
exports[`onListening option should handle GET request to /listening/some/path route: response headers content-type 1`] = `"text/html; charset=utf-8"`;
8+
9+
exports[`onListening option should handle GET request to /listening/some/path route: response status 1`] = `200`;
10+
11+
exports[`onListening option should handle GET request to /listening/some/path route: response text 1`] = `"listening"`;
12+
13+
exports[`onListening option should handle POST request to /listening/some/path route: console messages 1`] = `Array []`;
14+
15+
exports[`onListening option should handle POST request to /listening/some/path route: page errors 1`] = `Array []`;
16+
17+
exports[`onListening option should handle POST request to /listening/some/path route: response headers content-type 1`] = `"text/html; charset=utf-8"`;
18+
19+
exports[`onListening option should handle POST request to /listening/some/path route: response status 1`] = `200`;
20+
21+
exports[`onListening option should handle POST request to /listening/some/path route: response text 1`] = `"listening POST"`;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`onListening option should handle GET request to /listening/some/path route: console messages 1`] = `Array []`;
4+
5+
exports[`onListening option should handle GET request to /listening/some/path route: page errors 1`] = `Array []`;
6+
7+
exports[`onListening option should handle GET request to /listening/some/path route: response headers content-type 1`] = `"text/html; charset=utf-8"`;
8+
9+
exports[`onListening option should handle GET request to /listening/some/path route: response status 1`] = `200`;
10+
11+
exports[`onListening option should handle GET request to /listening/some/path route: response text 1`] = `"listening"`;
12+
13+
exports[`onListening option should handle POST request to /listening/some/path route: console messages 1`] = `Array []`;
14+
15+
exports[`onListening option should handle POST request to /listening/some/path route: page errors 1`] = `Array []`;
16+
17+
exports[`onListening option should handle POST request to /listening/some/path route: response headers content-type 1`] = `"text/html; charset=utf-8"`;
18+
19+
exports[`onListening option should handle POST request to /listening/some/path route: response status 1`] = `200`;
20+
21+
exports[`onListening option should handle POST request to /listening/some/path route: response text 1`] = `"listening POST"`;

test/e2e/on-listening.test.js

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
"use strict";
2+
3+
const webpack = require("webpack");
4+
const Server = require("../../lib/Server");
5+
const config = require("../fixtures/client-config/webpack.config");
6+
const runBrowser = require("../helpers/run-browser");
7+
const port = require("../ports-map")["on-listening-option"];
8+
9+
describe("onListening option", () => {
10+
let compiler;
11+
let server;
12+
let page;
13+
let browser;
14+
let pageErrors;
15+
let consoleMessages;
16+
let onListeningIsRunning = false;
17+
18+
beforeEach(async () => {
19+
compiler = webpack(config);
20+
server = new Server(
21+
{
22+
onAfterSetupMiddleware: (devServer) => {
23+
if (!devServer) {
24+
throw new Error("webpack-dev-server is not defined");
25+
}
26+
27+
onListeningIsRunning = true;
28+
29+
devServer.app.get("/listening/some/path", (_, response) => {
30+
response.send("listening");
31+
});
32+
33+
devServer.app.post("/listening/some/path", (_, response) => {
34+
response.send("listening POST");
35+
});
36+
},
37+
port,
38+
},
39+
compiler
40+
);
41+
42+
await server.start();
43+
44+
({ page, browser } = await runBrowser());
45+
46+
pageErrors = [];
47+
consoleMessages = [];
48+
});
49+
50+
afterEach(async () => {
51+
await browser.close();
52+
await server.stop();
53+
});
54+
55+
it("should handle GET request to /listening/some/path route", async () => {
56+
page
57+
.on("console", (message) => {
58+
consoleMessages.push(message);
59+
})
60+
.on("pageerror", (error) => {
61+
pageErrors.push(error);
62+
});
63+
64+
const response = await page.goto(
65+
`http://127.0.0.1:${port}/listening/some/path`,
66+
{
67+
waitUntil: "networkidle0",
68+
}
69+
);
70+
71+
expect(onListeningIsRunning).toBe(true);
72+
73+
expect(response.headers()["content-type"]).toMatchSnapshot(
74+
"response headers content-type"
75+
);
76+
77+
expect(response.status()).toMatchSnapshot("response status");
78+
79+
expect(await response.text()).toMatchSnapshot("response text");
80+
81+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
82+
"console messages"
83+
);
84+
85+
expect(pageErrors).toMatchSnapshot("page errors");
86+
});
87+
88+
it("should handle POST request to /listening/some/path route", async () => {
89+
await page.setRequestInterception(true);
90+
91+
page
92+
.on("console", (message) => {
93+
consoleMessages.push(message);
94+
})
95+
.on("pageerror", (error) => {
96+
pageErrors.push(error);
97+
})
98+
.on("request", (interceptedRequest) => {
99+
interceptedRequest.continue({ method: "POST" });
100+
});
101+
102+
const response = await page.goto(
103+
`http://127.0.0.1:${port}/listening/some/path`,
104+
{
105+
waitUntil: "networkidle0",
106+
}
107+
);
108+
109+
expect(onListeningIsRunning).toBe(true);
110+
111+
expect(response.headers()["content-type"]).toMatchSnapshot(
112+
"response headers content-type"
113+
);
114+
115+
expect(response.status()).toMatchSnapshot("response status");
116+
117+
expect(await response.text()).toMatchSnapshot("response text");
118+
119+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
120+
"console messages"
121+
);
122+
123+
expect(pageErrors).toMatchSnapshot("page errors");
124+
});
125+
});

test/server/onListening-option.test.js

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)