dramaling-vocab-learning/frontend/tests/setup.ts

54 lines
1.2 KiB
TypeScript

import '@testing-library/jest-dom'
import { beforeAll, afterEach, afterAll } from 'vitest'
import { cleanup } from '@testing-library/react'
// 全局測試設置
beforeAll(() => {
// Mock window.location 為測試環境
Object.defineProperty(window, 'location', {
value: {
href: 'http://localhost:3000',
search: '',
pathname: '/',
assign: vi.fn(),
replace: vi.fn(),
reload: vi.fn()
},
writable: true
})
// Mock localStorage
Object.defineProperty(window, 'localStorage', {
value: {
getItem: vi.fn(),
setItem: vi.fn(),
removeItem: vi.fn(),
clear: vi.fn()
},
writable: true
})
// Mock console.log 除非在調試模式
if (!process.env.DEBUG) {
global.console = {
...console,
log: vi.fn(),
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn()
}
}
})
// 每個測試後清理
afterEach(() => {
cleanup()
vi.clearAllMocks()
// 重置 localStorage mock
vi.mocked(window.localStorage.getItem).mockClear()
vi.mocked(window.localStorage.setItem).mockClear()
})
afterAll(() => {
vi.restoreAllMocks()
})