|
| 1 | +import { expect, test } from "@playwright/test"; |
| 2 | +import { |
| 3 | + setupDevServer, |
| 4 | + setupBuildAndPreview, |
| 5 | + setupWaitForLogs, |
| 6 | +} from "../../utils.ts"; |
| 7 | + |
| 8 | +test("Default build", async ({ page }) => { |
| 9 | + const { testUrl, server } = await setupBuildAndPreview("react-18"); |
| 10 | + await page.goto(testUrl); |
| 11 | + |
| 12 | + await page.click("button"); |
| 13 | + await expect(page.locator("button")).toHaveText("count is 1"); |
| 14 | + |
| 15 | + await server.httpServer.close(); |
| 16 | +}); |
| 17 | + |
| 18 | +test("HMR invalidate", async ({ page }) => { |
| 19 | + const { testUrl, server, editFile } = await setupDevServer("react-18"); |
| 20 | + const waitForLogs = await setupWaitForLogs(page); |
| 21 | + await page.goto(testUrl); |
| 22 | + await waitForLogs("[vite] connected."); |
| 23 | + |
| 24 | + await page.click("button"); |
| 25 | + await expect(page.locator("button")).toHaveText("count is 1"); |
| 26 | + |
| 27 | + editFile("src/App.tsx", ["{count}", "{count}!"]); |
| 28 | + await waitForLogs("[vite] hot updated: /src/App.tsx"); |
| 29 | + await expect(page.locator("button")).toHaveText("count is 1!"); |
| 30 | + |
| 31 | + // Edit component |
| 32 | + editFile("src/TitleWithExport.tsx", ["Vite +", "Vite *"]); |
| 33 | + await waitForLogs("[vite] hot updated: /src/TitleWithExport.tsx"); |
| 34 | + |
| 35 | + // Edit export |
| 36 | + editFile("src/TitleWithExport.tsx", ["React", "React!"]); |
| 37 | + await waitForLogs( |
| 38 | + '[vite] invalidate /src/TitleWithExport.tsx: Could not Fast Refresh ("framework" export is incompatible). Learn more at https://github.com/vitejs/vite-plugin-react-swc#consistent-components-exports', |
| 39 | + "[vite] hot updated: /src/App.tsx", |
| 40 | + ); |
| 41 | + await expect(page.locator("h1")).toHaveText("Vite * React!"); |
| 42 | + |
| 43 | + // Add non-component export |
| 44 | + editFile("src/TitleWithExport.tsx", [ |
| 45 | + 'React!";', |
| 46 | + 'React!";\nexport const useless = 3;', |
| 47 | + ]); |
| 48 | + await waitForLogs( |
| 49 | + "[vite] invalidate /src/TitleWithExport.tsx: Could not Fast Refresh (new export)", |
| 50 | + "[vite] hot updated: /src/App.tsx", |
| 51 | + ); |
| 52 | + |
| 53 | + // Add component export |
| 54 | + editFile("src/TitleWithExport.tsx", [ |
| 55 | + "</h1>;", |
| 56 | + "</h1>;\nexport const Title2 = () => <h2>Title2</h2>;", |
| 57 | + ]); |
| 58 | + await waitForLogs("[vite] hot updated: /src/TitleWithExport.tsx"); |
| 59 | + |
| 60 | + // Import new component |
| 61 | + editFile( |
| 62 | + "src/App.tsx", |
| 63 | + ["import { TitleWithExport", "import { TitleWithExport, Title2"], |
| 64 | + ["<TitleWithExport />", "<TitleWithExport /> <Title2 />"], |
| 65 | + ); |
| 66 | + await waitForLogs("[vite] hot updated: /src/App.tsx"); |
| 67 | + await expect(page.locator("h2")).toHaveText("Title2"); |
| 68 | + |
| 69 | + // Remove component export |
| 70 | + editFile("src/TitleWithExport.tsx", [ |
| 71 | + "\nexport const Title2 = () => <h2>Title2</h2>;", |
| 72 | + "", |
| 73 | + ]); |
| 74 | + await waitForLogs( |
| 75 | + "[vite] invalidate /src/TitleWithExport.tsx: Could not Fast Refresh (export removed)", |
| 76 | + "[vite] hot updated: /src/App.tsx", |
| 77 | + /Failed to reload \/src\/App\.tsx. This could be due to syntax errors or importing non-existent modules\. \(see errors above\)$/, |
| 78 | + ); |
| 79 | + |
| 80 | + // Remove usage from App |
| 81 | + editFile( |
| 82 | + "src/App.tsx", |
| 83 | + ["import { TitleWithExport, Title2", "import { TitleWithExport"], |
| 84 | + ["<TitleWithExport /> <Title2 />", "<TitleWithExport />"], |
| 85 | + ); |
| 86 | + await waitForLogs("[vite] hot updated: /src/App.tsx"); |
| 87 | + await expect(page.locator("button")).toHaveText("count is 1!"); |
| 88 | + |
| 89 | + // Remove useless export |
| 90 | + editFile("src/TitleWithExport.tsx", ["\nexport const useless = 3;", ""]); |
| 91 | + await waitForLogs( |
| 92 | + "[vite] invalidate /src/TitleWithExport.tsx: Could not Fast Refresh (export removed)", |
| 93 | + "[vite] hot updated: /src/App.tsx", |
| 94 | + ); |
| 95 | + |
| 96 | + await server.close(); |
| 97 | +}); |
0 commit comments