Skip to content

Commit 73be8c4

Browse files
authored
test(react): bundled-dev playground (#1268)
1 parent 889efb0 commit 73be8c4

8 files changed

Lines changed: 123 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { expect, test } from 'vitest'
2+
import { editFile, isServe, page } from '~utils'
3+
4+
test('should render', async () => {
5+
// In bundled dev mode, the page initially shows a "Bundling in progress"
6+
// placeholder and reloads once the bundle is ready.
7+
await expect.poll(() => page.textContent('h1')).toMatch('Hello Vite + React')
8+
})
9+
10+
test('should update', async () => {
11+
expect(await page.textContent('#state-button')).toMatch('count is: 0')
12+
await page.click('#state-button')
13+
expect(await page.textContent('#state-button')).toMatch('count is: 1')
14+
})
15+
16+
test.runIf(isServe)('should hmr', async () => {
17+
editFile('src/App.tsx', (code) =>
18+
code.replace('Vite + React', 'Vite + React Updated'),
19+
)
20+
await expect
21+
.poll(() => page.textContent('h1'))
22+
.toMatch('Hello Vite + React Updated')
23+
// preserve state
24+
expect(await page.textContent('#state-button')).toMatch('count is: 1')
25+
26+
editFile('src/App.tsx', (code) =>
27+
code.replace('Vite + React Updated', 'Vite + React'),
28+
)
29+
await expect.poll(() => page.textContent('h1')).toMatch('Hello Vite + React')
30+
})

playground/bundled-dev/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<div id="app"></div>
2+
<script type="module" src="./src/main.tsx"></script>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "@vitejs/test-react-bundled-dev",
3+
"private": true,
4+
"type": "module",
5+
"scripts": {
6+
"dev": "vite",
7+
"build": "vite build",
8+
"debug": "node --inspect-brk vite",
9+
"preview": "vite preview"
10+
},
11+
"dependencies": {
12+
"react": "^19.2.7",
13+
"react-dom": "^19.2.7"
14+
},
15+
"devDependencies": {
16+
"@types/react": "^19.2.17",
17+
"@types/react-dom": "^19.2.3",
18+
"@vitejs/plugin-react": "workspace:*"
19+
}
20+
}

playground/bundled-dev/src/App.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { useState } from 'react'
2+
3+
export default function App() {
4+
const [count, setCount] = useState(0)
5+
6+
return (
7+
<>
8+
<h1>Hello Vite + React</h1>
9+
<button id="state-button" onClick={() => setCount((count) => count + 1)}>
10+
count is: {count}
11+
</button>
12+
</>
13+
)
14+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import ReactDOM from 'react-dom/client'
2+
import App from './App.tsx'
3+
4+
ReactDOM.createRoot(document.getElementById('app')!).render(<App />)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"include": ["src"],
3+
"compilerOptions": {
4+
"target": "ES2020",
5+
"useDefineForClassFields": true,
6+
"lib": ["ES2020", "DOM"],
7+
"types": ["vite/client"],
8+
"module": "ESNext",
9+
"skipLibCheck": true,
10+
11+
/* Bundler mode */
12+
"moduleResolution": "bundler",
13+
"allowImportingTsExtensions": true,
14+
"verbatimModuleSyntax": true,
15+
"noEmit": true,
16+
"jsx": "react-jsx",
17+
18+
/* Linting */
19+
"noUnusedLocals": true,
20+
"noUnusedParameters": true,
21+
"noFallthroughCasesInSwitch": true
22+
}
23+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import react from '@vitejs/plugin-react'
2+
import { defineConfig } from 'vite'
3+
4+
export default defineConfig({
5+
base: '/bundled-dev/',
6+
server: { port: 8910 /* Should be unique */ },
7+
experimental: {
8+
bundledDev: true,
9+
},
10+
plugins: [react()],
11+
})

pnpm-lock.yaml

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)