11import { 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'
310
411test ( 'should render' , async ( ) => {
512 expect ( await page . textContent ( 'h1' ) ) . toMatch ( 'Hello Vite + React' )
613} )
714
815test ( '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' )
1219} )
1320
1421test ( 'should hmr' , async ( ) => {
1522 editFile ( 'App.jsx' , ( code ) => code . replace ( 'Vite + React' , 'Updated' ) )
1623 await untilUpdated ( ( ) => page . textContent ( 'h1' ) , 'Hello Updated' )
1724 // preserve state
18- expect ( await page . textContent ( 'button' ) ) . toMatch ( 'count is: 1' )
25+ expect ( await page . textContent ( '#state- button' ) ) . toMatch ( 'count is: 1' )
1926} )
2027
2128test . runIf ( isServe ) (
2229 'should have annotated jsx with file location metadata' ,
2330 async ( ) => {
2431 const meta = await page . evaluate ( ( ) => {
25- const button = document . querySelector ( 'button' )
32+ const button = document . querySelector ( '#state- button' )
2633 const key = Object . keys ( button ) . find (
2734 ( key ) => key . indexOf ( '__reactFiber' ) === 0
2835 )
@@ -37,3 +44,46 @@ test.runIf(isServe)(
3744 ] )
3845 }
3946)
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