-
-
Notifications
You must be signed in to change notification settings - Fork 241
Expand file tree
/
Copy pathbasic.test.ts
More file actions
1514 lines (1346 loc) · 48.1 KB
/
basic.test.ts
File metadata and controls
1514 lines (1346 loc) · 48.1 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
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { createHash } from 'node:crypto'
import { readFileSync } from 'node:fs'
import { type Page, expect, test } from '@playwright/test'
import { type Fixture, useCreateEditor, useFixture } from './fixture'
import {
expectNoPageError,
expectNoReload,
testNoJs,
waitForHydration,
} from './helper'
import { x } from 'tinyexec'
import { normalizePath, type Rollup } from 'vite'
import path from 'node:path'
test.describe('dev-default', () => {
const f = useFixture({ root: 'examples/basic', mode: 'dev' })
defineTest(f)
})
test.describe('dev-initial', () => {
const f = useFixture({ root: 'examples/basic', mode: 'dev' })
// verify css is collected properly on server startup (i.e. empty module graph)
testNoJs('style', async ({ page }) => {
await page.goto(f.url('./'))
await expect(page.locator('.test-style-client')).toHaveCSS(
'color',
'rgb(255, 165, 0)',
)
await expect(page.locator('.test-style-server')).toHaveCSS(
'color',
'rgb(255, 165, 0)',
)
await expect(page.locator('.test-tw-client')).toHaveCSS(
'color',
// blue-500
'rgb(0, 0, 255)',
)
await expect(page.locator('.test-tw-server')).toHaveCSS(
'color',
// red-500
'rgb(255, 0, 0)',
)
})
})
test.describe('build-default', () => {
const f = useFixture({ root: 'examples/basic', mode: 'build' })
defineTest(f)
test('server-chunk-based client chunks', async () => {
const { chunks }: { chunks: Rollup.OutputChunk[] } = JSON.parse(
f.createEditor('dist/client/.vite/test.json').read(),
)
const expectedGroups = {
'facade:src/routes/chunk2/client1.tsx': ['src/routes/chunk2/client1.tsx'],
'facade:src/routes/chunk2/server2.tsx': [
'src/routes/chunk2/client2.tsx',
'src/routes/chunk2/client2b.tsx',
],
'shared:src/routes/chunk2/client3.tsx': ['src/routes/chunk2/client3.tsx'],
}
const actualGroups: Record<string, string[]> = {}
for (const key in expectedGroups) {
const groupId = `\0virtual:vite-rsc/client-references/group/${key}`
const groupChunk = chunks.find((c) => c.facadeModuleId === groupId)
if (groupChunk) {
actualGroups[key] = groupChunk.moduleIds
.filter((id) => id !== groupId)
.map((id) => normalizePath(path.relative(f.root, id)))
}
}
expect(actualGroups).toEqual(expectedGroups)
})
})
test.describe('custom-client-chunks', () => {
const f = useFixture({
root: 'examples/basic',
mode: 'build',
cliOptions: {
env: {
TEST_CUSTOM_CLIENT_CHUNKS: 'true',
},
},
})
test('basic', async () => {
const { chunks }: { chunks: Rollup.OutputChunk[] } = JSON.parse(
f.createEditor('dist/client/.vite/test.json').read(),
)
const chunk = chunks.find((c) => c.name === 'custom-chunk')
const expected = [1, 2, 3].map((i) =>
normalizePath(path.join(f.root, `src/routes/chunk/client${i}.tsx`)),
)
expect(chunk?.moduleIds).toEqual(expect.arrayContaining(expected))
})
})
test.describe('dev-non-optimized-cjs', () => {
test.beforeAll(async () => {
// remove explicitly added optimizeDeps.include
const editor = f.createEditor('vite.config.ts')
editor.edit((s) =>
s.replace(
`include: ['@vitejs/test-dep-transitive-cjs > @vitejs/test-dep-cjs'],`,
``,
),
)
})
const f = useFixture({
root: 'examples/basic',
mode: 'dev',
cliOptions: {
env: {
DEBUG: 'vite-rsc:cjs',
},
},
})
test('show warning', async ({ page }) => {
await page.goto(f.url())
expect(f.proc().stderr()).toMatch(
/non-optimized CJS dependency in 'ssr' environment.*@vitejs\/test-dep-cjs\/index.js/,
)
})
})
test.describe('dev-inconsistent-client-optimization', () => {
test.beforeAll(async () => {
// remove explicitly added optimizeDeps.exclude
const editor = f.createEditor('vite.config.ts')
editor.edit((s) =>
s.replace(`'@vitejs/test-dep-client-in-server2/client',`, ``),
)
})
const f = useFixture({
root: 'examples/basic',
mode: 'dev',
})
test('show warning', async ({ page }) => {
await page.goto(f.url())
expect(f.proc().stderr()).toContain(
'client component dependency is inconsistently optimized.',
)
})
})
test.describe('build-stable-chunks', () => {
const root = 'examples/basic'
const createEditor = useCreateEditor(root)
test('basic', async () => {
// 1st build
await x('pnpm', ['build'], {
throwOnError: true,
nodeOptions: {
cwd: root,
},
})
const manifest1: import('vite').Manifest = JSON.parse(
createEditor('dist/client/.vite/manifest.json').read(),
)
// edit src/routes/client.tsx
const editor = createEditor('src/routes/client.tsx')
editor.edit((s) => s.replace('client-counter', 'client-counter-v2'))
// 2nd build
await x('pnpm', ['build'], {
throwOnError: true,
nodeOptions: {
cwd: root,
},
})
const manifest2: import('vite').Manifest = JSON.parse(
createEditor('dist/client/.vite/manifest.json').read(),
)
// compare two mainfest.json
const files1 = new Set(Object.values(manifest1).map((v) => v.file))
const files2 = new Set(Object.values(manifest2).map((v) => v.file))
const oldChunks = Object.entries(manifest2)
.filter(([_k, v]) => !files1.has(v.file))
.map(([k]) => k)
.sort()
const newChunks = Object.entries(manifest1)
.filter(([_k, v]) => !files2.has(v.file))
.map(([k]) => k)
.sort()
expect(newChunks).toEqual([
'src/framework/entry.browser.tsx',
'virtual:vite-rsc/client-references/group/facade:src/routes/root.tsx',
])
expect(oldChunks).toEqual(newChunks)
})
})
function defineTest(f: Fixture) {
test('basic', async ({ page }) => {
using _ = expectNoPageError(page)
await page.goto(f.url())
await waitForHydration(page)
expect(f.proc().stderr()).toBe('')
})
test('client component', async ({ page }) => {
await page.goto(f.url())
await waitForHydration(page)
await page.getByRole('button', { name: 'client-counter: 0' }).click()
await page.getByRole('button', { name: 'client-counter: 1' }).click()
})
test('server action @js', async ({ page }) => {
await page.goto(f.url())
await waitForHydration(page)
await using _ = await expectNoReload(page)
await testAction(page)
})
testNoJs('server action @nojs', async ({ page }) => {
await page.goto(f.url())
await testAction(page)
})
async function testAction(page: Page) {
await page.getByRole('button', { name: 'server-counter: 0' }).click()
await page.getByRole('button', { name: 'server-counter: 1' }).click()
await expect(
page.getByRole('button', { name: 'server-counter: 2' }),
).toBeVisible()
await page.getByRole('button', { name: 'server-counter-reset' }).click()
await expect(
page.getByRole('button', { name: 'server-counter: 0' }),
).toBeVisible()
}
test('useActionState @js', async ({ page }) => {
await page.goto(f.url())
await waitForHydration(page)
await using _ = await expectNoReload(page)
await testUseActionState(page)
})
testNoJs('useActionState @nojs', async ({ page }) => {
await page.goto(f.url())
await testUseActionState(page)
})
test('useActionState nojs to js', async ({ page, browserName }) => {
// firefox seems to cache html and route interception doesn't work
test.skip(browserName === 'firefox')
// this test fails without `formState` passed to `hydrateRoot(..., { formState })`
// intercept request to disable js
let js: boolean
await page.route(f.url(), async (route) => {
if (!js) {
await route.continue({ url: route.request().url() + '?__nojs' })
return
}
await route.continue()
})
// no js
js = false
await page.goto(f.url())
await expect(page.getByTestId('use-action-state')).toContainText(
'test-useActionState: 0',
)
await page.getByTestId('use-action-state').click()
await expect(page.getByTestId('use-action-state')).toContainText(
'test-useActionState: 1',
)
// with js (hydration)
js = true
await page.getByTestId('use-action-state').click()
await waitForHydration(page)
await expect(page.getByTestId('use-action-state')).toContainText(
'test-useActionState: 2', // this becomes "0" without formState
)
})
async function testUseActionState(page: Page) {
await expect(page.getByTestId('use-action-state')).toContainText(
'test-useActionState: 0',
)
await page.getByTestId('use-action-state').click()
await expect(page.getByTestId('use-action-state')).toContainText(
'test-useActionState: 1',
)
await page.getByTestId('use-action-state').click()
await expect(page.getByTestId('use-action-state')).toContainText(
'test-useActionState: 2',
)
}
test('useActionState with jsx @js', async ({ page }) => {
await page.goto(f.url())
await waitForHydration(page)
await using _ = await expectNoReload(page)
await testUseActionStateJsx(page)
})
testNoJs('useActionState with jsx @nojs', async ({ page }) => {
await page.goto(f.url())
await testUseActionStateJsx(page, { js: false })
})
async function testUseActionStateJsx(page: Page, options?: { js?: boolean }) {
await page.getByTestId('use-action-state-jsx').getByRole('button').click()
await expect(page.getByTestId('use-action-state-jsx')).toContainText(
/\(ok\)/,
)
// 1st call "works" but it shows an error during reponse and it breaks 2nd call.
// Failed to serialize an action for progressive enhancement:
// Error: React Element cannot be passed to Server Functions from the Client without a temporary reference set. Pass a TemporaryReferenceSet to the options.
// [Promise, <span/>]
if (!options?.js) return
await page.getByTestId('use-action-state-jsx').getByRole('button').click()
await expect(page.getByTestId('use-action-state-jsx')).toContainText(
/\(ok\).*\(ok\)/,
)
}
test.describe(() => {
test.skip(f.mode !== 'build')
testNoJs('module preload on ssr', async ({ page }) => {
await page.goto(f.url())
const srcs = await page
.locator(`head >> link[rel="modulepreload"]`)
.evaluateAll((elements) =>
elements.map((el) => el.getAttribute('href')),
)
const manifest = JSON.parse(
readFileSync(
f.root + '/dist/ssr/__vite_rsc_assets_manifest.js',
'utf-8',
).slice('export default '.length),
)
const hashString = (v: string) =>
createHash('sha256').update(v).digest().toString('hex').slice(0, 12)
const deps =
manifest.clientReferenceDeps[hashString('src/routes/client.tsx')]
expect(srcs).toEqual(expect.arrayContaining(deps.js))
})
})
test.describe(() => {
test.skip(f.mode !== 'dev')
test('server reference update @js', async ({ page }) => {
await page.goto(f.url())
await waitForHydration(page)
await using _ = await expectNoReload(page)
await testServerActionUpdate(page, { js: true })
})
test('server reference update @nojs', async ({ page }) => {
await page.goto(f.url())
await testServerActionUpdate(page, { js: false })
})
})
async function testServerActionUpdate(page: Page, options: { js: boolean }) {
await page.getByRole('button', { name: 'server-counter: 0' }).click()
await expect(
page.getByRole('button', { name: 'server-counter: 1' }),
).toBeVisible()
// update server code
const editor = f.createEditor('src/routes/action/action.tsx')
editor.edit((s) =>
s.replace('const TEST_UPDATE = 1\n', 'const TEST_UPDATE = 10\n'),
)
await expect(async () => {
if (!options.js) await page.goto(f.url())
await expect(
page.getByRole('button', { name: 'server-counter: 0' }),
).toBeVisible({ timeout: 10 })
}).toPass()
await page.getByRole('button', { name: 'server-counter: 0' }).click()
await expect(
page.getByRole('button', { name: 'server-counter: 10' }),
).toBeVisible()
editor.reset()
await expect(async () => {
if (!options.js) await page.goto(f.url())
await expect(
page.getByRole('button', { name: 'server-counter: 0' }),
).toBeVisible({ timeout: 10 })
}).toPass()
}
test.describe(() => {
test.skip(f.mode !== 'dev')
test('client hmr', async ({ page }) => {
await page.goto(f.url())
await waitForHydration(page)
await page.getByRole('button', { name: 'client-counter: 0' }).click()
await expect(
page.getByRole('button', { name: 'client-counter: 1' }),
).toBeVisible()
const editor = f.createEditor('src/routes/client.tsx')
editor.edit((s) => s.replace('client-counter', 'client-[edit]-counter'))
await expect(
page.getByRole('button', { name: 'client-[edit]-counter: 1' }),
).toBeVisible()
// check next ssr is also updated
const res = await page.goto(f.url())
expect(await res?.text()).toContain('client-[edit]-counter')
await waitForHydration(page)
editor.reset()
await page.getByRole('button', { name: 'client-counter: 0' }).click()
})
test('non-client-reference client hmr', async ({ page }) => {
await page.goto(f.url())
await waitForHydration(page)
const locator = page.getByTestId('test-hmr-client-dep')
await expect(locator).toHaveText('test-hmr-client-dep: 0[ok]')
await locator.locator('button').click()
await expect(locator).toHaveText('test-hmr-client-dep: 1[ok]')
const editor = f.createEditor('src/routes/hmr-client-dep/client-dep.tsx')
editor.edit((s) => s.replace('[ok]', '[ok-edit]'))
await expect(locator).toHaveText('test-hmr-client-dep: 1[ok-edit]')
// check next rsc payload includes current client reference and preserves state
await page.locator("a[href='?test-hmr-client-dep-re-render']").click()
await expect(
page.locator("a[href='?test-hmr-client-dep-re-render']"),
).toHaveText('re-render [ok]')
await expect(locator).toHaveText('test-hmr-client-dep: 1[ok-edit]')
// check next ssr is also updated
const res = await page.request.get(f.url(), {
headers: {
accept: 'text/html',
},
})
expect(await res?.text()).toContain('[ok-edit]')
editor.reset()
await expect(locator).toHaveText('test-hmr-client-dep: 1[ok]')
})
test('non-self-accepting client hmr', async ({ page }) => {
await page.goto(f.url())
await waitForHydration(page)
const locator = page.getByTestId('test-hmr-client-dep2')
await expect(locator).toHaveText('test-hmr-client-dep2: 0[ok]')
await locator.locator('button').click()
await expect(locator).toHaveText('test-hmr-client-dep2: 1[ok]')
const editor = f.createEditor('src/routes/hmr-client-dep2/client-dep.ts')
editor.edit((s) => s.replace('[ok]', '[ok-edit]'))
await expect(locator).toHaveText('test-hmr-client-dep2: 1[ok-edit]')
// check next rsc payload includes an updated client reference and preserves state
await page.locator("a[href='?test-hmr-client-dep2-re-render']").click()
await expect(
page.locator("a[href='?test-hmr-client-dep2-re-render']"),
).toHaveText('re-render [ok]')
await expect(locator).toHaveText('test-hmr-client-dep2: 1[ok-edit]')
// check next ssr is also updated
const res = await page.request.get(f.url(), {
headers: {
accept: 'text/html',
},
})
expect(await res?.text()).toContain('[ok-edit]')
editor.reset()
await expect(locator).toHaveText('test-hmr-client-dep2: 1[ok]')
})
test('server hmr', async ({ page }) => {
await page.goto(f.url())
await waitForHydration(page)
await using _ = await expectNoReload(page)
const editor = f.createEditor('src/routes/action/server.tsx')
editor.edit((s) => s.replace('server-counter', 'server-[edit]-counter'))
await expect(
page.getByRole('button', { name: 'server-[edit]-counter: 0' }),
).toBeVisible()
editor.reset()
await expect(
page.getByRole('button', { name: 'server-counter: 0' }),
).toBeVisible()
})
test('module invalidation', async ({ page }) => {
await page.goto(f.url())
await waitForHydration(page)
await using _ = await expectNoReload(page)
// change child module state
const locator = page.getByTestId('test-module-invalidation-server')
await expect(locator).toContainText('[dep: 0]')
locator.getByRole('button').click()
await expect(locator).toContainText('[dep: 1]')
// change parent module
const editor = f.createEditor('src/routes/module-invalidation/server.tsx')
editor.edit((s) => s.replace('[dep:', '[dep-edit:'))
// preserve child module state
await expect(locator).toContainText('[dep-edit: 1]')
editor.reset()
await expect(locator).toContainText('[dep: 1]')
})
test('shared hmr basic', async ({ page }) => {
await page.goto(f.url())
await waitForHydration(page)
await using _ = await expectNoReload(page)
// Test initial state
await expect(page.getByTestId('test-hmr-shared-server')).toContainText(
'(shared1, shared2)',
)
await expect(page.getByTestId('test-hmr-shared-client')).toContainText(
'(shared1, shared2)',
)
// Test 1: Component HMR (shared1.tsx)
const editor1 = f.createEditor('src/routes/hmr-shared/shared1.tsx')
editor1.edit((s) => s.replace('shared1', 'shared1-edit'))
// Verify both server and client components updated
await expect(page.getByTestId('test-hmr-shared-server')).toContainText(
'(shared1-edit, shared2)',
)
await expect(page.getByTestId('test-hmr-shared-client')).toContainText(
'(shared1-edit, shared2)',
)
editor1.reset()
await expect(page.getByTestId('test-hmr-shared-server')).toContainText(
'(shared1, shared2)',
)
await expect(page.getByTestId('test-hmr-shared-client')).toContainText(
'(shared1, shared2)',
)
// Test 2: Non-component HMR (shared2.tsx)
const editor2 = f.createEditor('src/routes/hmr-shared/shared2.tsx')
editor2.edit((s) => s.replace('shared2', 'shared2-edit'))
// Verify both server and client components updated
await expect(page.getByTestId('test-hmr-shared-server')).toContainText(
'(shared1, shared2-edit)',
)
await expect(page.getByTestId('test-hmr-shared-client')).toContainText(
'(shared1, shared2-edit)',
)
editor2.reset()
await expect(page.getByTestId('test-hmr-shared-server')).toContainText(
'(shared1, shared2)',
)
await expect(page.getByTestId('test-hmr-shared-client')).toContainText(
'(shared1, shared2)',
)
})
// for this use case to work, server refetch/render and client hmr needs to applied atomically
// at the same time. Next.js doesn't seem to support this either.
// https://github.com/hi-ogawa/reproductions/tree/main/next-rsc-hmr-shared-module
test('shared hmr not atomic', async ({ page }) => {
await page.goto(f.url())
await waitForHydration(page)
await expect(page.getByTestId('test-hmr-shared-atomic')).toContainText(
'ok (test-shared)',
)
// non-atomic update causes an error
const editor = f.createEditor('src/routes/hmr-shared/atomic/shared.tsx')
editor.edit((s) => s.replace('test-shared', 'test-shared-edit'))
await expect(page.getByTestId('test-hmr-shared-atomic')).toContainText(
'ErrorBoundary',
)
await page.reload()
await expect(page.getByText('ok (test-shared-edit)')).toBeVisible()
// non-atomic update causes an error
editor.reset()
await expect(page.getByTestId('test-hmr-shared-atomic')).toContainText(
'ErrorBoundary',
)
await page.reload()
await expect(page.getByText('ok (test-shared)')).toBeVisible()
})
test('hmr switch server to client', async ({ page }) => {
await page.goto(f.url())
await waitForHydration(page)
await using _ = await expectNoReload(page)
await expect(page.getByTestId('test-hmr-switch-server')).toContainText(
'(useState: false)',
)
const editor = f.createEditor('src/routes/hmr-switch/server.tsx')
editor.edit((s) => `"use client";\n` + s)
await expect(page.getByTestId('test-hmr-switch-server')).toContainText(
'(useState: true)',
)
await page.waitForTimeout(100)
editor.reset()
await expect(page.getByTestId('test-hmr-switch-server')).toContainText(
'(useState: false)',
)
})
test('hmr switch client to server', async ({ page }) => {
await page.goto(f.url())
await waitForHydration(page)
await using _ = await expectNoReload(page)
await expect(page.getByTestId('test-hmr-switch-client')).toContainText(
'(useState: true)',
)
const editor = f.createEditor('src/routes/hmr-switch/client.tsx')
editor.edit((s) => s.replace(`'use client'`, ''))
await expect(page.getByTestId('test-hmr-switch-client')).toContainText(
'(useState: false)',
)
await page.waitForTimeout(100)
editor.reset()
await expect(page.getByTestId('test-hmr-switch-client')).toContainText(
'(useState: true)',
)
})
})
test('css @js', async ({ page }) => {
await page.goto(f.url())
await waitForHydration(page)
await testCssBasic(page)
})
testNoJs('css @nojs', async ({ page }) => {
await page.goto(f.url())
await testCss(page)
})
async function testCssBasic(page: Page) {
await testCss(page)
await expect(page.locator('.test-dep-css-in-server')).toHaveCSS(
'color',
'rgb(255, 165, 0)',
)
await expect(page.locator('.test-style-server-manual')).toHaveCSS(
'color',
'rgb(255, 165, 0)',
)
await expect(page.getByTestId('css-module-client')).toHaveCSS(
'color',
'rgb(255, 165, 0)',
)
await expect(page.getByTestId('css-module-server')).toHaveCSS(
'color',
'rgb(255, 165, 0)',
)
await expect(page.locator('.test-style-url-client')).toHaveCSS(
'color',
'rgb(255, 165, 0)',
)
await expect(page.locator('.test-style-url-server')).toHaveCSS(
'color',
'rgb(255, 165, 0)',
)
}
async function testCss(page: Page, color = 'rgb(255, 165, 0)') {
await expect(page.locator('.test-style-client')).toHaveCSS('color', color)
await expect(page.locator('.test-style-server')).toHaveCSS('color', color)
}
test.describe(() => {
test.skip(f.mode !== 'dev')
test('css hmr client', async ({ page }) => {
await page.goto(f.url())
await waitForHydration(page)
await using _ = await expectNoReload(page)
const editor = f.createEditor('src/routes/style-client/client.css')
editor.edit((s) => s.replaceAll('rgb(255, 165, 0)', 'rgb(0, 165, 255)'))
await expect(page.locator('.test-style-client')).toHaveCSS(
'color',
'rgb(0, 165, 255)',
)
editor.edit((s) =>
s.replaceAll(
`color: rgb(0, 165, 255);`,
`/* color: rgb(0, 165, 255); */`,
),
)
await expect(page.locator('.test-style-client')).toHaveCSS(
'color',
'rgb(0, 0, 0)',
)
// wait longer for multiple edits
await page.waitForTimeout(100)
editor.reset()
await expect(page.locator('.test-style-client')).toHaveCSS(
'color',
'rgb(255, 165, 0)',
)
await expectNoDuplicateServerCss(page)
})
async function expectNoDuplicateServerCss(page: Page) {
// verify duplicate client-reference style link are removed
await expect(
page.locator(
'link[rel="stylesheet"][data-precedence="vite-rsc/client-reference"]',
),
).toHaveCount(0)
// await expect(
// page
// .locator(
// 'link[rel="stylesheet"][data-precedence="vite-rsc/importer-resources"]',
// )
// .nth(0),
// ).toBeAttached()
await expect(
page
.locator(
'link[rel="stylesheet"][data-precedence="test-style-manual-link"]',
)
.nth(0),
).toBeAttached()
}
test('no duplicate server css', async ({ page }) => {
await page.goto(f.url())
await waitForHydration(page)
await expectNoDuplicateServerCss(page)
})
test('adding/removing css client @js', async ({ page }) => {
await page.goto(f.url())
await waitForHydration(page)
await using _ = await expectNoReload(page)
await testAddRemoveCssClient(page, { js: true })
})
testNoJs('adding/removing css client @nojs', async ({ page }) => {
await page.goto(f.url())
await testAddRemoveCssClient(page, { js: false })
})
async function testAddRemoveCssClient(
page: Page,
options: { js: boolean },
) {
await expect(page.locator('.test-style-client-dep')).toHaveCSS(
'color',
'rgb(255, 165, 0)',
)
// remove css import
const editor = f.createEditor('src/routes/style-client/client-dep.tsx')
editor.edit((s) =>
s.replaceAll(
`import './client-dep.css'`,
`/* import './client-dep.css' */`,
),
)
await page.waitForTimeout(100)
await expect(async () => {
if (!options.js) await page.reload()
await expect(page.locator('.test-style-client-dep')).toHaveCSS(
'color',
'rgb(0, 0, 0)',
{ timeout: 10 },
)
}).toPass()
// add back css import
editor.reset()
await page.waitForTimeout(100)
await expect(async () => {
if (!options.js) await page.reload()
await expect(page.locator('.test-style-client-dep')).toHaveCSS(
'color',
'rgb(255, 165, 0)',
{ timeout: 10 },
)
}).toPass()
}
test('css hmr server', async ({ page }) => {
await page.goto(f.url())
await waitForHydration(page)
await using _ = await expectNoReload(page)
const editor = f.createEditor('src/routes/style-server/server.css')
editor.edit((s) => s.replaceAll('rgb(255, 165, 0)', 'rgb(0, 165, 255)'))
await expect(page.locator('.test-style-server')).toHaveCSS(
'color',
'rgb(0, 165, 255)',
)
editor.edit((s) =>
s.replaceAll(
`color: rgb(0, 165, 255);`,
`/* color: rgb(0, 165, 255); */`,
),
)
await expect(page.locator('.test-style-server')).toHaveCSS(
'color',
'rgb(0, 0, 0)',
)
editor.reset()
await expect(page.locator('.test-style-server')).toHaveCSS(
'color',
'rgb(255, 165, 0)',
)
await expect(page.locator('.test-style-server-manual')).toHaveCSS(
'color',
'rgb(255, 165, 0)',
)
await expectNoDuplicateServerCss(page)
})
// TODO: need a way to remove css links on server hmr. for now, it requires a manually reload.
test('adding/removing css server @js', async ({ page }) => {
await page.goto(f.url())
await waitForHydration(page)
await expect(page.locator('.test-style-server')).toHaveCSS(
'color',
'rgb(255, 165, 0)',
)
const editor = f.createEditor('src/routes/style-server/server.tsx')
// removing and adding new css works via hmr
{
await using _ = await expectNoReload(page)
// remove css import
editor.edit((s) =>
s.replaceAll(`import './server.css'`, `/* import './server.css' */`),
)
await expect(page.locator('.test-style-server')).toHaveCSS(
'color',
'rgb(0, 0, 0)',
)
// add new css
editor.edit((s) =>
s.replaceAll(`/* import './server.css' */`, `import './server2.css'`),
)
await expect(page.locator('.test-style-server')).toHaveCSS(
'color',
'rgb(0, 255, 165)',
)
}
// TODO: React doesn't re-inert same css link. so manual reload is required.
editor.reset()
await page.waitForTimeout(100)
await expect(async () => {
await page.reload()
await expect(page.locator('.test-style-server')).toHaveCSS(
'color',
'rgb(255, 165, 0)',
{ timeout: 10 },
)
}).toPass()
})
testNoJs('adding/removing css server @nojs', async ({ page }) => {
await page.goto(f.url())
await testAddRemoveCssServer(page, { js: false })
})
async function testAddRemoveCssServer(
page: Page,
options: { js: boolean },
) {
await expect(page.locator('.test-style-server')).toHaveCSS(
'color',
'rgb(255, 165, 0)',
)
// remove css import
const editor = f.createEditor('src/routes/style-server/server.tsx')
editor.edit((s) =>
s.replaceAll(`import './server.css'`, `/* import './server.css' */`),
)
await page.waitForTimeout(100)
await expect(async () => {
if (!options.js) await page.reload()
await expect(page.locator('.test-style-server')).toHaveCSS(
'color',
'rgb(0, 0, 0)',
{ timeout: 10 },
)
}).toPass()
// add back css import
editor.reset()
await page.waitForTimeout(100)
await expect(async () => {
if (!options.js) await page.reload()
await expect(page.locator('.test-style-server')).toHaveCSS(
'color',
'rgb(255, 165, 0)',
{ timeout: 10 },
)
}).toPass()
}
test('css module client hmr', async ({ page }) => {
await page.goto(f.url())
await waitForHydration(page)
await using _ = await expectNoReload(page)
const editor = f.createEditor('src/routes/style-client/client.module.css')
editor.edit((s) => s.replaceAll('rgb(255, 165, 0)', 'rgb(0, 165, 255)'))
await expect(page.getByTestId('css-module-client')).toHaveCSS(
'color',
'rgb(0, 165, 255)',
)
editor.reset()
await expect(page.getByTestId('css-module-client')).toHaveCSS(
'color',
'rgb(255, 165, 0)',
)
})
test('css module server hmr', async ({ page }) => {
await page.goto(f.url())
await waitForHydration(page)
await using _ = await expectNoReload(page)
const editor = f.createEditor('src/routes/style-server/server.module.css')
editor.edit((s) => s.replaceAll('rgb(255, 165, 0)', 'rgb(0, 165, 255)'))
await expect(page.getByTestId('css-module-server')).toHaveCSS(
'color',
'rgb(0, 165, 255)',
)
editor.reset()
await expect(page.getByTestId('css-module-server')).toHaveCSS(
'color',
'rgb(255, 165, 0)',
)
})
test('css url client hmr', async ({ page }) => {
await page.goto(f.url())
await waitForHydration(page)
await using _ = await expectNoReload(page)
const editor = f.createEditor('src/routes/style-client/client-url.css')
editor.edit((s) => s.replaceAll('rgb(255, 165, 0)', 'rgb(0, 165, 255)'))
await expect(page.locator('.test-style-url-client')).toHaveCSS(
'color',
'rgb(0, 165, 255)',
)
editor.reset()
await expect(page.locator('.test-style-url-client')).toHaveCSS(
'color',
'rgb(255, 165, 0)',
)
})
test('css url server hmr', async ({ page }) => {
await page.goto(f.url())
await waitForHydration(page)
await using _ = await expectNoReload(page)
const editor = f.createEditor('src/routes/style-server/server-url.css')
editor.edit((s) => s.replaceAll('rgb(255, 165, 0)', 'rgb(0, 165, 255)'))
await expect(page.locator('.test-style-url-server')).toHaveCSS(
'color',
'rgb(0, 165, 255)',
)
editor.reset()
await expect(page.locator('.test-style-url-server')).toHaveCSS(