|
1 | 1 | import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
2 | 2 | import { chromium, type Browser, type Page } from 'playwright'; |
3 | 3 | import path from 'node:path'; |
| 4 | +import fs from 'node:fs'; |
4 | 5 | import type { ChildProcess } from 'node:child_process'; |
5 | 6 | import { |
6 | 7 | createTempStateDir, |
@@ -121,4 +122,47 @@ describe('Browser e2e', () => { |
121 | 122 | expect(summary.commitCount).toBeGreaterThan(0); |
122 | 123 | expect(summary.componentRenderCounts.length).toBeGreaterThan(0); |
123 | 124 | }); |
| 125 | + |
| 126 | + it('HMR works — file change applies without full reload', async () => { |
| 127 | + const appFile = path.resolve( |
| 128 | + import.meta.dirname, |
| 129 | + '../../../examples/vite-app/src/App.tsx', |
| 130 | + ); |
| 131 | + const original = fs.readFileSync(appFile, 'utf-8'); |
| 132 | + |
| 133 | + try { |
| 134 | + // Set a window marker to detect full page reloads |
| 135 | + await page.evaluate(() => { |
| 136 | + (window as any).__hmrMarker = 'alive'; |
| 137 | + }); |
| 138 | + |
| 139 | + // Verify the original heading is present |
| 140 | + const h1 = page.locator('h1'); |
| 141 | + expect(await h1.textContent()).toBe('Perf Debug App'); |
| 142 | + |
| 143 | + // Modify the heading text |
| 144 | + const modified = original.replace('Perf Debug App', 'HMR Test Heading'); |
| 145 | + fs.writeFileSync(appFile, modified, 'utf-8'); |
| 146 | + |
| 147 | + // Wait for HMR to apply the change |
| 148 | + const deadline = Date.now() + 10_000; |
| 149 | + let found = false; |
| 150 | + while (Date.now() < deadline) { |
| 151 | + const text = await h1.textContent().catch(() => null); |
| 152 | + if (text === 'HMR Test Heading') { |
| 153 | + found = true; |
| 154 | + break; |
| 155 | + } |
| 156 | + await sleep(250); |
| 157 | + } |
| 158 | + expect(found, 'File change should be reflected in the browser').toBe(true); |
| 159 | + |
| 160 | + // Verify no full page reload occurred — the marker should still be set |
| 161 | + const marker = await page.evaluate(() => (window as any).__hmrMarker); |
| 162 | + expect(marker).toBe('alive'); |
| 163 | + } finally { |
| 164 | + // Always restore the original file |
| 165 | + fs.writeFileSync(appFile, original, 'utf-8'); |
| 166 | + } |
| 167 | + }); |
124 | 168 | }); |
0 commit comments