Skip to content

Commit fb2cab8

Browse files
authored
test: add e2e tests for client option (#3882)
* test: add e2e tests for `client` option * test: add e2e tests for `client` option * test: add more cases for ws * chore: update test case title
1 parent 01ed758 commit fb2cab8

File tree

3 files changed

+325
-0
lines changed

3 files changed

+325
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`client option configure client entry should disable client entry: console messages 1`] = `Array []`;
4+
5+
exports[`client option configure client entry should disable client entry: page errors 1`] = `Array []`;
6+
7+
exports[`client option configure client entry should disable client entry: response status 1`] = `200`;
8+
9+
exports[`client option default behaviour responds with a 200 status code for /ws path: console messages 1`] = `Array []`;
10+
11+
exports[`client option default behaviour responds with a 200 status code for /ws path: page errors 1`] = `Array []`;
12+
13+
exports[`client option default behaviour responds with a 200 status code for /ws path: response status 1`] = `200`;
14+
15+
exports[`client option should respect path option responds with a 200 status code for /foo/test/bar path: console messages 1`] = `Array []`;
16+
17+
exports[`client option should respect path option responds with a 200 status code for /foo/test/bar path: page errors 1`] = `Array []`;
18+
19+
exports[`client option should respect path option responds with a 200 status code for /foo/test/bar path: response status 1`] = `200`;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`client option configure client entry should disable client entry: console messages 1`] = `Array []`;
4+
5+
exports[`client option configure client entry should disable client entry: page errors 1`] = `Array []`;
6+
7+
exports[`client option configure client entry should disable client entry: response status 1`] = `200`;
8+
9+
exports[`client option default behaviour responds with a 200 status code for /ws path: console messages 1`] = `Array []`;
10+
11+
exports[`client option default behaviour responds with a 200 status code for /ws path: page errors 1`] = `Array []`;
12+
13+
exports[`client option default behaviour responds with a 200 status code for /ws path: response status 1`] = `200`;
14+
15+
exports[`client option should respect path option responds with a 200 status code for /foo/test/bar path: console messages 1`] = `Array []`;
16+
17+
exports[`client option should respect path option responds with a 200 status code for /foo/test/bar path: page errors 1`] = `Array []`;
18+
19+
exports[`client option should respect path option responds with a 200 status code for /foo/test/bar path: response status 1`] = `200`;

test/e2e/client.test.js

