NuxtN
Nuxt13mo ago
Yuri

useRoute and mountSuspended - test is freezing

My test:
import { describe, it, expect, } from 'vitest';
import { mockNuxtImport, mountSuspended } from '@nuxt/test-utils/runtime';
import { flushPromises } from '@vue/test-utils';
import LayoutLogo from '../LayoutLogo.vue';
import {useLayoutStore} from "~/store/layout";

// Mock useRoute and useLayoutStore
mockNuxtImport('useRoute', () => {
    return () => ({
        fullPath: '/' // default mock path
    });
});

const initialLogo = {
    width: 100,
    height: 50,
    alt: '',
    src:"https://example.com/test-image.jpg",
}
describe('LayoutLogo.vue', () => {
    const createWrapper = async () => {
        const layoutStore = useLayoutStore();
        layoutStore.logo = initialLogo
        return await mountSuspended(LayoutLogo);
    };

    it('renders the correct component based on the route', async () => {
        const wrapper = await createWrapper();
        expect(wrapper.find('div').exists()).toBe(true);
    });
});


My component:
import { useRoute } from 'nuxt/app'
import { NuxtLink } from '#components'
import { useLayoutStore } from '~/store/layout'

const layoutStore = useLayoutStore()
const route = useRoute()

const componentName = computed(() => {
  if (route.fullPath === '/') {
    return 'div'
  }
  return NuxtLink
})

const url = computed(() => {
  if (route.fullPath === '/') {
    return null
  }
  return '/'
})

When I start test, I get error:
TypeError: 'set' on proxy: trap returned falsish for property 'url'
 ❯ Proxy.clonedComponent.render node_modules/@nuxt/test-utils/dist/runtime-utils/index.mjs:141:44
    140|                           renderContext[key] = methods[key].bind(renderContext);
    141|                         }
    142|                       }
       |                  ^
    143|                       if (computed && typeof computed === "object") {
    144|                         for (const key in computed) {

Why?
Was this page helpful?