Update fetch-mock-jest to @fetch-mock/jest (#31720)

* Remove tests which assert feature_oidc_native_flow=false behaviour, that setting is long gone

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* Upgrade fetch-mock-jest to @fetch-mock/jest

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* Update yarn.lock

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* Make knip happy

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* Disable broken tests

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* Fix shared-components tests

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* Snapshots

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

---------

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Michael Telatynski
2026-01-15 09:21:25 +00:00
committed by GitHub
parent f5c6477ef7
commit 6f0cd7621b
64 changed files with 372 additions and 556 deletions

View File

@@ -24,5 +24,5 @@ export const mocks = {
setSinkId: jest.fn(),
suspend: jest.fn(),
decodeAudioData: jest.fn(),
},
} as unknown as AudioContext,
};

View File

@@ -6,7 +6,7 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Com
Please see LICENSE files in the repository root for full details.
*/
import fetchMock from "fetch-mock-jest";
import fetchMock from "@fetch-mock/jest";
import { ModuleLoader } from "@element-hq/element-web-module-api";
import { merge } from "lodash";
@@ -44,21 +44,29 @@ export function setupLanguageMock() {
const enTranslations = merge(enElementWeb, enSharedComponents);
const deTranslations = merge(deElementWeb, deSharedComponents);
fetchMock.mockGlobal();
fetchMock
.get("/i18n/languages.json", {
en: "en_EN.json",
de: "de_DE.json",
lv: "lv.json",
})
.get(
"end:/i18n/languages.json",
{
en: "en_EN.json",
de: "de_DE.json",
lv: "lv.json",
},
{ name: "languages" },
)
.get("end:en_EN.json", enTranslations)
.get("end:de_DE.json", deTranslations)
.get("end:lv.json", lv);
}
setupLanguageMock();
beforeEach(setupLanguageMock);
afterEach(() => fetchMock.callHistory.flush());
// Initialise the fetchMock before the test starts so the languageHandler.setLanguage call below can function
setupLanguageMock();
languageHandler.setLanguage("en");
languageHandler.setMissingEntryGenerator((key) => key.split("|", 2)[1]);
// Set up the mdule API (so the i18n API exists)
// Set up the module API (so the i18n API exists)
const moduleLoader = new ModuleLoader(ModuleApi.instance);
window.mxModuleLoader = moduleLoader;

View File

@@ -6,9 +6,8 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Com
Please see LICENSE files in the repository root for full details.
*/
import fetchMock from "fetch-mock-jest";
import { TextDecoder, TextEncoder } from "util";
import { Response } from "node-fetch";
import fetchMock, { manageFetchMockGlobally } from "@fetch-mock/jest";
import { jest } from "@jest/globals";
import { mocks } from "./mocks";
@@ -46,7 +45,7 @@ window.ClipboardEvent = MyClipboardEvent as any;
// matchMedia is not included in jsdom
// TODO: Extract this to a function and have tests that need it opt into it.
const mockMatchMedia = (query: string) => ({
global.matchMedia = (query: string) => ({
matches: false,
media: query,
onchange: null,
@@ -54,35 +53,39 @@ const mockMatchMedia = (query: string) => ({
removeListener: jest.fn(), // Deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
dispatchEvent: jest.fn<(event: Event) => boolean>(),
});
global.matchMedia = mockMatchMedia;
// maplibre requires a createObjectURL mock
global.URL.createObjectURL = jest.fn();
global.URL.createObjectURL = jest.fn((obj) => "blob");
global.URL.revokeObjectURL = jest.fn();
// polyfilling TextEncoder as it is not available on JSDOM
// view https://github.com/facebook/jest/issues/9983
// XXX: Node's implementation has marginally different types, so we fudge it
(globalThis as any).TextEncoder = TextEncoder;
// @ts-ignore
global.TextDecoder = TextDecoder;
// prevent errors whenever a component tries to manually scroll.
window.HTMLElement.prototype.scrollIntoView = jest.fn();
window.HTMLAudioElement.prototype.canPlayType = jest.fn((format) => (format === "audio/mpeg" ? "probably" : ""));
// set up fetch API mock
fetchMock.config.overwriteRoutes = false;
fetchMock.catch("");
fetchMock.get("/image-file-stub", "image file stub");
fetchMock.get("/_matrix/client/versions", {});
// @ts-ignore
window.fetch = fetchMock.sandbox();
function setupFileStubMocks() {
fetchMock.get("end:/image-file-stub", "image file stub", { sticky: true });
}
setupFileStubMocks();
// @ts-ignore
window.Response = Response;
beforeEach(() => {
// set up fetch API mock
fetchMock.hardReset();
fetchMock.catch(404);
setupFileStubMocks();
fetchMock.get("/_matrix/client/versions", {}, { sticky: true });
fetchMock.mockGlobal();
});
afterEach(() => {
fetchMock.removeRoutes();
window.sessionStorage?.clear();
window.localStorage?.clear();
});
fetchMock.config.allowRelativeUrls = true;
manageFetchMockGlobally(jest);
// set up AudioContext API mock
global.AudioContext = jest.fn().mockImplementation(() => ({ ...mocks.AudioContext }));
global.AudioContext = jest.fn<() => AudioContext>().mockImplementation(() => ({ ...mocks.AudioContext }));