-
Notifications
You must be signed in to change notification settings - Fork 86
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
Test fails if useNuxtApp used #744
Comments
I have the same issue. If I rename Edit: |
I'm seeing this same issue in my most basic test: <!-- File: /layouts/mount-suspended.vue -->
<template>
<div>
<div>
<h1>{{ isHydrated }}</h1>
<NuxtPage />
</div>
</div>
</template>
<script setup>
import { useNuxtApp } from '#app'
const nuxtApp = useNuxtApp()
const isHydrated = ref(nuxtApp.isHydrating)
</script> And in my Vitest test: // File: /layouts/tests/mount-suspended.test.js
// @vitest-environment nuxt
import { mountSuspended } from '@nuxt/test-utils/runtime'
import layoutToMountSuspended from '@/layouts/mount-suspended'
describe('mountSuspended experiment', () => {
let wrapper
beforeEach(async () => {
wrapper = await mountSuspended(layoutToMountSuspended, {
global: {
stubs: ['NuxtPage'],
},
})
})
it('is a component', () => {
expect(wrapper.vm).toBeTruthy()
})
}) This test fails with the same error as above, but for me I have to not only rename the constant to something other than import { useNuxtApp as whatever } from '#app'
const bobLoblaw = whatever()
const isHydrated = ref(bobLoblaw.isHydrating) Now my test runs successfully. I'm guessing there's some sort of auto-import issue that scans files for those exact names. (I'd like to add that I've also seen issues in the past, even if an automatic Nuxt import appears only in a commented section. It still reads the comments and causes issues. So watch out for commented out code too! Thanks @kabalage. This has been one of the most perplexing and difficult-to-solve issues I've come across in a long time. EDIT: All that said, I hope this issue will still be addressed. Changing the alias/name throughout all of our files doesn't seem like a very tenable solution, nor is this issue documented anywhere except here. |
I'd like to add that I'm seeing the same error for other imports as well, such as
I don't know how Update: Sometimes the issue is that the test simply times out after 10 seconds, and renaming |
I've been researching this problem some more, and I found another workaround that doesn't require aliasing However, this is also problematic because we've been using import { useRoute, useRouter } from 'vue-router' throughout our app, and when we remove this and instead rely on Nuxt's auto-import feature, the auto-import for the route/router is coming from Nuxt instead of Nevertheless I'm posting this here since people are going to need every workaround they can for this issue until it is addressed. |
So I tried debugging the issue and I made some progress on this. To debug I ran vitest with I looked for the warning thrown by vue: I found it in Inspecting the runtime objects, I figured out that anything defined in the setup of the Nuxt root component cannot be overriden. So basically none of these variable names work in script setup components when mounting via
From inspecting the callstack, this where the warning happens: I'm not exactly sure how this cloning/proxying is done in Sorry to ping you @danielroe, I know you must be getting many notifications, but I think this needs some of your attention. What direction should I take here? I tried the latter locally, that works, but |
Environment
Reproduction
https://stackblitz.com/edit/github-vy6psx-sn3lfr
Describe the bug
[Vue warn]: Cannot mutate <script setup> binding "nuxtApp" from Options API. at <MountSuspendedComponent > at <MountSuspendedHelper> at <Anonymous ref="VTU_COMPONENT" > at <VTUROOT> [Vue warn]: Unhandled error during execution of render function at <MountSuspendedComponent > at <MountSuspendedHelper> at <Anonymous ref="VTU_COMPONENT" > at <VTUROOT>
I get this error message whenever I try to use useNudxtApp() in a component.
Before the merge of nuxt-vitest and @nuxt/text-utils, this worked fine.
Additional context
Tests can be ran with: npm run test ./pages/tests/index.spec.ts
Logs
No response
The text was updated successfully, but these errors were encountered: