1
1
import { expect , test } from 'vitest'
2
- import { editFile , isServe , page , untilUpdated } from '~utils'
2
+ import {
3
+ browserLogs ,
4
+ editFile ,
5
+ isBuild ,
6
+ isServe ,
7
+ page ,
8
+ untilUpdated
9
+ } from '~utils'
3
10
4
11
test ( 'should render' , async ( ) => {
5
12
expect ( await page . textContent ( 'h1' ) ) . toMatch ( 'Hello Vite + React' )
6
13
} )
7
14
8
15
test ( 'should update' , async ( ) => {
9
- expect ( await page . textContent ( 'button' ) ) . toMatch ( 'count is: 0' )
10
- await page . click ( 'button' )
11
- expect ( await page . textContent ( 'button' ) ) . toMatch ( 'count is: 1' )
16
+ expect ( await page . textContent ( '#state- button' ) ) . toMatch ( 'count is: 0' )
17
+ await page . click ( '#state- button' )
18
+ expect ( await page . textContent ( '#state- button' ) ) . toMatch ( 'count is: 1' )
12
19
} )
13
20
14
21
test ( 'should hmr' , async ( ) => {
15
22
editFile ( 'App.jsx' , ( code ) => code . replace ( 'Vite + React' , 'Updated' ) )
16
23
await untilUpdated ( ( ) => page . textContent ( 'h1' ) , 'Hello Updated' )
17
24
// preserve state
18
- expect ( await page . textContent ( 'button' ) ) . toMatch ( 'count is: 1' )
25
+ expect ( await page . textContent ( '#state- button' ) ) . toMatch ( 'count is: 1' )
19
26
} )
20
27
21
28
test . runIf ( isServe ) (
22
29
'should have annotated jsx with file location metadata' ,
23
30
async ( ) => {
24
31
const meta = await page . evaluate ( ( ) => {
25
- const button = document . querySelector ( 'button' )
32
+ const button = document . querySelector ( '#state- button' )
26
33
const key = Object . keys ( button ) . find (
27
34
( key ) => key . indexOf ( '__reactFiber' ) === 0
28
35
)
@@ -37,3 +44,46 @@ test.runIf(isServe)(
37
44
] )
38
45
}
39
46
)
47
+
48
+ if ( ! isBuild ) {
49
+ // #9869
50
+ test ( 'should only hmr files with exported react components' , async ( ) => {
51
+ browserLogs . length = 0
52
+ editFile ( 'hmr/no-exported-comp.jsx' , ( code ) =>
53
+ code . replace ( 'An Object' , 'Updated' )
54
+ )
55
+ await untilUpdated ( ( ) => page . textContent ( '#parent' ) , 'Updated' )
56
+ expect ( browserLogs ) . toMatchObject ( [
57
+ '[vite] hot updated: /hmr/no-exported-comp.jsx' ,
58
+ '[vite] hot updated: /hmr/parent.jsx' ,
59
+ 'Parent rendered'
60
+ ] )
61
+ browserLogs . length = 0
62
+ } )
63
+
64
+ // #3301
65
+ test ( 'should hmr react context' , async ( ) => {
66
+ browserLogs . length = 0
67
+ expect ( await page . textContent ( '#context-button' ) ) . toMatch (
68
+ 'context-based count is: 0'
69
+ )
70
+ await page . click ( '#context-button' )
71
+ expect ( await page . textContent ( '#context-button' ) ) . toMatch (
72
+ 'context-based count is: 1'
73
+ )
74
+ editFile ( 'context/CountProvider.jsx' , ( code ) =>
75
+ code . replace ( 'context provider' , 'context provider updated' )
76
+ )
77
+ await untilUpdated (
78
+ ( ) => page . textContent ( '#context-provider' ) ,
79
+ 'context provider updated'
80
+ )
81
+ expect ( browserLogs ) . toMatchObject ( [
82
+ '[vite] hot updated: /context/CountProvider.jsx' ,
83
+ '[vite] hot updated: /App.jsx' ,
84
+ '[vite] hot updated: /context/ContextButton.jsx' ,
85
+ 'Parent rendered'
86
+ ] )
87
+ browserLogs . length = 0
88
+ } )
89
+ }
0 commit comments