Skip to content

Commit

Permalink
fix: don't invalidate when code is invalid (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Dec 19, 2022
1 parent 9955e30 commit 9231a86
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/plugin-react/src/fast-refresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ function isReactRefreshBoundary(mod) {
}
import.meta.hot.accept(mod => {
if (!mod) return;
if (isReactRefreshBoundary(mod)) {
${timeout}
} else {
Expand Down
30 changes: 26 additions & 4 deletions playground/react/__tests__/react.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,34 @@ test('should update', async () => {
})

test('should hmr', async () => {
editFile('App.jsx', (code) => code.replace('Vite + React', 'Updated'))
await untilUpdated(() => page.textContent('h1'), 'Hello Updated')
editFile('App.jsx', (code) =>
code.replace('Vite + React', 'Vite + React Updated'),
)
await untilUpdated(() => page.textContent('h1'), 'Hello Vite + React Updated')
// preserve state
expect(await page.textContent('#state-button')).toMatch('count is: 1')

editFile('App.jsx', (code) =>
code.replace('Vite + React Updated', 'Vite + React'),
)
await untilUpdated(() => page.textContent('h1'), 'Hello Vite + React')
})

test.runIf(isServe)('should not invalidate when code is invalid', async () => {
editFile('App.jsx', (code) =>
code.replace('<div className="App">', '<div className="App"}>'),
)

await untilUpdated(
() => page.textContent('vite-error-overlay .message-body'),
'Unexpected token',
)
// if import.meta.invalidate happened, the old page won't be shown because the page is reloaded
expect(await page.textContent('h1')).toMatch('Hello Vite + React')

editFile('App.jsx', (code) =>
code.replace('<div className="App"}>', '<div className="App">'),
)
})

test.runIf(isServe)(
Expand Down Expand Up @@ -59,7 +83,6 @@ if (!isBuild) {
'[vite] hot updated: /hmr/parent.jsx',
'Parent rendered',
],
true,
)
await untilUpdated(() => page.textContent('#parent'), 'Updated')
})
Expand All @@ -86,7 +109,6 @@ if (!isBuild) {
'[vite] hot updated: /context/ContextButton.jsx',
'Parent rendered',
],
true,
)
await untilUpdated(
() => page.textContent('#context-provider'),
Expand Down

0 comments on commit 9231a86

Please sign in to comment.