Skip to content

Commit 612d66f

Browse files
committed
chore: add tests for data loader hmr
1 parent d062732 commit 612d66f

File tree

7 files changed

+80
-14
lines changed

7 files changed

+80
-14
lines changed

__tests__/e2e/data-loading/basic.data.mts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@ export declare const data: Data
77
export default defineLoader({
88
watch: ['./data/*'],
99
async load(files: string[]): Promise<Data> {
10-
const foo = fs.readFileSync(
11-
files.find((f) => f.endsWith('foo.json'))!,
12-
'utf-8'
13-
)
14-
const bar = fs.readFileSync(
15-
files.find((f) => f.endsWith('bar.json'))!,
16-
'utf-8'
17-
)
18-
return [JSON.parse(foo), JSON.parse(bar)]
10+
const data: Data = []
11+
for (const file of files.sort().filter((file) => file.endsWith('.json'))) {
12+
data.push(JSON.parse(fs.readFileSync(file, 'utf-8')))
13+
}
14+
return data
1915
}
2016
})

__tests__/e2e/data-loading/data.test.ts

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import fs from 'node:fs/promises'
2+
import { fileURLToPath } from 'node:url'
3+
14
describe('static data file support in vite 3', () => {
25
beforeAll(async () => {
36
await goto('/data-loading/data')
@@ -7,10 +10,10 @@ describe('static data file support in vite 3', () => {
710
expect(await page.textContent('pre#basic')).toMatchInlineSnapshot(`
811
"[
912
{
10-
"foo": true
13+
"a": true
1114
},
1215
{
13-
"bar": true
16+
"b": true
1417
}
1518
]"
1619
`)
@@ -39,4 +42,67 @@ describe('static data file support in vite 3', () => {
3942
]"
4043
`)
4144
})
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+
*/
42108
})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"a": true
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"b": true
3+
}

__tests__/e2e/data-loading/data/bar.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

__tests__/e2e/data-loading/data/foo.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/node/plugins/staticDataPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export const staticDataPlugin: Plugin = {
121121
},
122122

123123
transform(_code, id) {
124-
if (server && loaderMatch.test(id)) {
124+
if (server && loaderMatch.test(id) && '_importGlobMap' in server) {
125125
// register this module as a glob importer
126126
const { watch } = idToLoaderModulesMap[id]!
127127
if (watch) {

0 commit comments

Comments
 (0)