Files
dify/web/jest.setup.ts
Coding On Star 12d019cd31 fix: improve compatibility of @headlessui/react with happy-dom by ensuring HTMLElement.prototype.focus is writable (#29399)
Co-authored-by: CodingOnStar <hanxujiang@dify.ai>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2025-12-10 14:40:48 +08:00

26 lines
818 B
TypeScript

import '@testing-library/jest-dom'
import { cleanup } from '@testing-library/react'
// Fix for @headlessui/react compatibility with happy-dom
// headlessui tries to override focus properties which may be read-only in happy-dom
if (typeof window !== 'undefined') {
const ensureWritable = (target: object, prop: string) => {
const descriptor = Object.getOwnPropertyDescriptor(target, prop)
if (descriptor && !descriptor.writable) {
const original = descriptor.value ?? descriptor.get?.call(target)
Object.defineProperty(target, prop, {
value: typeof original === 'function' ? original : jest.fn(),
writable: true,
configurable: true,
})
}
}
ensureWritable(window, 'focus')
ensureWritable(HTMLElement.prototype, 'focus')
}
afterEach(() => {
cleanup()
})