Enhance Vitest configuration and improve Vue integration

- Added Vue plugin to vitest.config.ts for better component testing.
- Introduced vitest.setup.ts to expose Vue reactivity and lifecycle methods globally, ensuring compatibility with .vue components.
- Updated mock implementations in nuxt-imports.ts to include readonly for improved reactivity handling.
- Refactored useMapBookmarks and useToast composables to utilize readonly from Vue for better state management.
This commit is contained in:
2026-03-04 14:12:48 +03:00
parent fd624c2357
commit 337386caa8
7 changed files with 75 additions and 15 deletions

View File

@@ -1,6 +1,26 @@
import { ref, reactive, computed, watch, watchEffect, onMounted, onUnmounted, nextTick } from 'vue' import {
ref,
reactive,
computed,
watch,
watchEffect,
onMounted,
onUnmounted,
nextTick,
readonly,
} from 'vue'
export { ref, reactive, computed, watch, watchEffect, onMounted, onUnmounted, nextTick } export {
ref,
reactive,
computed,
watch,
watchEffect,
onMounted,
onUnmounted,
nextTick,
readonly,
}
export function useRuntimeConfig() { export function useRuntimeConfig() {
return { return {

View File

@@ -1,3 +1,5 @@
import { readonly } from 'vue'
export interface MapBookmark { export interface MapBookmark {
id: string id: string
name: string name: string

View File

@@ -1,3 +1,5 @@
import { readonly } from 'vue'
export type ToastType = 'success' | 'error' | 'info' export type ToastType = 'success' | 'error' | 'info'
export interface Toast { export interface Toast {

View File

@@ -4,29 +4,36 @@ import type L from 'leaflet'
import type { Map, LayerGroup } from 'leaflet' import type { Map, LayerGroup } from 'leaflet'
import { createCharacter, type CharacterData, type CharacterMapViewRef } from '../Character' import { createCharacter, type CharacterData, type CharacterMapViewRef } from '../Character'
vi.mock('leaflet', () => { const { leafletMock } = vi.hoisted(() => {
const markerMock = { const markerMock = {
on: vi.fn().mockReturnThis(), on: vi.fn().mockReturnThis(),
addTo: vi.fn().mockReturnThis(), addTo: vi.fn().mockReturnThis(),
setLatLng: vi.fn().mockReturnThis(), setLatLng: vi.fn().mockReturnThis(),
setIcon: vi.fn().mockReturnThis(), setIcon: vi.fn().mockReturnThis(),
} }
return { const Icon = vi.fn().mockImplementation(function (this: unknown) {
default: { return {}
marker: vi.fn(() => markerMock),
Icon: vi.fn().mockImplementation(() => ({})),
},
marker: vi.fn(() => markerMock),
Icon: vi.fn().mockImplementation(() => ({})),
}
}) })
const L = {
marker: vi.fn(() => markerMock),
Icon,
}
return { leafletMock: L }
})
vi.mock('leaflet', () => ({
__esModule: true,
default: leafletMock,
marker: leafletMock.marker,
Icon: leafletMock.Icon,
}))
vi.mock('~/lib/LeafletCustomTypes', () => ({ vi.mock('~/lib/LeafletCustomTypes', () => ({
HnHMaxZoom: 6, HnHMaxZoom: 6,
})) }))
function getL(): L { function getL(): L {
return require('leaflet').default return leafletMock as unknown as L
} }
function makeCharData(overrides: Partial<CharacterData> = {}): CharacterData { function makeCharData(overrides: Partial<CharacterData> = {}): CharacterData {

View File

@@ -15,14 +15,13 @@ vi.mock('leaflet', () => {
openPopup: vi.fn().mockReturnThis(), openPopup: vi.fn().mockReturnThis(),
closePopup: vi.fn().mockReturnThis(), closePopup: vi.fn().mockReturnThis(),
} }
return { const point = (x: number, y: number) => ({ x, y })
default: { const L = {
marker: vi.fn(() => markerMock),
Icon: vi.fn(),
},
marker: vi.fn(() => markerMock), marker: vi.fn(() => markerMock),
Icon: vi.fn(), Icon: vi.fn(),
point,
} }
return { __esModule: true, default: L, ...L }
}) })
vi.mock('~/lib/LeafletCustomTypes', () => ({ vi.mock('~/lib/LeafletCustomTypes', () => ({

View File

@@ -1,11 +1,14 @@
import { defineConfig } from 'vitest/config' import { defineConfig } from 'vitest/config'
import Vue from '@vitejs/plugin-vue'
import { resolve } from 'path' import { resolve } from 'path'
export default defineConfig({ export default defineConfig({
plugins: [Vue()],
test: { test: {
environment: 'happy-dom', environment: 'happy-dom',
include: ['**/__tests__/**/*.test.ts', '**/*.test.ts'], include: ['**/__tests__/**/*.test.ts', '**/*.test.ts'],
globals: true, globals: true,
setupFiles: ['./vitest.setup.ts'],
coverage: { coverage: {
provider: 'v8', provider: 'v8',
reporter: ['text', 'html'], reporter: ['text', 'html'],

View File

@@ -0,0 +1,27 @@
/**
* Expose Vue reactivity and lifecycle on globalThis so that .vue components
* that rely on Nuxt auto-imports (ref, computed, etc.) work in Vitest.
*/
import {
ref,
computed,
reactive,
watch,
watchEffect,
onMounted,
onUnmounted,
nextTick,
readonly,
} from 'vue'
Object.assign(globalThis, {
ref,
computed,
reactive,
watch,
watchEffect,
onMounted,
onUnmounted,
nextTick,
readonly,
})