#Vitest Headless UI HTML not rendered correctly
3 messages · Page 1 of 1 (latest)
Based on the information provided in the knowledge sources, I can offer some insights and suggestions regarding your issue with testing a Headless UI dialog using Vitest in a Nuxt application.
The problem you're experiencing seems to be related to how Teleport components are handled during testing. Teleport is a Vue feature that allows rendering content elsewhere in the DOM tree, which is commonly used for modals and dialogs.
Here are a few points to consider:
-
Nuxt's SSR support for teleports: According to the Nuxt documentation on Teleport (https://nuxt.com/docs/api/components/teleports), Nuxt currently has SSR support for teleports to
#teleportsonly. For other targets, client-side support is available using a<ClientOnly>wrapper. -
Testing environment: Make sure you're using the Nuxt testing environment. In your Vitest configuration, you should have
environment: 'nuxt'set. This ensures that a global Nuxt app is initialized before your tests run, including plugins and code defined in yourapp.vueNuxt Testing Setup (https://nuxt.com/docs/getting-started/testing#setup). -
Using
mountSuspended: You're correctly usingmountSuspendedfrom@nuxt/test-utils/runtime, which allows mounting Vue components within the Nuxt environment Nuxt Test Helpers (https://nuxt.com/docs/getting-started/testing#-helpers).
To address your specific issue, you might try the following:
-
Ensure your dialog is wrapped in a
<ClientOnly>component if it's using Teleport to a target other than#teleports. -
Instead of using
wrapper.html(), try using thescreenutility from@testing-library/vue. This might give you a more accurate representation of what's actually rendered. Here's an example:
import { renderSuspended } from '@nuxt/test-utils/runtime'
import { screen } from '@testing-library/vue'
import CreateDaytisticDialog from './CreateDaytisticDialog.vue'
it('should render the dialog', async () => {
await renderSuspended(CreateDaytisticDialog, {
props: {
open: true,
}
})
// Now use screen to query the rendered content
const dialogContent = screen.getByText('Your expected dialog content')
expect(dialogContent).toBeDefined()
})
- If you're still having issues, you might need to add a specific target for your Teleport in your test environment. You can do this by adding a div with the appropriate id to your test file:
beforeEach(() => {
const teleportTarget = document.createElement('div')
teleportTarget.id = 'teleports' // or whatever id you're using
document.body.appendChild(teleportTarget)
})
Remember that when testing with the Nuxt environment, you're running in a happy-dom or jsdom environment, which might behave slightly differently from a real browser Nuxt Testing Environment (https://nuxt.com/docs/getting-started/testing#setup).