-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathvite-hmr-hdr-test.ts
341 lines (309 loc) · 10.8 KB
/
vite-hmr-hdr-test.ts
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import fs from "node:fs/promises";
import path from "node:path";
import type { Page, PlaywrightWorkerOptions } from "@playwright/test";
import { expect } from "@playwright/test";
import type { Files } from "./helpers/vite.js";
import {
test,
createEditor,
EXPRESS_SERVER,
viteConfig,
viteMajorTemplates,
} from "./helpers/vite.js";
const indexRoute = `
// imports
import { useState, useEffect } from "react";
export const meta = () => [{ title: "HMR updated title: 0" }]
// loader
export default function IndexRoute() {
// hooks
const [mounted, setMounted] = useState(false);
useEffect(() => {
setMounted(true);
}, []);
return (
<div id="index">
<h2 data-title>Index</h2>
<input />
<p data-mounted>Mounted: {mounted ? "yes" : "no"}</p>
<p data-hmr>HMR updated: 0</p>
{/* elements */}
</div>
);
}
`;
test.describe("Vite HMR & HDR", () => {
viteMajorTemplates.forEach(({ templateName, templateDisplayName }) => {
test.describe(templateDisplayName, () => {
test("vite dev", async ({ page, browserName, dev }) => {
let files: Files = async ({ port }) => ({
"vite.config.js": await viteConfig.basic({ port }),
"app/routes/_index.tsx": indexRoute,
});
let { cwd, port } = await dev(files, templateName);
await workflow({ page, browserName, cwd, port });
});
test("express", async ({ page, browserName, customDev }) => {
let files: Files = async ({ port }) => ({
"vite.config.js": await viteConfig.basic({ port }),
"server.mjs": EXPRESS_SERVER({ port }),
"app/routes/_index.tsx": indexRoute,
});
let { cwd, port } = await customDev(files, templateName);
await workflow({ page, browserName, cwd, port });
});
test("mdx", async ({ page, dev }) => {
let files: Files = async ({ port }) => ({
"vite.config.ts": `
import { defineConfig } from "vite";
import { reactRouter } from "@react-router/dev/vite";
import mdx from "@mdx-js/rollup";
export default defineConfig({
${await viteConfig.server({ port })}
plugins: [
mdx(),
reactRouter(),
],
});
`,
"app/component.tsx": `
import {useState} from "react";
export const Counter = () => {
const [count, setCount] = useState(0);
return <button onClick={() => setCount(count => count + 1)}>Count: {count}</button>
}
`,
"app/routes/mdx.mdx": `
import { Counter } from "../component";
# MDX Title (HMR: 0)
<Counter />
`,
});
let { port, cwd } = await dev(files, templateName);
let edit = createEditor(cwd);
await page.goto(`http://localhost:${port}/mdx`, {
waitUntil: "networkidle",
});
await expect(page.locator("h1")).toHaveText("MDX Title (HMR: 0)");
let button = page.locator("button");
await expect(button).toHaveText("Count: 0");
await button.click();
await expect(button).toHaveText("Count: 1");
await edit("app/routes/mdx.mdx", (contents) =>
contents.replace("(HMR: 0)", "(HMR: 1)")
);
await page.waitForLoadState("networkidle");
await expect(page.locator("h1")).toHaveText("MDX Title (HMR: 1)");
await expect(page.locator("button")).toHaveText("Count: 1");
expect(page.errors).toEqual([]);
});
});
});
});
async function workflow({
page,
browserName,
cwd,
port,
}: {
page: Page;
browserName: PlaywrightWorkerOptions["browserName"];
cwd: string;
port: number;
}) {
let edit = createEditor(cwd);
// setup: initial render
await page.goto(`http://localhost:${port}/`, {
waitUntil: "networkidle",
});
await expect(page.locator("#index [data-title]")).toHaveText("Index");
// setup: hydration
await expect(page.locator("#index [data-mounted]")).toHaveText(
"Mounted: yes"
);
// setup: browser state
let hmrStatus = page.locator("#index [data-hmr]");
await expect(page).toHaveTitle("HMR updated title: 0");
await expect(hmrStatus).toHaveText("HMR updated: 0");
let input = page.locator("#index input");
await expect(input).toBeVisible();
await input.type("stateful");
expect(page.errors).toEqual([]);
// route: HMR
await edit("app/routes/_index.tsx", (contents) =>
contents
.replace("HMR updated title: 0", "HMR updated title: 1")
.replace("HMR updated: 0", "HMR updated: 1")
);
await page.waitForLoadState("networkidle");
await expect(page).toHaveTitle("HMR updated title: 1");
await expect(hmrStatus).toHaveText("HMR updated: 1");
await expect(input).toHaveValue("stateful");
expect(page.errors).toEqual([]);
// route: add loader
await edit("app/routes/_index.tsx", (contents) =>
contents
.replace(
"// imports",
`// imports\nimport { useLoaderData } from "react-router"`
)
.replace(
"// loader",
`// loader\nexport const loader = () => ({ message: "HDR updated: 0" });`
)
.replace(
"// hooks",
"// hooks\nconst { message } = useLoaderData<typeof loader>();"
)
.replace(
"{/* elements */}",
`{/* elements */}\n<p data-hdr>{message}</p>`
)
);
await page.waitForLoadState("networkidle");
let hdrStatus = page.locator("#index [data-hdr]");
await expect(hdrStatus).toHaveText("HDR updated: 0");
// React Fast Refresh cannot preserve state for a component when hooks are added or removed
await expect(input).toHaveValue("");
await input.type("stateful");
expect(page.errors.length).toBeGreaterThan(0);
expect(
// When adding a loader, a harmless error is logged to the browser console.
// HMR works as intended, so this seems like a React Fast Refresh bug caused by off-screen rendering with old server data or something like that 🤷
page.errors.filter((error) => {
let chromium =
browserName === "chromium" &&
error.message ===
"Cannot destructure property 'message' of 'useLoaderData(...)' as it is null.";
let firefox =
browserName === "firefox" &&
error.message === "(intermediate value)() is null";
let webkit =
browserName === "webkit" &&
error.message === "Right side of assignment cannot be destructured";
let expected = chromium || firefox || webkit;
return !expected;
})
).toEqual([]);
page.errors = [];
// route: HDR
await edit("app/routes/_index.tsx", (contents) =>
contents.replace("HDR updated: 0", "HDR updated: 1")
);
await page.waitForLoadState("networkidle");
await expect(hdrStatus).toHaveText("HDR updated: 1");
await expect(input).toHaveValue("stateful");
// route: HMR + HDR
await edit("app/routes/_index.tsx", (contents) =>
contents
.replace("HMR updated: 1", "HMR updated: 2")
.replace("HDR updated: 1", "HDR updated: 2")
);
await page.waitForLoadState("networkidle");
await expect(hmrStatus).toHaveText("HMR updated: 2");
await expect(hdrStatus).toHaveText("HDR updated: 2");
await expect(input).toHaveValue("stateful");
expect(page.errors).toEqual([]);
// create new non-route component module
await fs.writeFile(
path.join(cwd, "app/component.tsx"),
String.raw`
export function MyComponent() {
return <p data-component>Component HMR: 0</p>;
}
`,
"utf8"
);
await edit("app/routes/_index.tsx", (contents) =>
contents
.replace(
"// imports",
`// imports\nimport { MyComponent } from "../component";`
)
.replace("{/* elements */}", "{/* elements */}\n<MyComponent />")
);
await page.waitForLoadState("networkidle");
let component = page.locator("#index [data-component]");
await expect(component).toBeVisible();
await expect(component).toHaveText("Component HMR: 0");
await expect(input).toHaveValue("stateful");
expect(page.errors).toEqual([]);
// non-route: HMR
await edit("app/component.tsx", (contents) =>
contents.replace("Component HMR: 0", "Component HMR: 1")
);
await page.waitForLoadState("networkidle");
await expect(component).toHaveText("Component HMR: 1");
await expect(input).toHaveValue("stateful");
expect(page.errors).toEqual([]);
// create new non-route server module
await fs.writeFile(
path.join(cwd, "app/indirect-hdr-dep.ts"),
String.raw`export const indirect = "indirect 0"`,
"utf8"
);
await fs.writeFile(
path.join(cwd, "app/direct-hdr-dep.ts"),
String.raw`
import { indirect } from "./indirect-hdr-dep"
export const direct = "direct 0 & " + indirect
`,
"utf8"
);
await edit("app/routes/_index.tsx", (contents) =>
contents
.replace(
"// imports",
`// imports\nimport { direct } from "../direct-hdr-dep"`
)
.replace(
`{ message: "HDR updated: 2" }`,
`{ message: "HDR updated: " + direct }`
)
);
await page.waitForLoadState("networkidle");
await expect(hdrStatus).toHaveText("HDR updated: direct 0 & indirect 0");
await expect(input).toHaveValue("stateful");
expect(page.errors).toEqual([]);
// non-route: HDR for direct dependency
await edit("app/direct-hdr-dep.ts", (contents) =>
contents.replace("direct 0 &", "direct 1 &")
);
await page.waitForLoadState("networkidle");
await expect(hdrStatus).toHaveText("HDR updated: direct 1 & indirect 0");
await expect(input).toHaveValue("stateful");
expect(page.errors).toEqual([]);
// non-route: HDR for indirect dependency
await edit("app/indirect-hdr-dep.ts", (contents) =>
contents.replace("indirect 0", "indirect 1")
);
await page.waitForLoadState("networkidle");
await expect(hdrStatus).toHaveText("HDR updated: direct 1 & indirect 1");
await expect(input).toHaveValue("stateful");
expect(page.errors).toEqual([]);
// everything everywhere all at once
await Promise.all([
edit("app/routes/_index.tsx", (contents) =>
contents
.replace("HMR updated: 2", "HMR updated: 3")
.replace("HDR updated: ", "HDR updated: route & ")
),
edit("app/component.tsx", (contents) =>
contents.replace("Component HMR: 1", "Component HMR: 2")
),
edit("app/direct-hdr-dep.ts", (contents) =>
contents.replace("direct 1 &", "direct 2 &")
),
edit("app/indirect-hdr-dep.ts", (contents) =>
contents.replace("indirect 1", "indirect 2")
),
]);
await page.waitForLoadState("networkidle");
await expect(hmrStatus).toHaveText("HMR updated: 3");
await expect(component).toHaveText("Component HMR: 2");
await expect(hdrStatus).toHaveText(
"HDR updated: route & direct 2 & indirect 2"
);
await expect(input).toHaveValue("stateful");
expect(page.errors).toEqual([]);
}