|
| 1 | +import fs from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { dev, rspackOnlyTest } from '@e2e/helper'; |
| 4 | +import { expect, test } from '@playwright/test'; |
| 5 | + |
| 6 | +rspackOnlyTest( |
| 7 | + 'HMR should work properly with `createContext`', |
| 8 | + async ({ page }) => { |
| 9 | + // HMR cases will fail in Windows |
| 10 | + if (process.platform === 'win32') { |
| 11 | + test.skip(); |
| 12 | + } |
| 13 | + |
| 14 | + const root = __dirname; |
| 15 | + const compFilePath = path.join(root, 'src/test-temp-B.jsx'); |
| 16 | + const compSourceCode = `const B = (props) => { |
| 17 | + return <div id="B">B: {props.count}</div>; |
| 18 | +}; |
| 19 | +
|
| 20 | +export default B; |
| 21 | +`; |
| 22 | + |
| 23 | + fs.writeFileSync(compFilePath, compSourceCode, 'utf-8'); |
| 24 | + |
| 25 | + const rsbuild = await dev({ |
| 26 | + cwd: root, |
| 27 | + page, |
| 28 | + }); |
| 29 | + |
| 30 | + const a = page.locator('#A'); |
| 31 | + const b = page.locator('#B'); |
| 32 | + |
| 33 | + await expect(a).toHaveText('A: 0'); |
| 34 | + await expect(b).toHaveText('B: 0'); |
| 35 | + |
| 36 | + await a.click({ clickCount: 5 }); |
| 37 | + await expect(a).toHaveText('A: 5'); |
| 38 | + await expect(b).toHaveText('B: 5'); |
| 39 | + |
| 40 | + // simulate a change to component B's source code |
| 41 | + fs.writeFileSync( |
| 42 | + compFilePath, |
| 43 | + compSourceCode.replace('B:', 'Beep:'), |
| 44 | + 'utf-8', |
| 45 | + ); |
| 46 | + |
| 47 | + await page.waitForFunction(() => { |
| 48 | + const aText = document.querySelector('#A')!.textContent; |
| 49 | + const bText = document.querySelector('#B')!.textContent; |
| 50 | + |
| 51 | + return ( |
| 52 | + // the state (count) of A should be kept |
| 53 | + aText === 'A: 5' && |
| 54 | + // content of B changed to `Beep: 5` means HMR has taken effect |
| 55 | + bText === 'Beep: 5' |
| 56 | + ); |
| 57 | + }); |
| 58 | + |
| 59 | + await rsbuild.close(); |
| 60 | + }, |
| 61 | +); |
0 commit comments