From 612d66fbb5162d9905cfb10919ca1761ce8c4680 Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Sat, 24 Aug 2024 21:10:09 +0530 Subject: [PATCH] chore: add tests for data loader hmr --- __tests__/e2e/data-loading/basic.data.mts | 14 ++--- __tests__/e2e/data-loading/data.test.ts | 70 ++++++++++++++++++++++- __tests__/e2e/data-loading/data/a.json | 3 + __tests__/e2e/data-loading/data/b.json | 3 + __tests__/e2e/data-loading/data/bar.json | 1 - __tests__/e2e/data-loading/data/foo.json | 1 - src/node/plugins/staticDataPlugin.ts | 2 +- 7 files changed, 80 insertions(+), 14 deletions(-) create mode 100644 __tests__/e2e/data-loading/data/a.json create mode 100644 __tests__/e2e/data-loading/data/b.json delete mode 100644 __tests__/e2e/data-loading/data/bar.json delete mode 100644 __tests__/e2e/data-loading/data/foo.json diff --git a/__tests__/e2e/data-loading/basic.data.mts b/__tests__/e2e/data-loading/basic.data.mts index 1a70d74a3a52..305551c018ff 100644 --- a/__tests__/e2e/data-loading/basic.data.mts +++ b/__tests__/e2e/data-loading/basic.data.mts @@ -7,14 +7,10 @@ export declare const data: Data export default defineLoader({ watch: ['./data/*'], async load(files: string[]): Promise { - const foo = fs.readFileSync( - files.find((f) => f.endsWith('foo.json'))!, - 'utf-8' - ) - const bar = fs.readFileSync( - files.find((f) => f.endsWith('bar.json'))!, - 'utf-8' - ) - return [JSON.parse(foo), JSON.parse(bar)] + const data: Data = [] + for (const file of files.sort().filter((file) => file.endsWith('.json'))) { + data.push(JSON.parse(fs.readFileSync(file, 'utf-8'))) + } + return data } }) diff --git a/__tests__/e2e/data-loading/data.test.ts b/__tests__/e2e/data-loading/data.test.ts index f12e97662209..fafc403acb9f 100644 --- a/__tests__/e2e/data-loading/data.test.ts +++ b/__tests__/e2e/data-loading/data.test.ts @@ -1,3 +1,6 @@ +import fs from 'node:fs/promises' +import { fileURLToPath } from 'node:url' + describe('static data file support in vite 3', () => { beforeAll(async () => { await goto('/data-loading/data') @@ -7,10 +10,10 @@ describe('static data file support in vite 3', () => { expect(await page.textContent('pre#basic')).toMatchInlineSnapshot(` "[ { - "foo": true + "a": true }, { - "bar": true + "b": true } ]" `) @@ -39,4 +42,67 @@ describe('static data file support in vite 3', () => { ]" `) }) + + test.runIf(!process.env.VITE_TEST_BUILD)('hmr works', async () => { + const a = fileURLToPath(new URL('./data/a.json', import.meta.url)) + const b = fileURLToPath(new URL('./data/b.json', import.meta.url)) + + try { + await fs.writeFile(a, JSON.stringify({ a: false }, null, 2) + '\n') + await page.waitForFunction( + () => + document.querySelector('pre#basic')?.textContent === + JSON.stringify([{ a: false }, { b: true }], null, 2), + undefined, + { timeout: 3000 } + ) + } finally { + await fs.writeFile(a, JSON.stringify({ a: true }, null, 2) + '\n') + } + + let err = true + + try { + await fs.unlink(b) + await page.waitForFunction( + () => + document.querySelector('pre#basic')?.textContent === + JSON.stringify([{ a: true }], null, 2), + undefined, + { timeout: 3000 } + ) + err = false + } finally { + if (err) { + await fs.writeFile(b, JSON.stringify({ b: true }, null, 2) + '\n') + } + } + + try { + await fs.writeFile(b, JSON.stringify({ b: false }, null, 2) + '\n') + await page.waitForFunction( + () => + document.querySelector('pre#basic')?.textContent === + JSON.stringify([{ a: true }, { b: false }], null, 2), + undefined, + { timeout: 3000 } + ) + } finally { + await fs.writeFile(b, JSON.stringify({ b: true }, null, 2) + '\n') + } + }) + + /* + MODIFY a.json with { a: false } + this should trigger a hmr update and the content should be updated to [{ a: false }, { b: true }] + reset a.json + + DELETE b.json + this should trigger a hmr update and the content should be updated to [{ a: true }] + reset b.json if failed + + CREATE b.json with { b: false } + this should trigger a hmr update and the content should be updated to [{ a: true }, { b: false }] + reset b.json + */ }) diff --git a/__tests__/e2e/data-loading/data/a.json b/__tests__/e2e/data-loading/data/a.json new file mode 100644 index 000000000000..eb174f2f6dff --- /dev/null +++ b/__tests__/e2e/data-loading/data/a.json @@ -0,0 +1,3 @@ +{ + "a": true +} diff --git a/__tests__/e2e/data-loading/data/b.json b/__tests__/e2e/data-loading/data/b.json new file mode 100644 index 000000000000..d44f4ab86f54 --- /dev/null +++ b/__tests__/e2e/data-loading/data/b.json @@ -0,0 +1,3 @@ +{ + "b": true +} diff --git a/__tests__/e2e/data-loading/data/bar.json b/__tests__/e2e/data-loading/data/bar.json deleted file mode 100644 index 01309803beec..000000000000 --- a/__tests__/e2e/data-loading/data/bar.json +++ /dev/null @@ -1 +0,0 @@ -{ "bar": true } diff --git a/__tests__/e2e/data-loading/data/foo.json b/__tests__/e2e/data-loading/data/foo.json deleted file mode 100644 index 46a10aa7fea1..000000000000 --- a/__tests__/e2e/data-loading/data/foo.json +++ /dev/null @@ -1 +0,0 @@ -{ "foo": true } diff --git a/src/node/plugins/staticDataPlugin.ts b/src/node/plugins/staticDataPlugin.ts index 283a5138e485..1d8d231fd729 100644 --- a/src/node/plugins/staticDataPlugin.ts +++ b/src/node/plugins/staticDataPlugin.ts @@ -121,7 +121,7 @@ export const staticDataPlugin: Plugin = { }, transform(_code, id) { - if (server && loaderMatch.test(id)) { + if (server && loaderMatch.test(id) && '_importGlobMap' in server) { // register this module as a glob importer const { watch } = idToLoaderModulesMap[id]! if (watch) {