fix(stm32): make Stm32BluePillElement import-safe in node (vitest)

useSimulatorStore eagerly imports STM32_LED from this module, so the
top-level `class extends HTMLElement` + customElements.define ran at import
time and threw "HTMLElement is not defined" under vitest's node environment,
breaking 20 test files that load the store. Guard the base class with a
dummy fallback and skip registration when customElements is absent; browser
behavior is unchanged.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
David Montero 2026-05-31 08:33:55 +02:00
parent 81fdb0b959
commit db6537681a
1 changed files with 18 additions and 5 deletions

View File

@ -207,7 +207,16 @@ const TAG_TO_KIND: Record<string, string> = {
'velxio-stm32-netduino2': 'stm32-netduino2',
};
class Stm32BoardElement extends HTMLElement {
// Import-safe base. vitest runs in the 'node' environment where HTMLElement is
// undefined, and this module is imported eagerly by useSimulatorStore (for the
// STM32_LED map). Fall back to a dummy base so the module loads in node; in the
// browser this is the real HTMLElement.
const HTMLElementCtor: typeof HTMLElement =
typeof HTMLElement !== 'undefined'
? HTMLElement
: (class {} as unknown as typeof HTMLElement);
class Stm32BoardElement extends HTMLElementCtor {
static get observedAttributes() {
return ['board-kind'];
}
@ -340,9 +349,13 @@ class Stm32BoardElement extends HTMLElement {
export { Stm32BoardElement as Stm32BluePillElement };
// One unique constructor per tag — customElements.define() requires it.
for (const tag of Object.keys(TAG_TO_KIND)) {
if (!customElements.get(tag)) {
const cls = class extends Stm32BoardElement {};
customElements.define(tag, cls);
// Guarded for non-DOM environments (vitest 'node') where customElements
// is undefined; registration only matters in the browser.
if (typeof customElements !== 'undefined') {
for (const tag of Object.keys(TAG_TO_KIND)) {
if (!customElements.get(tag)) {
const cls = class extends Stm32BoardElement {};
customElements.define(tag, cls);
}
}
}