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

mockNuxtImport unable to test for useRouter

Open cheesypeperoni opened this issue 1 year ago • 1 comments

I have a router.push in my store, its working completely fine and im able to navigate accordingly

Image

However when i try running test to it using vitest, it shows router not found. from what i see it actually couldnt read the "instance" of my router from my store.

i have this at the top of my file


  beforeEach(() => {
    setActivePinia(createPinia())

    mockNuxtImport('useRouter', () => {
      return () => {
        return mock<Router>()
      }
    })
  })

and then

    it('will navigate user to Route.dashboard upon logging in', async () => {
      const authStore = useAuthStore()
      const router = useRouter()

      expect(router.push).not.toHaveBeenCalled()

      await authStore.login({
        username: emailUsername,
        password,
      })

      expect(router.push).toHaveBeenCalledOnce()  // !! HERE is not found. any idea what might be the issue
     })

cheesypeperoni avatar Oct 11 '24 03:10 cheesypeperoni