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:
@@ -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 {
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { readonly } from 'vue'
|
||||||
|
|
||||||
export interface MapBookmark {
|
export interface MapBookmark {
|
||||||
id: string
|
id: string
|
||||||
name: string
|
name: string
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
|||||||
@@ -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', () => ({
|
||||||
|
|||||||
@@ -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'],
|
||||||
|
|||||||
27
frontend-nuxt/vitest.setup.ts
Normal file
27
frontend-nuxt/vitest.setup.ts
Normal 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,
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user