From db6537681a1df55e497d5a08ee5fb0ddfc25034a Mon Sep 17 00:00:00 2001 From: David Montero Date: Sun, 31 May 2026 08:33:55 +0200 Subject: [PATCH] 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 --- .../velxio-components/Stm32BluePillElement.ts | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/frontend/src/components/velxio-components/Stm32BluePillElement.ts b/frontend/src/components/velxio-components/Stm32BluePillElement.ts index 1d4a3af7..1d60553a 100644 --- a/frontend/src/components/velxio-components/Stm32BluePillElement.ts +++ b/frontend/src/components/velxio-components/Stm32BluePillElement.ts @@ -207,7 +207,16 @@ const TAG_TO_KIND: Record = { '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); + } } }