-
Notifications
You must be signed in to change notification settings - Fork 595
/
hmr_test.ts
107 lines (94 loc) · 3.01 KB
/
hmr_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
import {
basename,
copy,
delay,
dirname,
join,
Page,
puppeteer,
relative,
} from "./deps.ts";
import { startFreshServer } from "./test_utils.ts";
import { retry } from "$std/async/retry.ts";
async function withTmpFixture(
name: string,
fn: (page: Page, address: string, dir: string) => Promise<void>,
) {
const tmp = join(await Deno.makeTempDir(), "project");
const src = join(Deno.cwd(), dirname(name));
await copy(src, tmp);
// Update Fresh path
const denoJsonPath = join(tmp, "deno.json");
const json = JSON.parse(await Deno.readTextFile(denoJsonPath));
json.imports["$fresh/"] = relative(denoJsonPath, Deno.cwd()) + "/";
await Deno.writeTextFile(denoJsonPath, JSON.stringify(json, null, 2));
// Watcher tests tend to be flaky
await retry(async () => {
const { lines, serverProcess, address } = await startFreshServer({
args: ["run", "-A", "--watch", join(tmp, basename(name))],
env: {
"FRESH_ESBUILD_LOADER": "portable",
},
});
try {
await delay(100);
const browser = await puppeteer.launch({ args: ["--no-sandbox"] });
try {
const page = await browser.newPage();
await fn(page, address, tmp);
} finally {
await browser.close();
}
} finally {
serverProcess.kill("SIGTERM");
// Wait until the process exits
await serverProcess.status;
// Drain the lines stream
for await (const _ of lines) { /* noop */ }
}
}, { maxAttempts: 3 });
}
Deno.test({
name: "page reloads with no island present",
// Watcher tests are pretty flaky in CI and non-UNIX systems.
// Until we know why, we'll gate these tests behind an
// environment variable.
ignore: !Deno.env.has("FRESH_WATCH_TESTS"),
fn: async () => {
await withTmpFixture(
"./tests/fixture_hmr/dev.ts",
async (page, address, dir) => {
await page.goto(`${address}/no_island`);
await page.waitForSelector("h1");
// Trigger file change
const file = join(dir, "routes", "no_island.tsx");
const text = await Deno.readTextFile(file);
await Deno.writeTextFile(file, text.replaceAll("foo", "bar"));
await page.waitForSelector(".bar");
},
);
},
});
Deno.test({
name: "page reloads with island",
// Watcher tests are pretty flaky in CI and non-UNIX systems.
// Until we know why, we'll gate these tests behind an
// environment variable.
ignore: !Deno.env.has("FRESH_WATCH_TESTS"),
fn: async () => {
await withTmpFixture(
"./tests/fixture_hmr/dev.ts",
async (page, address, dir) => {
await page.goto(`${address}/island`);
await page.waitForSelector("h1");
// Trigger file change
const file = join(dir, "routes", "island.tsx");
const text = await Deno.readTextFile(file);
await Deno.writeTextFile(file, text.replaceAll("foo", "bar"));
await page.waitForSelector(".bar");
// TODO: Once we support proper HMR check that state stays
// the same
},
);
},
});