Files
ThreadNet-Web/apps/web/src/test/setupTests.ts
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

58 lines
1.6 KiB
TypeScript
Raw Normal View History

2026-06-15 14:24:33 +01:00
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE files in the repository root for full details.
*/
2026-07-07 10:50:40 +01:00
import { vi, beforeEach, afterEach } from "vitest";
2026-06-15 14:24:33 +01:00
import fetchMock, { manageFetchMockGlobally } from "@fetch-mock/vitest";
2026-06-19 16:34:18 +01:00
import SdkConfig, { DEFAULTS } from "../SdkConfig";
import "./setupGlobals.ts";
import { setupLanguageMock } from "./setupLanguage.ts";
2026-06-19 16:34:18 +01:00
2026-07-30 10:11:29 +01:00
declare global {
var IS_REACT_ACT_ENVIRONMENT: boolean;
}
globalThis.IS_REACT_ACT_ENVIRONMENT = true;
2026-08-05 09:51:02 +01:00
// Ignore benign post-teardown exceptions as they cause flakes
const guardState = globalThis as unknown as { __vitestTestRunning?: boolean; __teardownGuardInstalled?: boolean };
if (!guardState.__teardownGuardInstalled) {
guardState.__teardownGuardInstalled = true;
const isPostTeardownStraggler = (): boolean => !guardState.__vitestTestRunning || typeof window === "undefined";
process.on("uncaughtException", (err) => {
if (isPostTeardownStraggler()) return;
throw err;
});
process.on("unhandledRejection", (reason) => {
if (isPostTeardownStraggler()) return;
throw reason;
});
}
2026-06-15 14:24:33 +01:00
manageFetchMockGlobally();
beforeEach(() => {
2026-08-05 09:51:02 +01:00
guardState.__vitestTestRunning = true;
2026-07-07 10:50:40 +01:00
vi.stubEnv("TZ", "UTC");
2026-06-15 14:24:33 +01:00
// set up fetch API mock
fetchMock.hardReset();
fetchMock.catch(404);
fetchMock.mockGlobal();
setupLanguageMock();
2026-06-15 14:24:33 +01:00
});
2026-06-19 16:34:18 +01:00
2026-08-05 09:51:02 +01:00
afterEach(() => {
guardState.__vitestTestRunning = false;
return fetchMock.callHistory.flush();
});
2026-06-19 16:34:18 +01:00
// uninitialised SdkConfig causes lots of warnings in console, init with defaults
SdkConfig.put(DEFAULTS);