test-utils
test-utils copied to clipboard
navigateTo assert not working when triggered inside page
Environment
https://stackblitz.com/edit/github-shyy23?file=pages%2Fmypage.vue
Reproduction
https://stackblitz.com/edit/github-shyy23?file=pages%2Fmypage.vue
Describe the bug
expect(useRoute().path).toEqual('/')
fails when navigateTo('/')
called within page component. Doesn't fail when called directly in test itself.
Additional context
No response
Logs
No response
For me it fails too, but there is a catch.
If I use watch(() => useRoute().path)
and then inside I use expect
it actually works.
So I think the problem is with timing. It does not await navigating between pages, so it happens after your expect
executes.
Yes, I think this is just an implementation detail. navigateTo
is an async function which doesn't update the route until an additional tick has passed.
See example test in this repo here:
https://github.com/nuxt/test-utils/blob/de9a2a81f04a5e41b677b33c31f831612493a218/examples/app-vitest-full/tests/nuxt/routing.spec.ts#L16-L21
@danielroe Did you look at the reproduction? You can see in the reproduction that we do use nextTick and the problem still persist so I don't think this issue is solved.