test-utils icon indicating copy to clipboard operation
test-utils copied to clipboard

navigateTo assert not working when triggered inside page

Open henritoivar opened this issue 6 months ago • 3 comments

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

henritoivar avatar Dec 20 '23 14:12 henritoivar

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.

ExEr7um avatar Jan 17 '24 16:01 ExEr7um

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 avatar Jan 28 '24 11:01 danielroe

@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.

henritoivar avatar Jan 29 '24 08:01 henritoivar