Lines changed: 287 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
1+
"use strict";
2+
3+
const webpack = require("webpack");
4+
const Server = require("../../lib/Server");
5+
const config = require("../fixtures/simple-config-other/webpack.config");
6+
const runBrowser = require("../helpers/run-browser");
7+
const port = require("../ports-map")["client-option"];
8+
9+
describe("client option", () => {
10+
describe("default behaviour", () => {
11+
let compiler;
12+
let server;
13+
let page;
14+
let browser;
15+
let pageErrors;
16+
let consoleMessages;
17+
18+
beforeEach(async () => {
19+
compiler = webpack(config);
20+
21+
server = new Server(
22+
{
23+
client: {
24+
webSocketTransport: "sockjs",
25+
},
26+
webSocketServer: "sockjs",
27+
port,
28+
},
29+
compiler
30+
);
31+
32+
await server.start();
33+
34+
({ page, browser } = await runBrowser());
35+
36+
pageErrors = [];
37+
consoleMessages = [];
38+
});
39+
40+
afterEach(async () => {
41+
await browser.close();
42+
await server.stop();
43+
});
44+
45+
it("overlay should be true by default", () => {
46+
expect(server.options.client.overlay).toBe(true);
47+
});
48+
49+
it("responds with a 200 status code for /ws path", async () => {
50+
page
51+
.on("console", (message) => {
52+
consoleMessages.push(message);
53+
})
54+
.on("pageerror", (error) => {
55+
pageErrors.push(error);
56+
});
57+
58+
const response = await page.goto(`http://127.0.0.1:${port}/ws`, {
59+
waitUntil: "networkidle0",
60+
});
61+
62+
expect(response.status()).toMatchSnapshot("response status");
63+
64+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
65+
"console messages"
66+
);
67+
68+
expect(pageErrors).toMatchSnapshot("page errors");
69+
});
70+
});
71+
72+
describe("should respect path option", () => {
73+
let compiler;
74+
let server;
75+
let page;
76+
let browser;
77+
let pageErrors;
78+
let consoleMessages;
79+
80+
beforeEach(async () => {
81+
compiler = webpack(config);
82+
83+
server = new Server(
84+
{
85+
client: {
86+
webSocketTransport: "sockjs",
87+
},
88+
webSocketServer: {
89+
type: "sockjs",
90+
options: {
91+
host: "localhost",
92+
port,
93+
path: "/foo/test/bar",
94+
},
95+
},
96+
port,
97+
},
98+
compiler
99+
);
100+
101+
await server.start();
102+
103+
({ page, browser } = await runBrowser());
104+
105+
pageErrors = [];
106+
consoleMessages = [];
107+
});
108+
109+
afterEach(async () => {
110+
await browser.close();
111+
await server.stop();
112+
});
113+
114+
it("responds with a 200 status code for /foo/test/bar path", async () => {
115+
page
116+
.on("console", (message) => {
117+
consoleMessages.push(message);
118+
})
119+
.on("pageerror", (error) => {
120+
pageErrors.push(error);
121+
});
122+
123+
const response = await page.goto(
124+
`http://127.0.0.1:${port}/foo/test/bar`,
125+
{
126+
waitUntil: "networkidle0",
127+
}
128+
);
129+
130+
expect(response.status()).toMatchSnapshot("response status");
131+
132+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
133+
"console messages"
134+
);
135+
136+
expect(pageErrors).toMatchSnapshot("page errors");
137+
});
138+
});
139+
140+
describe("configure client entry", () => {
141+
let compiler;
142+
let server;
143+
let page;
144+
let browser;
145+
let pageErrors;
146+
let consoleMessages;
147+
148+
beforeEach(async () => {
149+
compiler = webpack(config);
150+
151+
server = new Server(
152+
{
153+
client: false,
154+
port,
155+
},
156+
compiler
157+
);
158+
159+
await server.start();
160+
161+
({ page, browser } = await runBrowser());
162+
163+
pageErrors = [];
164+
consoleMessages = [];
165+
});
166+
167+
afterEach(async () => {
168+
await browser.close();
169+
await server.stop();
170+
});
171+
172+
it("should disable client entry", async () => {
173+
page
174+
.on("console", (message) => {
175+
consoleMessages.push(message);
176+
})
177+
.on("pageerror", (error) => {
178+
pageErrors.push(error);
179+
});
180+
181+
const response = await page.goto(`http://127.0.0.1:${port}/main.js`, {
182+
waitUntil: "networkidle0",
183+
});
184+
185+
expect(response.status()).toMatchSnapshot("response status");
186+
187+
expect(await response.text()).not.toMatch(/client\/index\.js/);
188+
189+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
190+
"console messages"
191+
);
192+
193+
expect(pageErrors).toMatchSnapshot("page errors");
194+
});
195+
});
196+
197+
describe("webSocketTransport", () => {
198+
const clientModes = [
199+
{
200+
title: 'as a string ("sockjs")',
201+
client: {
202+
webSocketTransport: "sockjs",
203+
},
204+
webSocketServer: "sockjs",
205+
shouldThrow: false,
206+
},
207+
{
208+
title: 'as a string ("ws")',
209+
client: {
210+
webSocketTransport: "ws",
211+
},
212+
webSocketServer: "ws",
213+
shouldThrow: false,
214+
},
215+
{
216+
title: 'as a path ("sockjs")',
217+
client: {
218+
webSocketTransport: require.resolve(
219+
"../../client-src/clients/SockJSClient"
220+
),
221+
},
222+
webSocketServer: "sockjs",
223+
shouldThrow: false,
224+
},
225+
{
226+
title: 'as a path ("ws")',
227+
client: {
228+
webSocketTransport: require.resolve(
229+
"../../client-src/clients/WebSocketClient"
230+
),
231+
},
232+
webSocketServer: "ws",
233+
shouldThrow: false,
234+
},
235+
{
236+
title: "as a nonexistent path (sockjs)",
237+
client: {
238+
webSocketTransport: "/bad/path/to/implementation",
239+
},
240+
webSocketServer: "sockjs",
241+
shouldThrow: true,
242+
},
243+
{
244+
title: "as a nonexistent path (ws)",
245+
client: {
246+
webSocketTransport: "/bad/path/to/implementation",
247+
},
248+
webSocketServer: "ws",
249+
shouldThrow: true,
250+
},
251+
];
252+
253+
describe("passed to server", () => {
254+
clientModes.forEach((data) => {
255+
it(`${data.title} ${
256+
data.shouldThrow ? "should throw" : "should not throw"
257+
}`, async () => {
258+
const compiler = webpack(config);
259+
260+
const server = new Server(
261+
{
262+
client: data.client,
263+
port,
264+
},
265+
compiler
266+
);
267+
268+
let thrownError;
269+
270+
try {
271+
await server.start();
272+
} catch (error) {
273+
thrownError = error;
274+
}
275+
276+
if (data.shouldThrow) {
277+
expect(thrownError.message).toMatch(
278+
/client\.webSocketTransport must be a string/
279+
);
280+
}
281+
282+
await server.stop();
283+
});
284+
});
285+
});
286+
});
287+
});

0 commit comments

Comments
 (0)