import { setupZonelessTestEnv } from "jest-preset-angular/setup-env/zoneless" setupZonelessTestEnv() const numberToLocaleString = Number.prototype.toLocaleString Number.prototype.toLocaleString = function (locale = "en-US") { return numberToLocaleString.call(this, locale) } window["__TEST_ENVIRONMENT__"] = true // Polyfill Request and fetch for Three.js SVGLoader in JSDOM environment if (typeof globalThis.Request === "undefined") { globalThis.Request = class Request { url: string constructor(input: string) { this.url = input } } as any } if (typeof globalThis.fetch === "undefined") { globalThis.fetch = jest.fn(() => Promise.resolve({ ok: true, text: () => Promise.resolve("") }) ) as any } // JSDOM implements neither Blob.text()/arrayBuffer() nor their File counterparts, which every browser // the app runs in has had for years, so reading an uploaded file would reject in tests alone. // FileReader is implemented, so it stands in. if (typeof Blob.prototype.text === "undefined") { Blob.prototype.text = function (this: Blob) { return new Promise((resolve, reject) => { const reader = new FileReader() reader.onload = () => resolve(String(reader.result)) reader.onerror = () => reject(reader.error) reader.readAsText(this) }) } } if (typeof Blob.prototype.arrayBuffer === "undefined") { Blob.prototype.arrayBuffer = function (this: Blob) { return new Promise((resolve, reject) => { const reader = new FileReader() reader.onload = () => resolve(reader.result as ArrayBuffer) reader.onerror = () => reject(reader.error) reader.readAsArrayBuffer(this) }) } } // JSDOM (nwsapi) does not implement the native Popover API or the :popover-open selector. // Without zone.js swallowing the thrown error, components calling popover.matches(":popover-open") // would fail tests. Make the selector match nothing and stub the popover methods. const originalElementMatches = Element.prototype.matches // Cast because the native signature is a set of overloads that narrow the receiver, and a function // literal cannot express a type predicate — the shim stands in for all of them. Element.prototype.matches = function (this: Element, selectors: string): boolean { if (selectors === ":popover-open") { return false } return originalElementMatches.call(this, selectors) } as typeof Element.prototype.matches if (typeof HTMLElement.prototype.showPopover === "undefined") { HTMLElement.prototype.showPopover = function () {} } if (typeof HTMLElement.prototype.hidePopover === "undefined") { HTMLElement.prototype.hidePopover = function () {} } if (typeof HTMLElement.prototype.togglePopover === "undefined") { HTMLElement.prototype.togglePopover = function () { return false } } // JSDOM applies the UA rule `dialog:not([open]) { display: none }` but loads none of the app's // stylesheets, so a popover built on would hide its content from accessible queries. // In the browser the app's own display declaration wins over that UA rule, so mirror it here. const dialogPopoverStyle = document.createElement("style") dialogPopoverStyle.textContent = "dialog[popover] { display: flex }" document.head.append(dialogPopoverStyle)