Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions web_src/js/utils/dom.ts
Original file line number Diff line number Diff line change
@@ -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<T> = ArrayLike<T> & Iterable<T>; // for NodeListOf and Array
type ElementArg = Element | string | ArrayLikeIterable<Element> | ReturnType<typeof $>;
Expand Down Expand Up @@ -76,8 +77,8 @@ export function queryElemSiblings<T extends Element>(el: Element, selector = '*'

// it works like jQuery.children: only the direct children are selected
export function queryElemChildren<T extends Element>(parent: Element | ParentNode, selector = '*', fn?: ElementsCallback<T>): ArrayLikeIterable<T> {
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<T>(parent.children as any).filter((child) => child.matches(selector));
return applyElemsCallback<T>(selected, fn);
}
Expand Down Expand Up @@ -357,6 +358,6 @@ export function addDelegatedEventListener<T extends HTMLElement, E extends Event
parent.addEventListener(type, (e: Event) => {
const elem = (e.target as HTMLElement).closest(selector);
if (!elem) return;
listener(elem as T, e);
listener(elem as T, e as E);
}, options);
}
6 changes: 6 additions & 0 deletions web_src/js/utils/testhelper.ts
Original file line number Diff line number Diff line change
@@ -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';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you check if NODE_ENV=production make test-frontend still sets the correct test env? I assume it will, but good to verify.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes or no, I guess it isn't in this PR's scope and it doesn't affect anything.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well it certainly is "in scope" if your solution does not work in all cases, and some users are known to alter NODE_ENV.

Copy link
Member

@silverwind silverwind Nov 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it and vitest does not set test when the env var is present:

https://github.com/vitest-dev/vitest/blob/7b35d13a6f235ce2b5bb406107980d36c19e9a2f/packages/vitest/src/node/pool.ts#L125

So let's use process.env.TEST instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not my solution, that's the official approach.

There is no trick, but only process.env.NODE_ENV, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return process.env.NODE_ENV === 'test';
return process.env.TEST === 'true';

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See below. I think it's a more robust approach, now using process.env.TEST

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already done in #32656 (comment)

}