| name | Create Utility Tests |
| description | Guide for creating utility tests in this project using Nuxt test utils and component mounting. |
1. Environment Setup
Tests are organized into projects within vitest.config.ts. The suites project is specifically configured for Nuxt utilities.
- Location: Place utility tests in
test/suites/utilities/ and you can add them under their own directory.
2. Component-Based Testing
Do not use mocks for DOM elements or Vue instances. Instead, use real components.
Pattern
-
Create a Test Component: Create a .vue file (e.g., test-component.vue) in the test suite directory if the utility works with vue components or template refs
- Use
<script setup lang="ts"> and useTemplateRef for type-safe refs.
- Explicitly
defineExpose any refs needed for testing.
- Only if the utility can't use other components present, i.e it requires something different from what's in other components present
<script setup lang="ts">
import { useTemplateRef } from 'vue'
const myRef = useTemplateRef('myRef')
defineExpose({ myRef })
</script>
<template>
<div ref="myRef"></div>
</template>
-
Mount the component using with mountSuspended
import { mountSuspended } from '@nuxt/test-utils/runtime'
import TestComponent from './test-component.vue'
const wrapper = await mountSuspended(TestComponent)
const vm = wrapper.vm
3. Strict Typing
The project enforces strict TypeScript. Avoid any.
4. Test Structure
Grouping logic
If testing multiple functions with similar signatures (e.g., variations of a utility), use a data-driven approach to share common tests.
const variants = [
{ name: 'variantA', fn: funcA },
{ name: 'variantB', fn: funcB }
]
describe('Common Behavior', () => {
variants.forEach(({ name, fn }) => {
describe(name, () => {
})
})
})
Separating Specifics
Create separate describe blocks for behaviors unique to specific functions or edge cases.
describe('funcA (Specific)', () => {
it('should handle edge case X', () => { ... })
})
5. Do Not Mock Core Utils
- Avoid:
vi.mock('vue'), vi.mock('#imports').
- Use: Real imports from
#imports or relative paths to src/runtime.
Summary Checklist