Something went wrong. Try again.
A Excalidraw fork with syncing to your Atmosphere account and iroh for sync Https://lexidraw.app
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156import fs from "fs";
// vitest.setup.tsimport "vitest-canvas-mock";import "@testing-library/jest-dom";import { configure } from "@testing-library/react";import { vi } from "vitest";
import polyfill from "./packages/excalidraw/polyfill";import { mockThrottleRAF } from "./packages/excalidraw/tests/helpers/mocks";import { yellow } from "./packages/excalidraw/tests/helpers/colorize";import { PolyfillLocalStorage, testPolyfills,} from "./packages/excalidraw/tests/helpers/polyfills";
Object.assign(globalThis, testPolyfills);PolyfillLocalStorage();
// @testing-library/dom's waitFor only auto-advances fake timers when it// detects a jest-shaped global; vi is API-compatible for the calls it makes// (advanceTimersByTime). vitest-canvas-mock@0.3 used to leak a `jest` global// which masked this; 1.x no longer does.(globalThis as any).jest = vi;
// By default testing-library dumps the entire serialized DOM into the error// message whenever a `waitFor`/`getBy*` fails, which floods the test output// (often hundreds of lines of HTML per failure). Strip it out unless// VITE_DEBUG_DOM is enabled (see .env.test), e.g. `VITE_DEBUG_DOM=true yarn test`.const debugDom = ["true", "1"].includes(process.env.VITE_DEBUG_DOM ?? "");if (!debugDom) { configure({ getElementError: (message) => { const error = new Error(message ?? undefined); error.name = "TestingLibraryElementError"; return error; }, });}
vi.mock("@excalidraw/common", async (importOriginal) => { const module = await importOriginal<typeof import("@excalidraw/common")>();
return { ...module, throttleRAF: mockThrottleRAF, };});
// mock for pep.js not working with setPointerCapture()HTMLElement.prototype.setPointerCapture = vi.fn();
require("fake-indexeddb/auto");
polyfill();
Object.defineProperty(window, "matchMedia", { writable: true, value: vi.fn().mockImplementation((query) => ({ matches: false, media: query, onchange: null, addListener: vi.fn(), // deprecated removeListener: vi.fn(), // deprecated addEventListener: vi.fn(), removeEventListener: vi.fn(), dispatchEvent: vi.fn(), })),});
Object.defineProperty(window, "FontFace", { enumerable: true, value: class { private family: string; private source: string; private descriptors: any; private status: string; private unicodeRange: string;
constructor(family, source, descriptors) { this.family = family; this.source = source; this.descriptors = descriptors; this.status = "unloaded"; this.unicodeRange = "U+0000-00FF"; }
load() { this.status = "loaded"; } },});
Object.defineProperty(document, "fonts", { value: { load: vi.fn().mockResolvedValue([]), check: vi.fn().mockResolvedValue(true), has: vi.fn().mockResolvedValue(true), add: vi.fn(), },});
Object.defineProperty(window, "EXCALIDRAW_ASSET_PATH", { value: `file://${__dirname}/`,});
// mock the font fetch only, so that everything else, as font subsetting, can run inside of the (snapshot) testsvi.mock( "./packages/excalidraw/fonts/ExcalidrawFontFace", async (importOriginal) => { const mod = await importOriginal< typeof import("./packages/excalidraw/fonts/ExcalidrawFontFace") >(); const ExcalidrawFontFaceImpl = mod.ExcalidrawFontFace;
return { ...mod, ExcalidrawFontFace: class extends ExcalidrawFontFaceImpl { public async fetchFont(url: URL): Promise<ArrayBuffer> { if (!url.toString().startsWith("file://")) { return super.fetchFont(url); }
// read local assets directly, without running a server const content = await fs.promises.readFile(url); return content.buffer; } }, }; },);
// ReactDOM is located inside index.tsx file// as a result, we need a place for it to render intoconst element = document.createElement("div");element.id = "root";document.body.appendChild(element);
const _consoleError = console.error.bind(console);console.error = (...args) => { // the react's act() warning usually doesn't contain any useful stack trace // so we're catching the log and re-logging the message with the test name, // also stripping the actual component stack trace as it's not useful if (args[0]?.includes?.("act(")) { _consoleError( yellow( `<<< WARNING: test "${ expect.getState().currentTestName }" does not wrap some state update in act() >>>`, ), ); } else { _consoleError(...args); }};