|
| 1 | +import { cp } from 'node:fs/promises'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { rspack } from '@rspack/core'; |
| 4 | +import { RspackDevServer } from '@rspack/dev-server'; |
| 5 | +import { editFile, getRandomPort } from '@rstackjs/test-utils'; |
| 6 | +import { expect } from '@rstest/playwright'; |
| 7 | +import { test } from '../helpers'; |
| 8 | +import createConfig from './rspack.config'; |
| 9 | + |
| 10 | +for (const refresh of ['automatic', 'explicit Babel', 'pre-loader'] as const) { |
| 11 | + test(`preserves parent state during HMR with ${refresh} refresh`, async ({ |
| 12 | + page, |
| 13 | + tempDir, |
| 14 | + }) => { |
| 15 | + const src = path.join(tempDir, 'src'); |
| 16 | + await cp(path.join(import.meta.dirname, 'src'), src, { recursive: true }); |
| 17 | + const port = await getRandomPort(); |
| 18 | + const compiler = rspack({ ...createConfig({ refresh }), context: tempDir }); |
| 19 | + const server = new RspackDevServer( |
| 20 | + { host: '127.0.0.1', port, hot: true }, |
| 21 | + compiler, |
| 22 | + ); |
| 23 | + try { |
| 24 | + await server.start(); |
| 25 | + await page.goto(`http://127.0.0.1:${port}`); |
| 26 | + const button = page.getByRole('button'); |
| 27 | + await button.click(); |
| 28 | + await expect(button).toHaveText('Count: 1'); |
| 29 | + await editFile(path.join(src, 'Counter.tsx'), (code) => |
| 30 | + code.replace('Count:', 'Updated:'), |
| 31 | + ); |
| 32 | + await expect(button).toHaveText('Updated: 1'); |
| 33 | + } finally { |
| 34 | + await server.stop(); |
| 35 | + } |
| 36 | + }); |
| 37 | +} |
0 commit comments