2023-07-27 20:20:11 +02:00
|
|
|
// vitest.setup.ts
|
|
|
|
import "vitest-canvas-mock";
|
2020-02-11 23:19:43 +01:00
|
|
|
import "@testing-library/jest-dom";
|
2024-07-25 18:55:55 +02:00
|
|
|
import fs from "fs";
|
2023-07-27 20:20:11 +02:00
|
|
|
import { vi } from "vitest";
|
2023-12-12 07:02:51 +01:00
|
|
|
import polyfill from "./packages/excalidraw/polyfill";
|
|
|
|
import { testPolyfills } from "./packages/excalidraw/tests/helpers/polyfills";
|
2024-08-09 17:27:02 +02:00
|
|
|
import { yellow } from "./packages/excalidraw/tests/helpers/colorize";
|
2023-10-28 21:29:28 +02:00
|
|
|
|
|
|
|
Object.assign(globalThis, testPolyfills);
|
2021-11-24 16:25:19 +01:00
|
|
|
|
2022-11-04 13:52:21 +01:00
|
|
|
require("fake-indexeddb/auto");
|
|
|
|
|
2022-08-11 16:46:25 +02:00
|
|
|
polyfill();
|
2021-11-24 16:25:19 +01:00
|
|
|
|
2024-04-08 16:46:24 +02:00
|
|
|
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(),
|
|
|
|
})),
|
|
|
|
});
|
|
|
|
|
2024-07-25 18:55:55 +02:00
|
|
|
Object.defineProperty(window, "FontFace", {
|
|
|
|
enumerable: true,
|
|
|
|
value: class {
|
|
|
|
private family: string;
|
|
|
|
private source: string;
|
|
|
|
private descriptors: any;
|
|
|
|
private status: string;
|
|
|
|
|
|
|
|
constructor(family, source, descriptors) {
|
|
|
|
this.family = family;
|
|
|
|
this.source = source;
|
|
|
|
this.descriptors = descriptors;
|
|
|
|
this.status = "unloaded";
|
|
|
|
}
|
|
|
|
|
|
|
|
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}/`,
|
|
|
|
});
|
|
|
|
|
|
|
|
vi.mock(
|
|
|
|
"./packages/excalidraw/fonts/ExcalidrawFont",
|
|
|
|
async (importOriginal) => {
|
|
|
|
const mod = await importOriginal<
|
|
|
|
typeof import("./packages/excalidraw/fonts/ExcalidrawFont")
|
|
|
|
>();
|
|
|
|
const ExcalidrawFontImpl = mod.ExcalidrawFont;
|
|
|
|
|
|
|
|
return {
|
|
|
|
...mod,
|
|
|
|
ExcalidrawFont: class extends ExcalidrawFontImpl {
|
|
|
|
public async getContent(): Promise<string> {
|
2024-07-30 10:34:40 +02:00
|
|
|
const url = this.urls[0];
|
|
|
|
|
|
|
|
if (url.protocol !== "file:") {
|
2024-07-25 18:55:55 +02:00
|
|
|
return super.getContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
// read local assets directly, without running a server
|
2024-07-30 10:34:40 +02:00
|
|
|
const content = await fs.promises.readFile(url);
|
2024-07-25 18:55:55 +02:00
|
|
|
return `data:font/woff2;base64,${content.toString("base64")}`;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2023-07-27 20:20:11 +02:00
|
|
|
vi.mock("nanoid", () => {
|
2021-06-09 23:16:56 +02:00
|
|
|
return {
|
2023-07-27 20:20:11 +02:00
|
|
|
nanoid: vi.fn(() => "test-id"),
|
2021-06-09 23:16:56 +02:00
|
|
|
};
|
|
|
|
});
|
2020-02-11 23:19:43 +01:00
|
|
|
// ReactDOM is located inside index.tsx file
|
|
|
|
// as a result, we need a place for it to render into
|
|
|
|
const element = document.createElement("div");
|
|
|
|
element.id = "root";
|
|
|
|
document.body.appendChild(element);
|
2024-08-06 15:17:42 +02:00
|
|
|
|
2024-08-09 17:27:02 +02:00
|
|
|
const _consoleError = console.error.bind(console);
|
2024-08-06 15:17:42 +02:00
|
|
|
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(")) {
|
2024-08-09 17:27:02 +02:00
|
|
|
_consoleError(
|
|
|
|
yellow(
|
|
|
|
`<<< WARNING: test "${
|
|
|
|
expect.getState().currentTestName
|
|
|
|
}" does not wrap some state update in act() >>>`,
|
|
|
|
),
|
2024-08-06 15:17:42 +02:00
|
|
|
);
|
|
|
|
} else {
|
2024-08-09 17:27:02 +02:00
|
|
|
_consoleError(...args);
|
2024-08-06 15:17:42 +02:00
|
|
|
}
|
|
|
|
};
|