Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix: improve compatibility of @headlessui/react with happy-dom by ensuring HTMLElement.prototype.focus is writable (#29399)

Coding On Star12d019cd3181
diff --git a/web/jest.setup.ts b/web/jest.setup.ts
index 383ba412a2f..006b28322e0 100644
--- a/web/jest.setup.ts
+++ b/web/jest.setup.ts
@@ -2,16 +2,22 @@ import '@testing-library/jest-dom'
import { cleanup } from '@testing-library/react'
// Fix for @headlessui/react compatibility with happy-dom
-// headlessui tries to set focus property which is read-only in happy-dom
+// headlessui tries to override focus properties which may be read-only in happy-dom
if (typeof window !== 'undefined') {
- // Ensure window.focus is writable for headlessui
- if (!Object.getOwnPropertyDescriptor(window, 'focus')?.writable) {
- Object.defineProperty(window, 'focus', {
- value: jest.fn(),
- writable: true,
- configurable: true,
- })
+ 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(() => {