-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fix RouterView instance registration under HMR (Options API beforeRouteUpdate
)
#2545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dhyun2
wants to merge
2
commits into
vuejs:main
Choose a base branch
from
dhyun2:fix/hmr-beforeRouteUpdate-options-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+152
−4
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/** | ||
* @vitest-environment jsdom | ||
*/ | ||
import { describe, it, expect, vi } from 'vitest' | ||
|
||
describe('RouterView – HMR (Options API)', () => { | ||
it('Registers the instance on re-mount → nextTick via onVnodeMounted, even if watch(post) is blocked', async () => { | ||
// 1) Reset modules and prepare hoisted state to track blocked post-watch registrations | ||
vi.resetModules() | ||
const hoisted = vi.hoisted(() => ({ blockedPostWatchCalls: 0 })) | ||
|
||
// 2) Mock `vue`: block only watch({ flush: "post" }), delegate all others to the real implementation | ||
vi.doMock('vue', async () => { | ||
const actual = await vi.importActual<typeof import('vue')>('vue') | ||
return { | ||
...actual, | ||
watch: (source: any, cb: any, options: any) => { | ||
if (options?.flush === 'post') { | ||
hoisted.blockedPostWatchCalls++ | ||
return { stop() {} } as any // completely suppress post-watch callbacks | ||
} | ||
return (actual as any).watch(source, cb, options) | ||
}, | ||
} | ||
}) | ||
|
||
// 3) Dynamic import (after mocking is applied!) | ||
const { defineComponent, h, ref, nextTick } = await import('vue') | ||
const { mount } = await import('@vue/test-utils') | ||
|
||
// 4) Import router/RouterView and enable HMR flag | ||
;(globalThis as any).__DEV__ = true | ||
;(import.meta as any).hot = {} | ||
const { createRouter, createMemoryHistory, RouterView } = await import( | ||
'../src' | ||
) | ||
|
||
// 5) Define test component & router | ||
const beforeUpdateSpy = vi.fn() | ||
const OptComp = { | ||
template: `<div>Opt</div>`, | ||
beforeRouteUpdate(to: any) { | ||
beforeUpdateSpy(to.params.id) | ||
}, | ||
} | ||
|
||
const router = createRouter({ | ||
history: createMemoryHistory(), | ||
routes: [{ path: '/temp/:id', components: { default: OptComp } }], | ||
}) | ||
|
||
const rvKey = ref(0) | ||
const App = defineComponent({ | ||
setup() { | ||
return () => h('div', [h(RouterView, { key: rvKey.value })]) | ||
}, | ||
}) | ||
|
||
mount(App, { global: { plugins: [router] }, attachTo: document.body }) | ||
await router.push('/temp/1') | ||
await router.isReady() | ||
|
||
// 6) Prepare to track writes to rec.instances.default | ||
const rec = router.currentRoute.value.matched[0] | ||
expect(rec).toBeTruthy() | ||
|
||
if (rec?.instances && 'default' in rec.instances) { | ||
// @ts-ignore | ||
rec.instances.default = null | ||
} | ||
|
||
const desc = Object.getOwnPropertyDescriptor(rec.instances, 'default') | ||
let stored: any | ||
let preWrites = 0 | ||
let postWrites = 0 | ||
let phase: 'pre' | 'post' = 'pre' | ||
|
||
Object.defineProperty(rec.instances, 'default', { | ||
configurable: true, | ||
get() { | ||
return stored | ||
}, | ||
set(v) { | ||
stored = v | ||
if (phase === 'pre') { | ||
preWrites++ | ||
} else { | ||
postWrites++ | ||
} | ||
}, | ||
}) | ||
|
||
// 7) Re-mount → onVnodeMounted hasn’t fired yet in the same tick | ||
rvKey.value++ | ||
expect(preWrites).toBe(0) | ||
|
||
// 8) Next tick: onVnodeMounted should register the instance (post-watch is blocked) | ||
await nextTick() | ||
expect(preWrites).toBeGreaterThanOrEqual(1) // recorded via pre-registration only | ||
expect(Boolean(stored)).toBe(true) | ||
|
||
// 9) Navigation → post-watch is blocked, so postWrites must remain 0 | ||
phase = 'post' | ||
const navDone = new Promise<void>(resolve => { | ||
const remove = router.afterEach(() => { | ||
remove() | ||
resolve() | ||
}) | ||
}) | ||
router.push('/temp/2') | ||
await navDone | ||
|
||
expect(postWrites).toBe(0) // no post-watch writes | ||
expect(hoisted.blockedPostWatchCalls).toBeGreaterThanOrEqual(1) // attempted to register post-watch | ||
expect(beforeUpdateSpy).toHaveBeenCalledWith('2') // hook still works | ||
|
||
// 10) Restore original property & unmock | ||
if (desc) Object.defineProperty(rec.instances, 'default', desc) | ||
else delete (rec.instances as any).default | ||
;(rec.instances as any).default = stored | ||
vi.doUnmock('vue') | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.