Skip to content

Commit 722e703

Browse files
authored
Bypass vitest bug (#32647)
1 parent 88f5d33 commit 722e703

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

web_src/js/utils/dom.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {createElementFromAttrs, createElementFromHTML, querySingleVisibleElem} from './dom.ts';
1+
import {createElementFromAttrs, createElementFromHTML, queryElemChildren, querySingleVisibleElem} from './dom.ts';
22

33
test('createElementFromHTML', () => {
44
expect(createElementFromHTML('<a>foo<span>bar</span></a>').outerHTML).toEqual('<a>foo<span>bar</span></a>');
@@ -26,3 +26,9 @@ test('querySingleVisibleElem', () => {
2626
el = createElementFromHTML('<div><span>foo</span><span>bar</span></div>');
2727
expect(() => querySingleVisibleElem(el, 'span')).toThrowError('Expected exactly one visible element');
2828
});
29+
30+
test('queryElemChildren', () => {
31+
const el = createElementFromHTML('<div><span class="a">a</span><span class="b">b</span></div>');
32+
const children = queryElemChildren(el, '.a');
33+
expect(children.length).toEqual(1);
34+
});

web_src/js/utils/dom.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ export function queryElemSiblings<T extends Element>(el: Element, selector = '*'
7676

7777
// it works like jQuery.children: only the direct children are selected
7878
export function queryElemChildren<T extends Element>(parent: Element | ParentNode, selector = '*', fn?: ElementsCallback<T>): ArrayLikeIterable<T> {
79+
if (window.vitest) {
80+
// bypass the vitest bug: it doesn't support ":scope >"
81+
const selected = Array.from<T>(parent.children as any).filter((child) => child.matches(selector));
82+
return applyElemsCallback<T>(selected, fn);
83+
}
7984
return applyElemsCallback<T>(parent.querySelectorAll(`:scope > ${selector}`), fn);
8085
}
8186

0 commit comments

Comments
 (0)