203 lines
4.1 KiB
JavaScript
203 lines
4.1 KiB
JavaScript
import { vi, beforeEach, afterEach, beforeAll, afterAll } from 'vitest'
|
|
import { config } from '@vue/test-utils'
|
|
import { createPinia, setActivePinia } from 'pinia'
|
|
import ElementPlus from 'element-plus'
|
|
|
|
// 全局模拟
|
|
// 模拟window.matchMedia
|
|
Object.defineProperty(window, 'matchMedia', {
|
|
writable: true,
|
|
value: vi.fn().mockImplementation(query => ({
|
|
matches: false,
|
|
media: query,
|
|
onchange: null,
|
|
addListener: vi.fn(), // deprecated
|
|
removeListener: vi.fn(), // deprecated
|
|
addEventListener: vi.fn(),
|
|
removeEventListener: vi.fn(),
|
|
dispatchEvent: vi.fn(),
|
|
})),
|
|
})
|
|
|
|
// 模拟ResizeObserver
|
|
global.ResizeObserver = class ResizeObserver {
|
|
constructor(cb) {
|
|
this.cb = cb
|
|
}
|
|
observe() {}
|
|
unobserve() {}
|
|
disconnect() {}
|
|
}
|
|
|
|
// 模拟IntersectionObserver
|
|
global.IntersectionObserver = class IntersectionObserver {
|
|
constructor(cb) {
|
|
this.cb = cb
|
|
}
|
|
observe() {}
|
|
unobserve() {}
|
|
disconnect() {}
|
|
}
|
|
|
|
// 模拟window.location
|
|
Object.defineProperty(window, 'location', {
|
|
value: {
|
|
href: 'http://localhost:3000',
|
|
origin: 'http://localhost:3000',
|
|
protocol: 'http:',
|
|
host: 'localhost:3000',
|
|
hostname: 'localhost',
|
|
port: '3000',
|
|
pathname: '/',
|
|
search: '',
|
|
hash: '',
|
|
assign: vi.fn(),
|
|
replace: vi.fn(),
|
|
reload: vi.fn(),
|
|
},
|
|
writable: true,
|
|
})
|
|
|
|
// 模拟localStorage
|
|
const localStorageMock = {
|
|
getItem: vi.fn(),
|
|
setItem: vi.fn(),
|
|
removeItem: vi.fn(),
|
|
clear: vi.fn(),
|
|
length: 0,
|
|
key: vi.fn(),
|
|
}
|
|
global.localStorage = localStorageMock
|
|
|
|
// 模拟sessionStorage
|
|
const sessionStorageMock = {
|
|
getItem: vi.fn(),
|
|
setItem: vi.fn(),
|
|
removeItem: vi.fn(),
|
|
clear: vi.fn(),
|
|
length: 0,
|
|
key: vi.fn(),
|
|
}
|
|
global.sessionStorage = sessionStorageMock
|
|
|
|
// 模拟navigator
|
|
Object.defineProperty(window, 'navigator', {
|
|
value: {
|
|
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
|
|
language: 'en-US',
|
|
languages: ['en-US', 'en'],
|
|
platform: 'Win32',
|
|
cookieEnabled: true,
|
|
onLine: true,
|
|
geolocation: {
|
|
getCurrentPosition: vi.fn(),
|
|
watchPosition: vi.fn(),
|
|
clearWatch: vi.fn(),
|
|
},
|
|
},
|
|
writable: true,
|
|
})
|
|
|
|
// 模拟performance
|
|
Object.defineProperty(window, 'performance', {
|
|
value: {
|
|
now: vi.fn(() => Date.now()),
|
|
timing: {
|
|
navigationStart: Date.now() - 1000,
|
|
loadEventEnd: Date.now(),
|
|
},
|
|
getEntriesByType: vi.fn(() => []),
|
|
mark: vi.fn(),
|
|
measure: vi.fn(),
|
|
getEntriesByName: vi.fn(() => []),
|
|
clearMarks: vi.fn(),
|
|
clearMeasures: vi.fn(),
|
|
},
|
|
writable: true,
|
|
})
|
|
|
|
// 模拟console方法以减少测试输出噪音
|
|
global.console = {
|
|
...console,
|
|
log: vi.fn(),
|
|
debug: vi.fn(),
|
|
info: vi.fn(),
|
|
warn: vi.fn(),
|
|
error: vi.fn(),
|
|
}
|
|
|
|
// 配置Vue Test Utils
|
|
config.global.plugins = [ElementPlus, createPinia()]
|
|
|
|
// 配置全局组件
|
|
config.global.stubs = {
|
|
'el-button': true,
|
|
'el-input': true,
|
|
'el-form': true,
|
|
'el-form-item': true,
|
|
'el-select': true,
|
|
'el-option': true,
|
|
'el-table': true,
|
|
'el-table-column': true,
|
|
'el-pagination': true,
|
|
'el-dialog': true,
|
|
'el-drawer': true,
|
|
'el-tooltip': true,
|
|
'el-popover': true,
|
|
'el-alert': true,
|
|
'el-loading': true,
|
|
'el-icon': true,
|
|
'router-link': true,
|
|
'router-view': true,
|
|
}
|
|
|
|
// 配置全局mocks
|
|
config.global.mocks = {
|
|
$t: (key) => key,
|
|
$router: {
|
|
push: vi.fn(),
|
|
replace: vi.fn(),
|
|
go: vi.fn(),
|
|
back: vi.fn(),
|
|
forward: vi.fn(),
|
|
},
|
|
$route: {
|
|
path: '/',
|
|
name: 'Home',
|
|
params: {},
|
|
query: {},
|
|
hash: '',
|
|
fullPath: '/',
|
|
matched: [],
|
|
meta: {},
|
|
},
|
|
}
|
|
|
|
// 在每个测试前设置Pinia
|
|
beforeEach(() => {
|
|
const pinia = createPinia()
|
|
setActivePinia(pinia)
|
|
})
|
|
|
|
// 在所有测试前执行
|
|
beforeAll(() => {
|
|
// 设置测试环境变量
|
|
process.env.NODE_ENV = 'test'
|
|
|
|
// 模拟API基础URL
|
|
process.env.VITE_API_BASE_URL = 'http://localhost:3000/api'
|
|
})
|
|
|
|
// 在所有测试后执行
|
|
afterAll(() => {
|
|
// 清理测试环境
|
|
})
|
|
|
|
// 在每个测试后执行
|
|
afterEach(() => {
|
|
// 清理模拟
|
|
vi.clearAllMocks()
|
|
|
|
// 重置模块注册表
|
|
vi.resetModules()
|
|
}) |