From 89cf452f64fafabb70376ff6337355860dcd0ec9 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 27 Nov 2024 13:01:02 +0800 Subject: [PATCH 1/3] fix --- web_src/js/utils/dom.ts | 5 +++-- web_src/js/utils/testhelper.ts | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 web_src/js/utils/testhelper.ts diff --git a/web_src/js/utils/dom.ts b/web_src/js/utils/dom.ts index 06c5e8b486e95..557d57e946b38 100644 --- a/web_src/js/utils/dom.ts +++ b/web_src/js/utils/dom.ts @@ -1,6 +1,7 @@ import {debounce} from 'throttle-debounce'; import type {Promisable} from 'type-fest'; import type $ from 'jquery'; +import {isInFrontendUnitTest} from './testhelper.ts'; type ArrayLikeIterable = ArrayLike & Iterable; // for NodeListOf and Array type ElementArg = Element | string | ArrayLikeIterable | ReturnType; @@ -76,8 +77,8 @@ export function queryElemSiblings(el: Element, selector = '*' // it works like jQuery.children: only the direct children are selected export function queryElemChildren(parent: Element | ParentNode, selector = '*', fn?: ElementsCallback): ArrayLikeIterable { - if (window.vitest) { - // bypass the vitest bug: it doesn't support ":scope >" + if (isInFrontendUnitTest()) { + // https://github.com/capricorn86/happy-dom/issues/1620 : ":scope" doesn't work const selected = Array.from(parent.children as any).filter((child) => child.matches(selector)); return applyElemsCallback(selected, fn); } diff --git a/web_src/js/utils/testhelper.ts b/web_src/js/utils/testhelper.ts new file mode 100644 index 0000000000000..a3ea2918f9bc9 --- /dev/null +++ b/web_src/js/utils/testhelper.ts @@ -0,0 +1,6 @@ +// there could be different "testing" concepts, for example: backend's "setting.IsInTesting" +// even if backend is in testing mode, frontend could be complied in production mode +// so this function only checks if the frontend is in unit testing mode (usually from *.test.ts files) +export function isInFrontendUnitTest() { + return process.env.NODE_ENV === 'test'; +} From 004cf128ae2d8db48fc20bd118434ca7344ec17c Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 27 Nov 2024 13:04:13 +0800 Subject: [PATCH 2/3] fix ts type error --- web_src/js/utils/dom.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_src/js/utils/dom.ts b/web_src/js/utils/dom.ts index 557d57e946b38..6e830b9817938 100644 --- a/web_src/js/utils/dom.ts +++ b/web_src/js/utils/dom.ts @@ -358,6 +358,6 @@ export function addDelegatedEventListener { const elem = (e.target as HTMLElement).closest(selector); if (!elem) return; - listener(elem as T, e); + listener(elem as T, e as E); }, options); } From 0349b88af7b5db873ae40f76e0424054cdd7a62c Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 27 Nov 2024 18:12:13 +0800 Subject: [PATCH 3/3] Update web_src/js/utils/testhelper.ts --- web_src/js/utils/testhelper.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_src/js/utils/testhelper.ts b/web_src/js/utils/testhelper.ts index a3ea2918f9bc9..e91cf85e864bc 100644 --- a/web_src/js/utils/testhelper.ts +++ b/web_src/js/utils/testhelper.ts @@ -2,5 +2,5 @@ // even if backend is in testing mode, frontend could be complied in production mode // so this function only checks if the frontend is in unit testing mode (usually from *.test.ts files) export function isInFrontendUnitTest() { - return process.env.NODE_ENV === 'test'; + return process.env.TEST === 'true'; }