blob: fec659a76b48d6ce19176ae79b22c61179873189 [file] [log] [blame]
San3yuan6f2ed692025-04-16 20:24:49 +08001// jest.setup.js
2
3// 引入 jest-dom 断言库,使得 expect 可以使用更多 DOM 相关断言
4import {jest} from '@jest/globals';
5import '@testing-library/jest-dom';
6import { TextEncoder, TextDecoder } from 'util';
7
8global.TextEncoder = TextEncoder as typeof global.TextEncoder;
9global.TextDecoder = TextDecoder as typeof global.TextDecoder;
10
11// 强制覆盖 window.alert
12window.alert = jest.fn(() => {}); // 增加空函数实现
13
14// 保留原始 console.error 的 mock
15const originalConsoleError = console.error;
16console.error = jest.fn((...args) => {
17 originalConsoleError(...args);
18});
19
20// 每次测试前重置 mock 状态
21beforeEach(() => {
22 jest.clearAllMocks();
23});
24
25// 如果需要模拟一些全局对象,可以在这里进行
26global.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