-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathapp.test.js
More file actions
195 lines (176 loc) · 5.23 KB
/
Copy pathapp.test.js
File metadata and controls
195 lines (176 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import fs from "node:fs";
import { createSecureServer } from "node:http2";
import { createServer as createHttpsServer } from "node:https";
import path from "node:path";
import { afterEach, beforeEach, describe, it } from "node:test";
import { fileURLToPath } from "node:url";
import { createAdaptorServer } from "@hono/node-server";
import connect from "connect";
import { expect } from "expect";
import express from "express";
import { Hono } from "hono";
import webpack from "webpack";
import wdm from "webpack-dev-middleware";
import Server from "../../lib/Server.js";
import config from "../fixtures/client-config/webpack.config.js";
import runBrowser from "../helpers/run-browser.js";
import portsMap from "../ports-map.js";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const port = portsMap.app;
const staticDirectory = path.resolve(
__dirname,
"../fixtures/static-config/public",
);
const apps = [
["express", () => express(), "http"],
["express", () => express(), "https"],
["connect", () => connect(), "http"],
["connect", () => connect(), "https"],
["connect", () => connect(), "http2"],
["connect (async)", () => connect(), "http"],
[
"hono",
() => new Hono(),
(options, app) =>
createAdaptorServer({
fetch: app.fetch,
}),
(_, devServer) => [
{
name: "webpack-dev-middleware",
middleware: wdm.honoWrapper(devServer.compiler),
},
],
],
[
"hono",
() => new Hono(),
(_, app) =>
createAdaptorServer({
fetch: app.fetch,
createServer: createHttpsServer,
serverOptions: {
key: fs.readFileSync(
path.resolve(__dirname, "../fixtures/ssl/localhost-privkey.pem"),
),
cert: fs.readFileSync(
path.resolve(__dirname, "../fixtures/ssl/localhost-cert.pem"),
),
},
}),
(_, devServer) => [
{
name: "webpack-dev-middleware",
middleware: wdm.honoWrapper(devServer.compiler),
},
],
],
[
"hono",
() => new Hono(),
{
type: (options, app) =>
createAdaptorServer({
fetch: app.fetch,
createServer: createSecureServer,
serverOptions: options,
}),
options: {
allowHTTP1: true,
key: fs.readFileSync(
path.resolve(__dirname, "../fixtures/ssl/localhost-privkey.pem"),
),
cert: fs.readFileSync(
path.resolve(__dirname, "../fixtures/ssl/localhost-cert.pem"),
),
},
},
(_, devServer) => [
{
name: "webpack-dev-middleware",
middleware: wdm.honoWrapper(devServer.compiler),
},
],
],
];
describe("app option", () => {
for (const [appName, app, server, setupMiddlewares] of apps) {
let compiler;
let devServer;
let page;
let browser;
let pageErrors;
let consoleMessages;
describe(`should work using "${appName}" application and "${typeof server === "function" ? "custom server" : server}" server`, () => {
beforeEach(async () => {
compiler = webpack(config);
devServer = new Server(
{
static: {
directory: staticDirectory,
watch: false,
},
app,
server,
port,
setupMiddlewares:
typeof setupMiddlewares !== "undefined"
? setupMiddlewares
: undefined,
},
compiler,
);
await devServer.start();
({ page, browser } = await runBrowser());
pageErrors = [];
consoleMessages = [];
});
afterEach(async () => {
await browser.close();
await devServer.stop();
await new Promise((resolve) => {
compiler.close(() => {
resolve();
});
});
});
it("should handle GET request to index route (/)", async () => {
page
.on("console", (message) => {
consoleMessages.push(message);
})
.on("pageerror", (error) => {
pageErrors.push(error);
});
const pageUrl = devServer.isTlsServer
? `https://localhost:${port}/`
: `http://localhost:${port}/`;
const response = await page.goto(pageUrl, {
waitUntil: "networkidle0",
});
const HTTPVersion = await page.evaluate(
() => performance.getEntries()[0].nextHopProtocol,
);
if (
server === "http2" ||
(server.options && server.options.allowHTTP1)
) {
expect(HTTPVersion).toBe("h2");
} else {
expect(HTTPVersion).toBe("http/1.1");
}
expect(response.status()).toBe(200);
const text = await response.text();
expect(text).toContain(
'<script type="text/javascript" charset="utf-8" src="/main.js"></script>',
);
expect(consoleMessages.map((message) => message.text())).toEqual([
"[webpack-dev-server] Server started: Hot Module Replacement enabled, Live Reloading enabled, Progress disabled, Overlay enabled.",
"[HMR] Waiting for update signal from WDS...",
"Hey.",
]);
expect(pageErrors).toHaveLength(0);
});
});
}
});