-
Notifications
You must be signed in to change notification settings - Fork 750
Expand file tree
/
Copy pathbuild_test.ts
More file actions
913 lines (798 loc) · 26.5 KB
/
build_test.ts
File metadata and controls
913 lines (798 loc) · 26.5 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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
import { createLogger } from "vite";
import type { BabelFileResult } from "@babel/core";
import { expect } from "@std/expect";
import { walk } from "@std/fs/walk";
import {
waitFor,
waitForText,
withBrowser,
} from "../../fresh/tests/test_utils.tsx";
import {
buildVite,
DEMO_DIR,
FIXTURE_DIR,
integrationTest,
launchProd,
usingEnv,
} from "./test_utils.ts";
import { toPosix } from "fresh/internal-dev";
import * as path from "@std/path";
import { FRESH_CSS_PLACEHOLDER } from "../src/plugins/server_snapshot.ts";
const viteResult = await buildVite(DEMO_DIR);
const NON_ISLAND_CSS_MODULES_FIXTURE = path.join(
FIXTURE_DIR,
"non_island_css_modules",
);
integrationTest("vite build - launches", async () => {
await launchProd(
{ cwd: viteResult.tmp },
async (address) => {
const res = await fetch(address);
const text = await res.text();
expect(text).toEqual("it works");
},
);
});
integrationTest("vite build - creates compiled entry", async () => {
const stat = await Deno.stat(
path.join(viteResult.tmp, "_fresh", "compiled-entry.js"),
);
expect(stat.isFile).toEqual(true);
});
integrationTest("vite build - serves static files", async () => {
await launchProd(
{ cwd: viteResult.tmp },
async (address) => {
const res = await fetch(`${address}/test_static/foo.txt`);
const text = await res.text();
expect(text).toEqual("it works");
const resWithSpace = await fetch(
`${address}/test%20%2520encodeUri/foo%20%2520encodeUri.txt`,
);
const textWithSpace = await resWithSpace.text();
expect(textWithSpace).toEqual("space it works");
},
);
});
integrationTest("vite build - loads islands", async () => {
await launchProd(
{ cwd: viteResult.tmp },
async (address) => {
await withBrowser(async (page) => {
await page.goto(`${address}/tests/island_hooks`, {
waitUntil: "networkidle2",
});
await waitForText(page, "button", "count: 0");
await page.locator("button").click();
await waitForText(page, "button", "count: 1");
});
},
);
});
integrationTest("vite build - nested islands", async () => {
await launchProd(
{ cwd: viteResult.tmp },
async (address) => {
await withBrowser(async (page) => {
await page.goto(`${address}/tests/island_nested`, {
waitUntil: "networkidle2",
});
await page.locator(".outer-ready").wait();
await page.locator(".inner-ready").wait();
});
},
);
});
integrationTest("vite build - without static/ dir", async () => {
const fixture = path.join(FIXTURE_DIR, "no_static");
await using res = await buildVite(fixture);
await launchProd(
{ cwd: res.tmp },
async (address) => {
const res = await fetch(`${address}/ok`);
const text = await res.text();
expect(text).toEqual("ok");
},
);
});
integrationTest("vite build - without islands/ dir", async () => {
const fixture = path.join(FIXTURE_DIR, "no_islands");
await using res = await buildVite(fixture);
await launchProd(
{ cwd: res.tmp },
async (address) => {
const res = await fetch(`${address}`);
const text = await res.text();
expect(text).toContain("ok");
},
);
});
integrationTest("vite build - without routes/ dir", async () => {
const fixture = path.join(FIXTURE_DIR, "no_routes");
await using res = await buildVite(fixture);
await launchProd(
{ cwd: res.tmp },
async (address) => {
const res = await fetch(`${address}`);
const text = await res.text();
expect(text).toEqual("ok");
},
);
});
integrationTest("vite build - load json inside npm package", async () => {
await launchProd(
{ cwd: viteResult.tmp },
async (address) => {
await withBrowser(async (page) => {
await page.goto(`${address}/tests/mime`, {
waitUntil: "networkidle2",
});
await page.locator(".ready").wait();
});
},
);
});
integrationTest("vite build - fetch static assets", async () => {
await launchProd(
{ cwd: viteResult.tmp },
async (address) => {
await withBrowser(async (page) => {
await page.goto(`${address}/tests/assets`, {
waitUntil: "networkidle2",
});
const url = await page.locator("img").evaluate((el) =>
// deno-lint-ignore no-explicit-any
(el as any).src
);
const res = await fetch(url);
await res.body?.cancel();
expect(res.status).toEqual(200);
expect(res.headers.get("Content-Type")).toEqual("image/png");
});
},
);
});
integrationTest("vite build - tailwind no _app", async () => {
const fixture = path.join(FIXTURE_DIR, "tailwind_no_app");
await using res = await buildVite(fixture);
await launchProd(
{ cwd: res.tmp },
async (address) => {
await withBrowser(async (page) => {
await page.goto(`${address}`, {
waitUntil: "networkidle2",
});
const href = await page
.locator("link[rel='stylesheet']")
.evaluate((el) => {
// deno-lint-ignore no-explicit-any
return (el as any).href;
});
expect(href).toMatch(/\/assets\/client-entry-.*\.css(\?.*)?$/);
});
},
);
});
integrationTest("vite build - tailwind _app", async () => {
const fixture = path.join(FIXTURE_DIR, "tailwind_app");
await using res = await buildVite(fixture);
await launchProd(
{ cwd: res.tmp },
async (address) => {
await withBrowser(async (page) => {
await page.goto(`${address}`, {
waitUntil: "networkidle2",
});
const href = await page
.locator("link[rel='stylesheet']")
.evaluate((el) => {
// deno-lint-ignore no-explicit-any
return (el as any).href;
});
expect(href).toMatch(/\/assets\/client-entry-.*\.css/);
});
},
);
});
integrationTest("vite build - partial island", async () => {
await launchProd(
{ cwd: viteResult.tmp },
async (address) => {
await withBrowser(async (page) => {
await page.goto(`${address}/tests/partial`, {
waitUntil: "networkidle2",
});
await page.locator(".ready").wait();
await page.locator("a").click();
await page.locator(".counter-hooks").wait();
await page.locator(".counter-hooks button").click();
await waitForText(page, ".counter-hooks button", "count: 1");
});
},
);
});
integrationTest(
"vite build - build ID uses env variables when set",
async () => {
const revision = "test-commit-hash-123";
// We're running on GitHub Actions, so GITHUB_SHA will always
// be set
Deno.env.delete("GITHUB_SHA");
for (
const key of [
"DENO_DEPLOYMENT_ID",
"GITHUB_SHA",
"CI_COMMIT_SHA",
"OTHER",
]
) {
using _ = usingEnv(key, revision);
await using res = await buildVite(DEMO_DIR);
await launchProd(
{ cwd: res.tmp },
async (address) => {
const res = await fetch(`${address}/tests/build_id`);
const text = await res.text();
if (key === "OTHER") {
expect(text).not.toEqual(revision);
} else {
expect(text).toEqual(revision);
}
},
);
}
},
);
integrationTest(
"vite build - import json from jsr dependency",
async () => {
await launchProd(
{ cwd: viteResult.tmp },
async (address) => {
const res = await fetch(`${address}/tests/dep_json`);
const json = await res.json();
expect(json.name).toEqual("@marvinh-test/import-json");
},
);
},
);
integrationTest("vite build - import node:*", async () => {
await launchProd(
{ cwd: viteResult.tmp },
async (address) => {
const res = await fetch(`${address}/tests/feed`);
await res.body?.cancel();
expect(res.status).toEqual(200);
},
);
});
integrationTest("vite build - css modules", async () => {
await launchProd(
{ cwd: viteResult.tmp },
async (address) => {
await withBrowser(async (page) => {
await page.goto(`${address}/tests/css_modules`, {
waitUntil: "networkidle2",
});
let color = await page
.locator(".red > h1")
// deno-lint-ignore no-explicit-any
.evaluate((el) => window.getComputedStyle(el as any).color);
expect(color).toEqual("rgb(255, 0, 0)");
color = await page
.locator(".green > h1")
// deno-lint-ignore no-explicit-any
.evaluate((el) => window.getComputedStyle(el as any).color);
expect(color).toEqual("rgb(0, 128, 0)");
color = await page
.locator(".blue > h1")
// deno-lint-ignore no-explicit-any
.evaluate((el) => window.getComputedStyle(el as any).color);
expect(color).toEqual("rgb(0, 0, 255)");
// Route css
color = await page
.locator(".route > h1")
// deno-lint-ignore no-explicit-any
.evaluate((el) => window.getComputedStyle(el as any).color);
expect(color).toEqual("rgb(255, 218, 185)");
});
},
);
});
// Issue: https://github.com/denoland/fresh/issues/3633
integrationTest(
"vite build - css modules in _app.tsx island are injected",
async () => {
await launchProd(
{ cwd: viteResult.tmp },
async (address) => {
await withBrowser(async (page) => {
await page.goto(`${address}/tests/css_modules`, {
waitUntil: "networkidle2",
});
// The AppNav island is in _app.tsx and uses a CSS module.
// Its styles should be injected even though the island
// is discovered after <head> renders.
const bgColor = await page
.locator(".app-nav")
.evaluate((el) =>
window.getComputedStyle(el as Element).backgroundColor
);
expect(bgColor).toEqual("rgb(30, 30, 30)");
});
},
);
},
);
// Issue: https://github.com/denoland/fresh/issues/3633
integrationTest(
"vite build - css modules work on second page with shared island",
async () => {
await launchProd(
{ cwd: viteResult.tmp },
async (address) => {
await withBrowser(async (page) => {
// Access the second page that shares the CssModules island
await page.goto(`${address}/tests/css_modules_page2`, {
waitUntil: "networkidle2",
});
// The shared CssModules island's CSS should work here too
const color = await page
.locator(".red > h1")
// deno-lint-ignore no-explicit-any
.evaluate((el) => window.getComputedStyle(el as any).color);
expect(color).toEqual("rgb(255, 0, 0)");
});
},
);
},
);
integrationTest(
"vite build - css modules in _app/_layout/_error non-island component are injected",
async () => {
await using res = await buildVite(NON_ISLAND_CSS_MODULES_FIXTURE);
await launchProd(
{ cwd: res.tmp },
async (address) => {
await withBrowser(async (page) => {
{
// check _app/_layout
await page.goto(`${address}`, {
waitUntil: "networkidle2",
});
const _app = await page
.locator<HTMLHeadingElement>(".green > h1")
.evaluate((el) => window.getComputedStyle(el).color);
expect(_app).toEqual("rgb(0, 128, 0)");
const _layout = await page
.locator<HTMLHeadingElement>(".red > h1")
.evaluate((el) => window.getComputedStyle(el).color);
expect(_layout).toEqual("rgb(255, 0, 0)");
}
{
// check _app/_layout/_error
await page.goto(`${address}/boom`, {
waitUntil: "networkidle2",
});
const _app = await page
.locator<HTMLHeadingElement>(".green > h1")
.evaluate((el) => window.getComputedStyle(el).color);
expect(_app).toEqual("rgb(0, 128, 0)");
const _layout = await page
.locator<HTMLHeadingElement>(".red > h1")
.evaluate((el) => window.getComputedStyle(el).color);
expect(_layout).toEqual("rgb(255, 0, 0)");
const _error = await page
.locator<HTMLHeadingElement>(".blue > h1")
.evaluate((el) => window.getComputedStyle(el).color);
expect(_error).toEqual("rgb(0, 0, 255)");
}
{
// check _app/_layout/_404
await page.goto(`${address}/non_existent`, {
waitUntil: "networkidle2",
});
const _app = await page
.locator<HTMLHeadingElement>(".green > h1")
.evaluate((el) => window.getComputedStyle(el).color);
expect(_app).toEqual("rgb(0, 128, 0)");
const _layout = await page
.locator<HTMLHeadingElement>(".red > h1")
.evaluate((el) => window.getComputedStyle(el).color);
expect(_layout).toEqual("rgb(255, 0, 0)");
const _404 = await page
.locator<HTMLHeadingElement>(".orange > h1")
.evaluate((el) => window.getComputedStyle(el).color);
expect(_404).toEqual("rgb(255, 165, 0)");
}
});
},
);
},
);
integrationTest("vite build - route css import", async () => {
await launchProd(
{ cwd: viteResult.tmp },
async (address) => {
await withBrowser(async (page) => {
await page.goto(`${address}/tests/css`, {
waitUntil: "networkidle2",
});
await waitFor(async () => {
const color = await page
.locator("h1")
// deno-lint-ignore no-explicit-any
.evaluate((el) => window.getComputedStyle(el as any).color);
expect(color).toEqual("rgb(255, 0, 0)");
return true;
});
});
},
);
});
integrationTest(
"vite build - __FRESH_CSS_PLACEHOLDER__ has been replaced in all server chunks",
async () => {
await using res = await buildVite(NON_ISLAND_CSS_MODULES_FIXTURE, {
base: "/my-app/",
});
const serverDir = path.join(res.tmp, "_fresh", "server");
const inspected: string[] = [];
for await (
const entry of walk(serverDir, {
exts: [".mjs"],
includeDirs: false,
})
) {
const content = await Deno.readTextFile(entry.path);
inspected.push(path.relative(serverDir, entry.path));
expect(content).not.toContain(FRESH_CSS_PLACEHOLDER);
}
expect(inspected.length).toBeGreaterThan(0);
},
);
integrationTest(
"vite build - shared server chunk keeps utility css references after replacement",
async () => {
await using res = await buildVite(NON_ISLAND_CSS_MODULES_FIXTURE);
const serverEntryJs = await Deno.readTextFile(
path.join(res.tmp, "_fresh", "server", "server-entry.mjs"),
);
const cssRefs = serverEntryJs.match(/"\/assets\/server-entry-.*?\.css"/g) ??
[];
expect(cssRefs.length).toBeGreaterThanOrEqual(4);
},
);
integrationTest("vite build - remote island", async () => {
const fixture = path.join(FIXTURE_DIR, "remote_island");
await using res = await buildVite(fixture);
await launchProd(
{ cwd: res.tmp },
async (address) => {
await withBrowser(async (page) => {
await page.goto(`${address}`, {
waitUntil: "networkidle2",
});
await page.locator(".remote-island").wait();
await page.locator(".increment").click();
await waitForText(page, ".result", "Count: 1");
});
},
);
});
integrationTest(
"vite build - error on 'node:process' import",
async () => {
const fixture = path.join(FIXTURE_DIR, "node_builtin");
await expect(buildVite(fixture)).rejects.toThrow(
"Node built-in modules cannot be imported in the browser",
);
},
);
integrationTest("vite build - static index.html", async () => {
await launchProd(
{ cwd: viteResult.tmp },
async (address) => {
const res = await fetch(`${address}/test_static/foo`);
const text = await res.text();
expect(text).toContain("<h1>ok</h1>");
const resWithSpace = await fetch(`${address}/test%20%2520encodeUri`);
const textWithSpace = await resWithSpace.text();
expect(textWithSpace).toContain("<h1>ok</h1>");
},
);
});
integrationTest("vite build - base path asset handling", async () => {
await using res = await buildVite(DEMO_DIR, { base: "/my-app/" });
// Read the generated server.js to check asset paths
const serverJs = await Deno.readTextFile(
path.join(res.tmp, "_fresh", "server.js"),
);
// Asset paths should include the base path /my-app/
expect(serverJs).toContain('"/my-app/assets/');
});
integrationTest(
"vite build - custom rollup entryFileNames in server.js",
async () => {
await using res = await buildVite(DEMO_DIR, {
build: {
rollupOptions: {
output: {
entryFileNames: "[hash].mjs",
chunkFileNames: "[hash].mjs",
},
},
},
});
const serverJs = await Deno.readTextFile(
path.join(res.tmp, "_fresh", "server.js"),
);
// When custom entryFileNames is set, server.js should use the actual
// hashed filename from the manifest, not hardcoded "server-entry.mjs"
expect(serverJs).not.toContain("server-entry.mjs");
expect(serverJs).toMatch(
/from "\.\/server\/[a-zA-Z0-9_-]+\.mjs"/,
);
},
);
integrationTest("vite build - env files", async () => {
await launchProd(
{ cwd: viteResult.tmp },
async (address) => {
const res = await fetch(`${address}/tests/env_files`);
const json = await res.json();
expect(json).toEqual({
MY_ENV: "MY_ENV test value",
VITE_MY_ENV: "VITE_MY_ENV test value",
MY_LOCAL_ENV: "MY_LOCAL_ENV test value",
VITE_MY_LOCAL_ENV: "VITE_MY_LOCAL_ENV test value",
});
},
);
});
integrationTest("vite build - support _middleware Array", async () => {
await launchProd(
{ cwd: viteResult.tmp },
async (address) => {
const res = await fetch(`${address}/tests/middlewares`);
const text = await res.text();
expect(text).toEqual("AB");
},
);
});
integrationTest(
"vite build - island named after global object (Map)",
async () => {
const fixture = path.join(FIXTURE_DIR, "island_global_name");
await using res = await buildVite(fixture);
await launchProd(
{ cwd: res.tmp },
async (address) => {
const response = await fetch(address);
const html = await response.text();
// Verify the fix: UniqueNamer prefixes "Map" with underscore to prevent shadowing
// Without the fix: import Map from "..." and boot({Map}, ...) - shadows global Map
// With the fix: import _Map_N from "..." and boot({_Map_N}, ...) - no shadowing
expect(html).toMatch(/import.*_Map(_\d+)?.*from/);
expect(html).toMatch(/boot\(\s*\{\s*_Map(_\d+)?\s*\}/);
},
);
},
);
integrationTest(
"vite build - excludes test files from routes",
async () => {
const fixture = path.join(FIXTURE_DIR, "test_files_exclusion");
await using res = await buildVite(fixture);
// Verify that test files in routes/ are not bundled
const serverAssetsDir = path.join(
res.tmp,
"_fresh",
"server",
"assets",
);
// List all compiled route files
const files: string[] = [];
for await (const entry of Deno.readDir(serverAssetsDir)) {
if (entry.isFile && entry.name.startsWith("_fresh-route")) {
files.push(entry.name);
}
}
// Should have index route but NOT test files
expect(files.some((f) => f.includes("_fresh-route___index"))).toBe(true);
expect(files.some((f) => f.includes("index_test"))).toBe(false);
expect(files.some((f) => f.includes("foo.test"))).toBe(false);
// Verify no test pattern files at all
// Note: files with "_test" or ".test" in their name (not just a "tests" folder)
const testPatterns = ["_test.", "_test-", ".test."];
for (const file of files) {
for (const pattern of testPatterns) {
expect(file.includes(pattern)).toBe(false);
}
}
},
);
integrationTest("vite build - client side <Head>", async () => {
await launchProd(
{ cwd: viteResult.tmp },
async (address) => {
await withBrowser(async (page) => {
await page.goto(`${address}/tests/head_counter`, {
waitUntil: "networkidle2",
});
await page.locator(".ready").wait();
await page.locator("button").click();
await waitForText(page, ".result", "Count: 1");
await waitFor(async () => {
const title = await page.evaluate(() => document.title);
expect(title).toEqual("Count: 1");
return true;
});
});
},
);
});
integrationTest(
"vite build - vite-plugin-pwa generates service worker",
async () => {
const fixture = path.join(FIXTURE_DIR, "vite_plugin_pwa");
await using res = await buildVite(fixture);
// Verify that vite-plugin-pwa generated the expected files in _fresh/client
const swPath = path.join(res.tmp, "_fresh", "client", "sw.js");
const manifestPath = path.join(
res.tmp,
"_fresh",
"client",
"manifest.webmanifest",
);
// Check that files were generated
const swStat = await Deno.stat(swPath);
expect(swStat.isFile).toEqual(true);
const manifestStat = await Deno.stat(manifestPath);
expect(manifestStat.isFile).toEqual(true);
},
);
integrationTest(
"vite build - vite-plugin-pwa files are accessible via HTTP",
async () => {
const fixture = path.join(FIXTURE_DIR, "vite_plugin_pwa");
await using res = await buildVite(fixture);
await launchProd(
{ cwd: res.tmp },
async (address) => {
// Test that service worker is accessible
const swRes = await fetch(`${address}/sw.js`);
expect(swRes.status).toEqual(200);
expect(swRes.headers.get("content-type")).toMatch(/javascript/);
const swContent = await swRes.text();
expect(swContent.length).toBeGreaterThan(0);
// Test that manifest is accessible
const manifestRes = await fetch(`${address}/manifest.webmanifest`);
expect(manifestRes.status).toEqual(200);
expect(manifestRes.headers.get("content-type")).toMatch(/json/);
const manifestContent = await manifestRes.json();
expect(manifestContent.name).toEqual("Fresh PWA Test");
// Test that registerSW.js is accessible
const registerRes = await fetch(`${address}/registerSW.js`);
expect(registerRes.status).toEqual(200);
expect(registerRes.headers.get("content-type")).toMatch(/javascript/);
},
);
},
);
integrationTest(
"vite build - asset cache headers on CSS and JS",
async () => {
await launchProd(
{ cwd: viteResult.tmp },
async (address) => {
// Fetch a page with islands to get CSS and JS asset URLs
const res = await fetch(`${address}/tests/island_hooks`);
const html = await res.text();
// CSS link tags should get immutable cache headers
const cssMatches = html.matchAll(
/href="(\/assets\/[^"]*\.css[^"]*)"/g,
);
for (const match of cssMatches) {
const href = match[1];
const cssRes = await fetch(`${address}${href}`);
await cssRes.body?.cancel();
expect(cssRes.status).toEqual(200);
expect(cssRes.headers.get("Cache-Control")).toEqual(
"public, max-age=31536000, immutable",
);
}
// JS module imports should get immutable cache headers
const scriptMatch = html.match(
/<script[^>]*type="module"[^>]*>([\s\S]*?)<\/script>/,
);
expect(scriptMatch).not.toBeNull();
const scriptContent = scriptMatch![1];
const importMatches = scriptContent.matchAll(
/from "([^"]+)"/g,
);
for (const match of importMatches) {
const url = match[1];
if (url.startsWith("/assets/") || url.includes("/assets/")) {
const jsRes = await fetch(`${address}${url}`);
await jsRes.body?.cancel();
expect(jsRes.status).toEqual(200);
expect(jsRes.headers.get("Cache-Control")).toEqual(
"public, max-age=31536000, immutable",
);
}
}
},
);
},
);
integrationTest(
"vite build - ssr sourcemap should be generated collectly",
async () => {
await using tmp = await buildVite(DEMO_DIR, {
environments: {
ssr: {
build: {
sourcemap: true,
},
},
},
});
const serverAssetsDir = path.join(tmp.tmp, "_fresh", "server", "assets");
for await (
const entry of walk(serverAssetsDir, {
exts: [".mjs"],
includeDirs: false,
})
) {
const js = await Deno.readTextFile(entry.path);
const match = js.match(/\/\/# sourceMappingURL=(.+)$/m);
const mapPath = path.join(path.dirname(entry.path), match![1]);
const mapText = await Deno.readTextFile(mapPath);
const map: NonNullable<BabelFileResult["map"]> = JSON.parse(mapText);
// check a specific sourcemap file which contains
// the reference of original source file
if (entry.name.includes("_fresh-route___tests_feed-")) {
expect(
map.sources.some((source) =>
toPosix(source).endsWith("demo/routes/tests/feed.tsx")
),
).toBe(true);
}
}
},
);
// rollup specific test
// https://rollupjs.org/troubleshooting/#warning-sourcemap-is-likely-to-be-incorrect
// this test could be broke if it will migrate to rolldown
integrationTest(
"vite build - ssr sourcemap should be generated without warnings",
async () => {
const warnMsgs = new Set<string>();
const customLogger = createLogger("error");
customLogger.warn = (msg) => {
customLogger.hasWarned = true;
warnMsgs.add(msg);
};
customLogger.warnOnce = (msg) => {
customLogger.hasWarned = true;
warnMsgs.add(msg);
};
await using _ = await buildVite(DEMO_DIR, {
logLevel: "warn",
clearScreen: true,
customLogger,
environments: {
ssr: {
build: {
sourcemap: true,
},
},
},
});
const sourceMapIncorrectMsg = warnMsgs.has(
"[plugin deno] Sourcemap is likely to be incorrect: a plugin (deno) was used to transform files, but didn't generate a sourcemap for the transformation. Consult the plugin documentation for help",
);
expect(sourceMapIncorrectMsg).not.toBeTruthy();
},
);