1
+ import fs from 'node:fs/promises'
2
+ import { fileURLToPath } from 'node:url'
3
+
1
4
describe ( 'static data file support in vite 3' , ( ) => {
2
5
beforeAll ( async ( ) => {
3
6
await goto ( '/data-loading/data' )
@@ -7,10 +10,10 @@ describe('static data file support in vite 3', () => {
7
10
expect ( await page . textContent ( 'pre#basic' ) ) . toMatchInlineSnapshot ( `
8
11
"[
9
12
{
10
- "foo ": true
13
+ "a ": true
11
14
},
12
15
{
13
- "bar ": true
16
+ "b ": true
14
17
}
15
18
]"
16
19
` )
@@ -39,4 +42,67 @@ describe('static data file support in vite 3', () => {
39
42
]"
40
43
` )
41
44
} )
45
+
46
+ test . runIf ( ! process . env . VITE_TEST_BUILD ) ( 'hmr works' , async ( ) => {
47
+ const a = fileURLToPath ( new URL ( './data/a.json' , import . meta. url ) )
48
+ const b = fileURLToPath ( new URL ( './data/b.json' , import . meta. url ) )
49
+
50
+ try {
51
+ await fs . writeFile ( a , JSON . stringify ( { a : false } , null , 2 ) + '\n' )
52
+ await page . waitForFunction (
53
+ ( ) =>
54
+ document . querySelector ( 'pre#basic' ) ?. textContent ===
55
+ JSON . stringify ( [ { a : false } , { b : true } ] , null , 2 ) ,
56
+ undefined ,
57
+ { timeout : 3000 }
58
+ )
59
+ } finally {
60
+ await fs . writeFile ( a , JSON . stringify ( { a : true } , null , 2 ) + '\n' )
61
+ }
62
+
63
+ let err = true
64
+
65
+ try {
66
+ await fs . unlink ( b )
67
+ await page . waitForFunction (
68
+ ( ) =>
69
+ document . querySelector ( 'pre#basic' ) ?. textContent ===
70
+ JSON . stringify ( [ { a : true } ] , null , 2 ) ,
71
+ undefined ,
72
+ { timeout : 3000 }
73
+ )
74
+ err = false
75
+ } finally {
76
+ if ( err ) {
77
+ await fs . writeFile ( b , JSON . stringify ( { b : true } , null , 2 ) + '\n' )
78
+ }
79
+ }
80
+
81
+ try {
82
+ await fs . writeFile ( b , JSON . stringify ( { b : false } , null , 2 ) + '\n' )
83
+ await page . waitForFunction (
84
+ ( ) =>
85
+ document . querySelector ( 'pre#basic' ) ?. textContent ===
86
+ JSON . stringify ( [ { a : true } , { b : false } ] , null , 2 ) ,
87
+ undefined ,
88
+ { timeout : 3000 }
89
+ )
90
+ } finally {
91
+ await fs . writeFile ( b , JSON . stringify ( { b : true } , null , 2 ) + '\n' )
92
+ }
93
+ } )
94
+
95
+ /*
96
+ MODIFY a.json with { a: false }
97
+ this should trigger a hmr update and the content should be updated to [{ a: false }, { b: true }]
98
+ reset a.json
99
+
100
+ DELETE b.json
101
+ this should trigger a hmr update and the content should be updated to [{ a: true }]
102
+ reset b.json if failed
103
+
104
+ CREATE b.json with { b: false }
105
+ this should trigger a hmr update and the content should be updated to [{ a: true }, { b: false }]
106
+ reset b.json
107
+ */
42
108
} )
0 commit comments