import '@testing-library/jest-dom' import { TextDecoder, TextEncoder } from 'util' // Node/JSDOM 环境下某些依赖(如 Prisma/cuid2)需要 TextEncoder/TextDecoder if (typeof global.TextEncoder === 'undefined') { global.TextEncoder = TextEncoder } if (typeof global.TextDecoder === 'undefined') { global.TextDecoder = TextDecoder } // Polyfill setImmediate for Prisma if (typeof setImmediate === 'undefined') { global.setImmediate = (callback, ...args) => setTimeout(callback, 0, ...args) } // Polyfill Request and Response for Next.js server components if (typeof Request === 'undefined') { global.Request = class Request { constructor(input, init) { this.url = typeof input === 'string' ? input : input.url this.method = init?.method || 'GET' this.headers = new Map(Object.entries(init?.headers || {})) this.body = init?.body } } } if (typeof Response === 'undefined') { global.Response = class Response { constructor(body, init) { this.body = body this.status = init?.status || 200 this.statusText = init?.statusText || 'OK' this.headers = new Map(Object.entries(init?.headers || {})) } json() { return Promise.resolve(JSON.parse(this.body)) } text() { return Promise.resolve(this.body) } } } // Mock IntersectionObserver global.IntersectionObserver = class IntersectionObserver { constructor() {} disconnect() {} observe() {} unobserve() {} } // Mock ResizeObserver global.ResizeObserver = class ResizeObserver { constructor() {} disconnect() {} observe() {} unobserve() {} } // Mock matchMedia (only in browser environment) if (typeof window !== 'undefined') { Object.defineProperty(window, 'matchMedia', { writable: true, value: jest.fn().mockImplementation(query => ({ matches: false, media: query, onchange: null, addListener: jest.fn(), // deprecated removeListener: jest.fn(), // deprecated addEventListener: jest.fn(), removeEventListener: jest.fn(), dispatchEvent: jest.fn(), })), }) } // Mock CSS.supports global.CSS = { supports: jest.fn().mockReturnValue(true), } // Mock performance.now global.performance.now = jest.fn(() => Date.now())