N
Nuxt3w ago
Orbis

Nuxt UI v3 vue only x vitest

Does anyone know the nuxt ui v3 setup for vitest (vue only installation). I am facing:
TypeError: Package import specifier "#imports" is not defined in package
TypeError: Package import specifier "#imports" is not defined in package
5 Replies
kapa.ai
kapa.ai3w ago
Hi @Orbis. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- I understand you're having issues with Vitest testing in a Vue-only installation of Nuxt UI v3. The error you're seeing about #imports is related to Nuxt's auto-imports system. Based on the knowledge sources, there are a few important points to consider when setting up Vitest with Nuxt UI v3 in a Vue-only project: 1. First, Nuxt UI v3 is indeed compatible with standalone Vue projects as mentioned in the documentation: Nuxt UI v3 is now compatible with Vue. 2. For testing with Vitest, you'll need to properly configure your test environment. The error you're seeing is likely because Vitest is trying to resolve Nuxt's auto-imports which aren't available in a Vue-only setup. Here's how you can set up Vitest for a Vue-only Nuxt UI v3 project: 1. Install the necessary dependencies:
npm i --save-dev vitest @vue/test-utils happy-dom @vitejs/plugin-vue
npm i --save-dev vitest @vue/test-utils happy-dom @vitejs/plugin-vue
2. Create a vitest.config.ts file with the following content:
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ui from '@nuxt/ui/vite' // For Nuxt UI
// or import uiPro from '@nuxt/ui-pro/vite' if you're using UI Pro

export default defineConfig({
plugins: [
vue(),
ui({
// Your UI configuration here
})
],
test: {
environment: 'happy-dom',
}
})
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ui from '@nuxt/ui/vite' // For Nuxt UI
// or import uiPro from '@nuxt/ui-pro/vite' if you're using UI Pro

export default defineConfig({
plugins: [
vue(),
ui({
// Your UI configuration here
})
],
test: {
environment: 'happy-dom',
}
})
3. Make sure your package.json has "type": "module" specified or rename your Vitest config file to have the .mts or .mjs extension as mentioned in the Nuxt testing documentation. 4. For the #imports error specifically, you'll need to mock any Nuxt auto-imports in your tests. You can do this using the mockNuxtImport utility if you're using nuxt-vitest:
import { mockNuxtImport } from 'nuxt-vitest/utils'

// Mock any Nuxt auto-imports your components might be using
mockNuxtImport('useHead', () => {
return () => {}
})

// your tests here
import { mockNuxtImport } from 'nuxt-vitest/utils'

// Mock any Nuxt auto-imports your components might be using
mockNuxtImport('useHead', () => {
return () => {}
})

// your tests here
If you're not using nuxt-vitest, you'll need to manually mock these imports using Vitest's mocking capabilities:
import { vi } from 'vitest'

vi.mock('#imports', () => ({
// Mock any auto-imports your components are using
useHead: vi.fn(),
// other imports...
}))
import { vi } from 'vitest'

vi.mock('#imports', () => ({
// Mock any auto-imports your components are using
useHead: vi.fn(),
// other imports...
}))
This approach should help resolve the #imports error in your Vue-only Nuxt UI v3 setup with Vitest. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3w ago
<@382652505471057920> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Orbis
OrbisOP3w ago
@kapa.ai its not a nuxt app. I can only use @vue/test-utils
kapa.ai
kapa.ai3w ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- I understand you're working with a Vue-only application (not a Nuxt app) that uses Nuxt UI components, and you need to set up testing with Vitest and @vue/test-utils only. Since you're not using Nuxt, you'll need to handle the #imports error differently. The error occurs because Nuxt UI components might be using Nuxt's auto-imports system internally, but your Vue-only app doesn't have access to these imports. Here's how you can set up testing for a Vue-only app with Nuxt UI components: 1. Set up a basic Vitest configuration with @vue/test-utils:
import { defineConfig } from 'vitest/config'
import vue from '@vitejs/plugin-vue'

export default defineConfig({
plugins: [vue()],
test: {
environment: 'happy-dom',
},
});
import { defineConfig } from 'vitest/config'
import vue from '@vitejs/plugin-vue'

export default defineConfig({
plugins: [vue()],
test: {
environment: 'happy-dom',
},
});
2. For the #imports error, you'll need to mock any Nuxt-specific imports that the Nuxt UI components might be using. Since you can't use mockNuxtImport, you'll need to use Vitest's mocking capabilities:
import { vi } from 'vitest'

// Mock the #imports module
vi.mock('#imports', () => ({
// Add any auto-imports that Nuxt UI components might be using
useHead: vi.fn(),
useState: vi.fn(),
// Add other imports as needed
}))
import { vi } from 'vitest'

// Mock the #imports module
vi.mock('#imports', () => ({
// Add any auto-imports that Nuxt UI components might be using
useHead: vi.fn(),
useState: vi.fn(),
// Add other imports as needed
}))
3. When testing components that use Nuxt UI, you might need to stub certain components that rely on Nuxt features. For example:
import { mount } from '@vue/test-utils'
import MyComponent from './MyComponent.vue'

const wrapper = mount(MyComponent, {
global: {
stubs: {
// Stub any problematic Nuxt UI components
'UButton': true,
'SlideYTransition': {
template: '<div><slot /></div>'
}
}
}
})
import { mount } from '@vue/test-utils'
import MyComponent from './MyComponent.vue'

const wrapper = mount(MyComponent, {
global: {
stubs: {
// Stub any problematic Nuxt UI components
'UButton': true,
'SlideYTransition': {
template: '<div><slot /></div>'
}
}
}
})
This approach of stubbing transition components has been shown to help with similar issues, as mentioned in Stack Overflow. Remember that since you're using Nuxt UI in a non-Nuxt environment, you might encounter other integration challenges that would require additional mocking or stubbing depending on which Nuxt UI components you're using and their dependencies. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3w ago
<@382652505471057920> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?