San3yuan | 6f2ed69 | 2025-04-16 20:24:49 +0800 | [diff] [blame^] | 1 | // jest.setup.js |
| 2 | |
| 3 | // 引入 jest-dom 断言库,使得 expect 可以使用更多 DOM 相关断言 |
| 4 | import {jest} from '@jest/globals'; |
| 5 | import '@testing-library/jest-dom'; |
| 6 | import { TextEncoder, TextDecoder } from 'util'; |
| 7 | |
| 8 | global.TextEncoder = TextEncoder as typeof global.TextEncoder; |
| 9 | global.TextDecoder = TextDecoder as typeof global.TextDecoder; |
| 10 | |
| 11 | // 强制覆盖 window.alert |
| 12 | window.alert = jest.fn(() => {}); // 增加空函数实现 |
| 13 | |
| 14 | // 保留原始 console.error 的 mock |
| 15 | const originalConsoleError = console.error; |
| 16 | console.error = jest.fn((...args) => { |
| 17 | originalConsoleError(...args); |
| 18 | }); |
| 19 | |
| 20 | // 每次测试前重置 mock 状态 |
| 21 | beforeEach(() => { |
| 22 | jest.clearAllMocks(); |
| 23 | }); |
| 24 | |
| 25 | // 如果需要模拟一些全局对象,可以在这里进行 |
| 26 | global.localStorage = { |
| 27 | getItem: jest.fn(), |
| 28 | setItem: jest.fn(), |
| 29 | removeItem: jest.fn(), |
| 30 | clear: jest.fn(), |
| 31 | key: jest.fn(), |
| 32 | length: 0, |
| 33 | }; |
| 34 | |