From 0c61944ffd4383eea65c7b8f83ea6acdbf49a81f Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 15 Jul 2026 09:48:51 +0100 Subject: [PATCH] Move more tests over to vitest (#34249) * Move more tests over to vitest * Add exception for test_setting --- .../controllers/AnalyticsController.test.ts} | 14 ++-- .../BlockInvitesConfigController.test.ts} | 24 +++--- .../DeviceIsolationModeController.test.ts} | 13 +-- .../FallbackIceServerController.test.ts} | 15 ++-- .../controllers/FontSizeController.test.ts} | 12 +-- .../IncompatibleController.test.ts} | 14 ++-- .../InviteRulesConfigController.test.ts} | 37 ++++---- .../MediaPreviewConfigController.test.ts} | 45 +++++----- .../RequiresSettingsController.test.ts} | 22 +++-- ...rSupportUnstableFeatureController.test.ts} | 29 ++++--- .../controllers/SystemFontController.test.ts} | 14 ++-- .../notifications/NotificationLevel.test.ts} | 7 +- .../RoomNotificationState.test.ts} | 53 ++++++------ .../utils/AutoDiscoveryUtils.test.ts} | 12 +-- .../utils/DMRoomMap.test.ts} | 24 +++--- .../utils/Feedback.test.ts} | 18 ++-- .../utils/MediaEventHelper.test.ts} | 10 ++- .../utils/MultiInviter.test.ts} | 84 ++++++++++--------- .../utils/SearchInput.test.ts} | 19 +++-- .../utils/ShieldUtils.test.ts} | 17 ++-- .../utils/WidgetUtils.test.ts} | 6 +- .../AutoDiscoveryUtils.test.ts.snap} | 4 +- .../utils/membership.test.ts} | 30 +++---- scripts/find-unused-settings.ts | 2 +- 24 files changed, 292 insertions(+), 233 deletions(-) rename apps/web/{test/unit-tests/settings/controllers/AnalyticsController-test.ts => src/settings/controllers/AnalyticsController.test.ts} (63%) rename apps/web/{test/unit-tests/settings/controllers/BlockInvitesConfigController-test.ts => src/settings/controllers/BlockInvitesConfigController.test.ts} (83%) rename apps/web/{test/unit-tests/settings/controllers/DeviceIsolationModeController-test.ts => src/settings/controllers/DeviceIsolationModeController.test.ts} (79%) rename apps/web/{test/unit-tests/settings/controllers/FallbackIceServerController-test.ts => src/settings/controllers/FallbackIceServerController.test.ts} (80%) rename apps/web/{test/unit-tests/settings/controllers/FontSizeController-test.ts => src/settings/controllers/FontSizeController.test.ts} (63%) rename apps/web/{test/unit-tests/settings/controllers/IncompatibleController-test.ts => src/settings/controllers/IncompatibleController.test.ts} (88%) rename apps/web/{test/unit-tests/settings/controllers/InviteRulesConfigController-test.ts => src/settings/controllers/InviteRulesConfigController.test.ts} (86%) rename apps/web/{test/unit-tests/settings/controllers/MediaPreviewConfigController-test.ts => src/settings/controllers/MediaPreviewConfigController.test.ts} (82%) rename apps/web/{test/unit-tests/settings/controllers/RequiresSettingsController-test.ts => src/settings/controllers/RequiresSettingsController.test.ts} (89%) rename apps/web/{test/unit-tests/settings/controllers/ServerSupportUnstableFeatureController-test.ts => src/settings/controllers/ServerSupportUnstableFeatureController.test.ts} (83%) rename apps/web/{test/unit-tests/settings/controllers/SystemFontController-test.ts => src/settings/controllers/SystemFontController.test.ts} (68%) rename apps/web/{test/unit-tests/stores/notifications/NotificationColor-test.ts => src/stores/notifications/NotificationLevel.test.ts} (85%) rename apps/web/{test/unit-tests/stores/notifications/RoomNotificationState-test.ts => src/stores/notifications/RoomNotificationState.test.ts} (84%) rename apps/web/{test/unit-tests/utils/AutoDiscoveryUtils-test.tsx => src/utils/AutoDiscoveryUtils.test.ts} (96%) rename apps/web/{test/unit-tests/utils/DMRoomMap-test.ts => src/utils/DMRoomMap.test.ts} (91%) rename apps/web/{test/unit-tests/utils/Feedback-test.ts => src/utils/Feedback.test.ts} (74%) rename apps/web/{test/unit-tests/utils/MediaEventHelper-test.ts => src/utils/MediaEventHelper.test.ts} (86%) rename apps/web/{test/unit-tests/utils/MultiInviter-test.ts => src/utils/MultiInviter.test.ts} (79%) rename apps/web/{test/unit-tests/utils/SearchInput-test.ts => src/utils/SearchInput.test.ts} (74%) rename apps/web/{test/unit-tests/utils/ShieldUtils-test.ts => src/utils/ShieldUtils.test.ts} (94%) rename apps/web/{test/unit-tests/utils/WidgetUtils-test.ts => src/utils/WidgetUtils.test.ts} (90%) rename apps/web/{test/unit-tests/utils/__snapshots__/AutoDiscoveryUtils-test.tsx.snap => src/utils/__snapshots__/AutoDiscoveryUtils.test.ts.snap} (73%) rename apps/web/{test/unit-tests/utils/membership-test.ts => src/utils/membership.test.ts} (84%) diff --git a/apps/web/test/unit-tests/settings/controllers/AnalyticsController-test.ts b/apps/web/src/settings/controllers/AnalyticsController.test.ts similarity index 63% rename from apps/web/test/unit-tests/settings/controllers/AnalyticsController-test.ts rename to apps/web/src/settings/controllers/AnalyticsController.test.ts index fb7d4a5613..dfb4113de8 100644 --- a/apps/web/test/unit-tests/settings/controllers/AnalyticsController-test.ts +++ b/apps/web/src/settings/controllers/AnalyticsController.test.ts @@ -6,17 +6,21 @@ 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 PosthogTrackers from "../../../../src/PosthogTrackers"; -import AnalyticsController from "../../../../src/settings/controllers/AnalyticsController"; -import { SettingLevel } from "../../../../src/settings/SettingLevel"; +// @vitest-environment happy-dom + +import { vi, describe, it, expect, afterEach } from "vitest"; + +import PosthogTrackers from "../../PosthogTrackers"; +import AnalyticsController from "../controllers/AnalyticsController"; +import { SettingLevel } from "../SettingLevel"; describe("AnalyticsController", () => { afterEach(() => { - jest.restoreAllMocks(); + vi.restoreAllMocks(); }); it("Tracks a Posthog interaction on change", () => { - const trackInteractionSpy = jest.spyOn(PosthogTrackers, "trackInteraction"); + const trackInteractionSpy = vi.spyOn(PosthogTrackers, "trackInteraction"); const controller = new AnalyticsController("WebSettingsNotificationsTACOnlyNotificationsToggle"); diff --git a/apps/web/test/unit-tests/settings/controllers/BlockInvitesConfigController-test.ts b/apps/web/src/settings/controllers/BlockInvitesConfigController.test.ts similarity index 83% rename from apps/web/test/unit-tests/settings/controllers/BlockInvitesConfigController-test.ts rename to apps/web/src/settings/controllers/BlockInvitesConfigController.test.ts index 55ef127545..5a5fc6d89e 100644 --- a/apps/web/test/unit-tests/settings/controllers/BlockInvitesConfigController-test.ts +++ b/apps/web/src/settings/controllers/BlockInvitesConfigController.test.ts @@ -6,24 +6,26 @@ 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 { type MatrixClient, MatrixEvent } from "matrix-js-sdk/src/matrix"; -import { mocked } from "jest-mock"; +// @vitest-environment happy-dom -import { SETTINGS } from "../../../../src/settings/Settings"; -import { stubClient } from "../../../test-utils"; -import MatrixClientBackedController from "../../../../src/settings/controllers/MatrixClientBackedController"; -import { SettingLevel } from "../../../../src/settings/SettingLevel.ts"; +import { vi, describe, it, expect, beforeEach } from "vitest"; +import { type MatrixClient, MatrixEvent } from "matrix-js-sdk/src/matrix"; +import { stubClient } from "test-utils"; + +import { SETTINGS } from "../Settings"; +import MatrixClientBackedController from "./MatrixClientBackedController"; +import { SettingLevel } from "../SettingLevel.ts"; describe("BlockInvitesConfigController", () => { describe("When server does not support MSC4380", () => { let cli: MatrixClient; beforeEach(() => { cli = stubClient(); - cli.doesServerSupportUnstableFeature = jest.fn(async () => false); + cli.doesServerSupportUnstableFeature = vi.fn(async () => false); MatrixClientBackedController.matrixClient = cli; }); - test("settingDisabled() should give a message", () => { + it("settingDisabled() should give a message", () => { const controller = SETTINGS.blockInvites.controller!; expect(controller.settingDisabled).toEqual("Your server does not implement this feature."); }); @@ -33,13 +35,13 @@ describe("BlockInvitesConfigController", () => { let cli: MatrixClient; beforeEach(async () => { cli = stubClient(); - cli.doesServerSupportUnstableFeature = jest.fn(async (feature) => { + cli.doesServerSupportUnstableFeature = vi.fn(async (feature) => { return feature == "org.matrix.msc4380.stable"; }); MatrixClientBackedController.matrixClient = cli; }); - test("settingDisabled() should be false", () => { + it("settingDisabled() should be false", () => { const controller = SETTINGS.blockInvites.controller!; expect(controller.settingDisabled).toEqual(false); }); @@ -85,7 +87,7 @@ describe("BlockInvitesConfigController", () => { * in response to any request for `m.invite_permission_config`. */ function mockAccountData(cli: MatrixClient, mockAccountData: object) { - mocked(cli.getAccountData).mockImplementation((eventType) => { + vi.mocked(cli.getAccountData).mockImplementation((eventType) => { if (eventType == "m.invite_permission_config") { return new MatrixEvent({ type: "m.invite_permission_config", diff --git a/apps/web/test/unit-tests/settings/controllers/DeviceIsolationModeController-test.ts b/apps/web/src/settings/controllers/DeviceIsolationModeController.test.ts similarity index 79% rename from apps/web/test/unit-tests/settings/controllers/DeviceIsolationModeController-test.ts rename to apps/web/src/settings/controllers/DeviceIsolationModeController.test.ts index dc1829a05f..4ef266ff66 100644 --- a/apps/web/test/unit-tests/settings/controllers/DeviceIsolationModeController-test.ts +++ b/apps/web/src/settings/controllers/DeviceIsolationModeController.test.ts @@ -5,15 +5,18 @@ 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 { AllDevicesIsolationMode, OnlySignedDevicesIsolationMode } from "matrix-js-sdk/src/crypto-api"; +// @vitest-environment happy-dom -import { stubClient } from "../../../test-utils"; -import DeviceIsolationModeController from "../../../../src/settings/controllers/DeviceIsolationModeController.ts"; -import { SettingLevel } from "../../../../src/settings/SettingLevel"; +import { vi, describe, it, expect, afterEach } from "vitest"; +import { AllDevicesIsolationMode, OnlySignedDevicesIsolationMode } from "matrix-js-sdk/src/crypto-api"; +import { stubClient } from "test-utils"; + +import DeviceIsolationModeController from "./DeviceIsolationModeController.ts"; +import { SettingLevel } from "../SettingLevel"; describe("DeviceIsolationModeController", () => { afterEach(() => { - jest.resetAllMocks(); + vi.resetAllMocks(); }); describe("tracks enabling and disabling", () => { diff --git a/apps/web/test/unit-tests/settings/controllers/FallbackIceServerController-test.ts b/apps/web/src/settings/controllers/FallbackIceServerController.test.ts similarity index 80% rename from apps/web/test/unit-tests/settings/controllers/FallbackIceServerController-test.ts rename to apps/web/src/settings/controllers/FallbackIceServerController.test.ts index 7834c4fd91..57019e6851 100644 --- a/apps/web/test/unit-tests/settings/controllers/FallbackIceServerController-test.ts +++ b/apps/web/src/settings/controllers/FallbackIceServerController.test.ts @@ -5,13 +5,16 @@ 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"; +// @vitest-environment happy-dom + +import { vi, describe, it, expect, beforeEach, afterEach } from "vitest"; +import fetchMock from "@fetch-mock/vitest"; import { ClientEvent, MatrixClient } from "matrix-js-sdk/src/matrix"; -import { SettingLevel } from "../../../../src/settings/SettingLevel"; -import FallbackIceServerController from "../../../../src/settings/controllers/FallbackIceServerController.ts"; -import MatrixClientBackedController from "../../../../src/settings/controllers/MatrixClientBackedController.ts"; -import SettingsStore from "../../../../src/settings/SettingsStore.ts"; +import { SettingLevel } from "../SettingLevel"; +import FallbackIceServerController from "./FallbackIceServerController.ts"; +import MatrixClientBackedController from "./MatrixClientBackedController.ts"; +import SettingsStore from "../SettingsStore.ts"; describe("FallbackIceServerController", () => { beforeEach(() => { @@ -19,7 +22,7 @@ describe("FallbackIceServerController", () => { }); afterEach(() => { - jest.restoreAllMocks(); + vi.restoreAllMocks(); }); it("should update MatrixClient's state when the setting is updated", async () => { diff --git a/apps/web/test/unit-tests/settings/controllers/FontSizeController-test.ts b/apps/web/src/settings/controllers/FontSizeController.test.ts similarity index 63% rename from apps/web/test/unit-tests/settings/controllers/FontSizeController-test.ts rename to apps/web/src/settings/controllers/FontSizeController.test.ts index e9dc3ee942..32240f19e3 100644 --- a/apps/web/test/unit-tests/settings/controllers/FontSizeController-test.ts +++ b/apps/web/src/settings/controllers/FontSizeController.test.ts @@ -6,12 +6,14 @@ 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 { Action } from "../../../../src/dispatcher/actions"; -import dis from "../../../../src/dispatcher/dispatcher"; -import FontSizeController from "../../../../src/settings/controllers/FontSizeController"; -import { SettingLevel } from "../../../../src/settings/SettingLevel"; +import { vi, describe, it, expect } from "vitest"; -const dispatchSpy = jest.spyOn(dis, "fire"); +import { Action } from "../../dispatcher/actions"; +import dis from "../../dispatcher/dispatcher"; +import FontSizeController from "./FontSizeController"; +import { SettingLevel } from "../SettingLevel"; + +const dispatchSpy = vi.spyOn(dis, "fire"); describe("FontSizeController", () => { it("dispatches a font size action on change", () => { diff --git a/apps/web/test/unit-tests/settings/controllers/IncompatibleController-test.ts b/apps/web/src/settings/controllers/IncompatibleController.test.ts similarity index 88% rename from apps/web/test/unit-tests/settings/controllers/IncompatibleController-test.ts rename to apps/web/src/settings/controllers/IncompatibleController.test.ts index 6db9a221b9..27f53ad92c 100644 --- a/apps/web/test/unit-tests/settings/controllers/IncompatibleController-test.ts +++ b/apps/web/src/settings/controllers/IncompatibleController.test.ts @@ -6,18 +6,20 @@ 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 IncompatibleController from "../../../../src/settings/controllers/IncompatibleController"; -import { SettingLevel } from "../../../../src/settings/SettingLevel"; -import SettingsStore from "../../../../src/settings/SettingsStore"; +import { vi, describe, it, expect, beforeEach } from "vitest"; -declare module "../../../../src/settings/Settings.tsx" { +import IncompatibleController from "./IncompatibleController"; +import { SettingLevel } from "../SettingLevel"; +import SettingsStore from "../SettingsStore"; + +declare module "../Settings.tsx" { interface Settings { test_setting: IBaseSetting; } } describe("IncompatibleController", () => { - const settingsGetValueSpy = jest.spyOn(SettingsStore, "getValue"); + const settingsGetValueSpy = vi.spyOn(SettingsStore, "getValue"); beforeEach(() => { settingsGetValueSpy.mockClear(); }); @@ -58,7 +60,7 @@ describe("IncompatibleController", () => { describe("when incompatibleValue is set to a function", () => { it("returns result from incompatibleValue function", () => { - const incompatibleValueFn = jest.fn().mockReturnValue(false); + const incompatibleValueFn = vi.fn().mockReturnValue(false); const controller = new IncompatibleController("test_setting", { key: null }, incompatibleValueFn); settingsGetValueSpy.mockReturnValue("test"); expect(controller.incompatibleSetting).toBe(false); diff --git a/apps/web/test/unit-tests/settings/controllers/InviteRulesConfigController-test.ts b/apps/web/src/settings/controllers/InviteRulesConfigController.test.ts similarity index 86% rename from apps/web/test/unit-tests/settings/controllers/InviteRulesConfigController-test.ts rename to apps/web/src/settings/controllers/InviteRulesConfigController.test.ts index 516b1641f5..ef1902eee5 100644 --- a/apps/web/test/unit-tests/settings/controllers/InviteRulesConfigController-test.ts +++ b/apps/web/src/settings/controllers/InviteRulesConfigController.test.ts @@ -5,17 +5,20 @@ 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 { MatrixEvent } from "matrix-js-sdk/src/matrix"; +// @vitest-environment happy-dom -import MatrixClientBackedController from "../../../../src/settings/controllers/MatrixClientBackedController"; -import InviteRulesConfigController from "../../../../src/settings/controllers/InviteRulesConfigController"; -import { SettingLevel } from "../../../../src/settings/SettingLevel"; -import { getMockClientWithEventEmitter, mockClientMethodsServer } from "../../../test-utils"; -import { INVITE_RULES_ACCOUNT_DATA_TYPE, type InviteConfigAccountData } from "../../../../src/@types/invite-rules"; +import { vi, describe, it, expect, afterEach } from "vitest"; +import { MatrixEvent } from "matrix-js-sdk/src/matrix"; +import { getMockClientWithEventEmitter, mockClientMethodsServer } from "test-utils"; + +import MatrixClientBackedController from "./MatrixClientBackedController"; +import InviteRulesConfigController from "./InviteRulesConfigController"; +import { SettingLevel } from "../SettingLevel"; +import { INVITE_RULES_ACCOUNT_DATA_TYPE, type InviteConfigAccountData } from "../../@types/invite-rules"; describe("InviteRulesConfigController", () => { afterEach(() => { - jest.restoreAllMocks(); + vi.restoreAllMocks(); }); it("gets the default settings when none are specified.", () => { @@ -23,7 +26,7 @@ describe("InviteRulesConfigController", () => { MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({ ...mockClientMethodsServer(), - getAccountData: jest.fn().mockReturnValue(null), + getAccountData: vi.fn().mockReturnValue(null), }); const value = controller.getValueOverride(SettingLevel.ACCOUNT); @@ -35,7 +38,7 @@ describe("InviteRulesConfigController", () => { MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({ ...mockClientMethodsServer(), - getAccountData: jest + getAccountData: vi .fn() .mockReturnValue(new MatrixEvent({ type: INVITE_RULES_ACCOUNT_DATA_TYPE, content: {} })), }); @@ -51,7 +54,7 @@ describe("InviteRulesConfigController", () => { MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({ ...mockClientMethodsServer(), - getAccountData: jest.fn().mockReturnValue( + getAccountData: vi.fn().mockReturnValue( new MatrixEvent({ type: INVITE_RULES_ACCOUNT_DATA_TYPE, content, @@ -73,7 +76,7 @@ describe("InviteRulesConfigController", () => { MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({ ...mockClientMethodsServer(), - getAccountData: jest.fn().mockReturnValue( + getAccountData: vi.fn().mockReturnValue( new MatrixEvent({ type: INVITE_RULES_ACCOUNT_DATA_TYPE, content, @@ -89,7 +92,7 @@ describe("InviteRulesConfigController", () => { const controller = new InviteRulesConfigController(); const client = (MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({ ...mockClientMethodsServer(), - getAccountData: jest.fn().mockReturnValue( + getAccountData: vi.fn().mockReturnValue( new MatrixEvent({ type: INVITE_RULES_ACCOUNT_DATA_TYPE, content: { @@ -98,7 +101,7 @@ describe("InviteRulesConfigController", () => { }, }), ), - setAccountData: jest.fn(), + setAccountData: vi.fn(), })); expect(await controller.beforeChange(SettingLevel.ACCOUNT, null, { allBlocked: true })).toBe(true); @@ -113,7 +116,7 @@ describe("InviteRulesConfigController", () => { const controller = new InviteRulesConfigController(); const client = (MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({ ...mockClientMethodsServer(), - getAccountData: jest.fn().mockReturnValue( + getAccountData: vi.fn().mockReturnValue( new MatrixEvent({ type: INVITE_RULES_ACCOUNT_DATA_TYPE, content: { @@ -123,7 +126,7 @@ describe("InviteRulesConfigController", () => { }, }), ), - setAccountData: jest.fn(), + setAccountData: vi.fn(), })); expect(await controller.beforeChange(SettingLevel.ACCOUNT, null, { allBlocked: false })).toBe(true); @@ -137,7 +140,7 @@ describe("InviteRulesConfigController", () => { const controller = new InviteRulesConfigController(); const client = (MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({ ...mockClientMethodsServer(), - getAccountData: jest.fn().mockReturnValue( + getAccountData: vi.fn().mockReturnValue( new MatrixEvent({ type: INVITE_RULES_ACCOUNT_DATA_TYPE, content: { @@ -147,7 +150,7 @@ describe("InviteRulesConfigController", () => { }, }), ), - setAccountData: jest.fn(), + setAccountData: vi.fn(), })); expect(await controller.beforeChange(SettingLevel.ACCOUNT, null, { allBlocked })).toBe(false); diff --git a/apps/web/test/unit-tests/settings/controllers/MediaPreviewConfigController-test.ts b/apps/web/src/settings/controllers/MediaPreviewConfigController.test.ts similarity index 82% rename from apps/web/test/unit-tests/settings/controllers/MediaPreviewConfigController-test.ts rename to apps/web/src/settings/controllers/MediaPreviewConfigController.test.ts index ec93afb799..2e81994475 100644 --- a/apps/web/test/unit-tests/settings/controllers/MediaPreviewConfigController-test.ts +++ b/apps/web/src/settings/controllers/MediaPreviewConfigController.test.ts @@ -5,17 +5,20 @@ 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 { MatrixEvent } from "matrix-js-sdk/src/matrix"; +// @vitest-environment happy-dom -import MatrixClientBackedController from "../../../../src/settings/controllers/MatrixClientBackedController"; -import MediaPreviewConfigController from "../../../../src/settings/controllers/MediaPreviewConfigController"; -import { SettingLevel } from "../../../../src/settings/SettingLevel"; -import { getMockClientWithEventEmitter, mockClientMethodsServer } from "../../../test-utils"; -import { MEDIA_PREVIEW_ACCOUNT_DATA_TYPE, MediaPreviewValue } from "../../../../src/@types/media_preview"; +import { vi, describe, it, expect, afterEach } from "vitest"; +import { MatrixEvent } from "matrix-js-sdk/src/matrix"; +import { getMockClientWithEventEmitter, mockClientMethodsServer } from "test-utils"; + +import MatrixClientBackedController from "./MatrixClientBackedController"; +import MediaPreviewConfigController from "./MediaPreviewConfigController"; +import { SettingLevel } from "../SettingLevel"; +import { MEDIA_PREVIEW_ACCOUNT_DATA_TYPE, MediaPreviewValue } from "../../@types/media_preview"; describe("MediaPreviewConfigController", () => { afterEach(() => { - jest.restoreAllMocks(); + vi.restoreAllMocks(); }); const ROOM_ID = "!room:example.org"; @@ -25,7 +28,7 @@ describe("MediaPreviewConfigController", () => { MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({ ...mockClientMethodsServer(), - getAccountData: jest.fn().mockReturnValue(null), + getAccountData: vi.fn().mockReturnValue(null), }); const value = controller.getValueOverride(SettingLevel.ACCOUNT, null); @@ -37,7 +40,7 @@ describe("MediaPreviewConfigController", () => { MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({ ...mockClientMethodsServer(), - getAccountData: jest + getAccountData: vi .fn() .mockReturnValue(new MatrixEvent({ type: MEDIA_PREVIEW_ACCOUNT_DATA_TYPE, content: {} })), }); @@ -51,7 +54,7 @@ describe("MediaPreviewConfigController", () => { MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({ ...mockClientMethodsServer(), - getAccountData: jest.fn().mockReturnValue( + getAccountData: vi.fn().mockReturnValue( new MatrixEvent({ type: MEDIA_PREVIEW_ACCOUNT_DATA_TYPE, content: { @@ -59,8 +62,8 @@ describe("MediaPreviewConfigController", () => { }, }), ), - getRoom: jest.fn().mockReturnValue({ - getAccountData: jest.fn().mockReturnValue(null), + getRoom: vi.fn().mockReturnValue({ + getAccountData: vi.fn().mockReturnValue(null), }), }); @@ -77,9 +80,9 @@ describe("MediaPreviewConfigController", () => { MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({ ...mockClientMethodsServer(), - getAccountData: jest.fn().mockReturnValue(null), - getRoom: jest.fn().mockReturnValue({ - getAccountData: jest.fn().mockReturnValue( + getAccountData: vi.fn().mockReturnValue(null), + getRoom: vi.fn().mockReturnValue({ + getAccountData: vi.fn().mockReturnValue( new MatrixEvent({ type: MEDIA_PREVIEW_ACCOUNT_DATA_TYPE, content: { @@ -105,7 +108,7 @@ describe("MediaPreviewConfigController", () => { MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({ ...mockClientMethodsServer(), - getAccountData: jest.fn().mockReturnValue( + getAccountData: vi.fn().mockReturnValue( new MatrixEvent({ type: MEDIA_PREVIEW_ACCOUNT_DATA_TYPE, content: { @@ -113,8 +116,8 @@ describe("MediaPreviewConfigController", () => { }, }), ), - getRoom: jest.fn().mockReturnValue({ - getAccountData: jest.fn().mockReturnValue(null), + getRoom: vi.fn().mockReturnValue({ + getAccountData: vi.fn().mockReturnValue(null), }), }); @@ -133,7 +136,7 @@ describe("MediaPreviewConfigController", () => { MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({ ...mockClientMethodsServer(), - getAccountData: jest.fn().mockReturnValue( + getAccountData: vi.fn().mockReturnValue( new MatrixEvent({ type: MEDIA_PREVIEW_ACCOUNT_DATA_TYPE, content: { @@ -141,8 +144,8 @@ describe("MediaPreviewConfigController", () => { }, }), ), - getRoom: jest.fn().mockReturnValue({ - getAccountData: jest.fn().mockReturnValue( + getRoom: vi.fn().mockReturnValue({ + getAccountData: vi.fn().mockReturnValue( new MatrixEvent({ type: MEDIA_PREVIEW_ACCOUNT_DATA_TYPE, content: { diff --git a/apps/web/test/unit-tests/settings/controllers/RequiresSettingsController-test.ts b/apps/web/src/settings/controllers/RequiresSettingsController.test.ts similarity index 89% rename from apps/web/test/unit-tests/settings/controllers/RequiresSettingsController-test.ts rename to apps/web/src/settings/controllers/RequiresSettingsController.test.ts index 36335d9323..a337e0a282 100644 --- a/apps/web/test/unit-tests/settings/controllers/RequiresSettingsController-test.ts +++ b/apps/web/src/settings/controllers/RequiresSettingsController.test.ts @@ -5,12 +5,16 @@ 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 type { Capabilities } from "matrix-js-sdk/src/matrix"; -import RequiresSettingsController from "../../../../src/settings/controllers/RequiresSettingsController"; -import { SettingLevel } from "../../../../src/settings/SettingLevel"; -import SettingsStore from "../../../../src/settings/SettingsStore"; -import MatrixClientBackedController from "../../../../src/settings/controllers/MatrixClientBackedController"; -import { getMockClientWithEventEmitter, mockClientMethodsServer } from "../../../test-utils"; +// @vitest-environment happy-dom + +import { vi, describe, it, expect, beforeEach, afterEach } from "vitest"; +import { type Capabilities } from "matrix-js-sdk/src/matrix"; +import { getMockClientWithEventEmitter, mockClientMethodsServer } from "test-utils"; + +import RequiresSettingsController from "./RequiresSettingsController"; +import { SettingLevel } from "../SettingLevel"; +import SettingsStore from "../SettingsStore"; +import MatrixClientBackedController from "./MatrixClientBackedController"; describe("RequiresSettingsController", () => { afterEach(() => { @@ -39,14 +43,14 @@ describe("RequiresSettingsController", () => { beforeEach(() => { client = getMockClientWithEventEmitter({ ...mockClientMethodsServer(), - getCachedCapabilities: jest.fn().mockImplementation(() => {}), - getCapabilities: jest.fn().mockRejectedValue({}), + getCachedCapabilities: vi.fn().mockImplementation(() => {}), + getCapabilities: vi.fn().mockRejectedValue({}), }); MatrixClientBackedController["_matrixClient"] = client; }); afterEach(() => { - jest.restoreAllMocks(); + vi.restoreAllMocks(); }); it("will disable setting if capability check is true", async () => { diff --git a/apps/web/test/unit-tests/settings/controllers/ServerSupportUnstableFeatureController-test.ts b/apps/web/src/settings/controllers/ServerSupportUnstableFeatureController.test.ts similarity index 83% rename from apps/web/test/unit-tests/settings/controllers/ServerSupportUnstableFeatureController-test.ts rename to apps/web/src/settings/controllers/ServerSupportUnstableFeatureController.test.ts index 1bd80e2535..0819267453 100644 --- a/apps/web/test/unit-tests/settings/controllers/ServerSupportUnstableFeatureController-test.ts +++ b/apps/web/src/settings/controllers/ServerSupportUnstableFeatureController.test.ts @@ -6,14 +6,17 @@ 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 { type MatrixClient } from "matrix-js-sdk/src/matrix"; +// @vitest-environment happy-dom -import ServerSupportUnstableFeatureController from "../../../../src/settings/controllers/ServerSupportUnstableFeatureController"; -import { SettingLevel } from "../../../../src/settings/SettingLevel"; -import { type FeatureSettingKey, LabGroup, SETTINGS } from "../../../../src/settings/Settings"; -import { stubClient } from "../../../test-utils"; -import { WatchManager } from "../../../../src/settings/WatchManager"; -import MatrixClientBackedController from "../../../../src/settings/controllers/MatrixClientBackedController"; +import { vi, describe, it, expect } from "vitest"; +import { type MatrixClient } from "matrix-js-sdk/src/matrix"; +import { stubClient } from "test-utils"; + +import ServerSupportUnstableFeatureController from "./ServerSupportUnstableFeatureController"; +import { SettingLevel } from "../SettingLevel"; +import { type FeatureSettingKey, LabGroup, SETTINGS } from "../Settings"; +import { WatchManager } from "../WatchManager"; +import MatrixClientBackedController from "./MatrixClientBackedController"; describe("ServerSupportUnstableFeatureController", () => { const watchers = new WatchManager(); @@ -41,7 +44,7 @@ describe("ServerSupportUnstableFeatureController", () => { describe("getValueOverride()", () => { it("should return forced value is setting is disabled", async () => { const cli = stubClient(); - cli.doesServerSupportUnstableFeature = jest.fn(async () => false); + cli.doesServerSupportUnstableFeature = vi.fn(async () => false); const controller = new ServerSupportUnstableFeatureController( setting, @@ -60,7 +63,7 @@ describe("ServerSupportUnstableFeatureController", () => { it("should pass through to the handler if setting is not disabled", async () => { const cli = stubClient(); - cli.doesServerSupportUnstableFeature = jest.fn(async () => true); + cli.doesServerSupportUnstableFeature = vi.fn(async () => true); const controller = new ServerSupportUnstableFeatureController( setting, @@ -82,7 +85,7 @@ describe("ServerSupportUnstableFeatureController", () => { it("considered disabled if not all required features in the only feature group are supported", async () => { const cli = stubClient(); - cli.doesServerSupportUnstableFeature = jest.fn(async (featureName) => { + cli.doesServerSupportUnstableFeature = vi.fn(async (featureName) => { return featureName === "org.matrix.msc3827.stable"; }); @@ -96,7 +99,7 @@ describe("ServerSupportUnstableFeatureController", () => { it("considered enabled if all required features in the only feature group are supported", async () => { const cli = stubClient(); - cli.doesServerSupportUnstableFeature = jest.fn(async (featureName) => { + cli.doesServerSupportUnstableFeature = vi.fn(async (featureName) => { return featureName === "org.matrix.msc3827.stable" || featureName === "org.matrix.msc3030"; }); const controller = new ServerSupportUnstableFeatureController(setting, watchers, [ @@ -109,7 +112,7 @@ describe("ServerSupportUnstableFeatureController", () => { it("considered enabled if all required features in one of the feature groups are supported", async () => { const cli = stubClient(); - cli.doesServerSupportUnstableFeature = jest.fn(async (featureName) => { + cli.doesServerSupportUnstableFeature = vi.fn(async (featureName) => { return featureName === "org.matrix.msc3827.stable" || featureName === "org.matrix.msc3030"; }); const controller = new ServerSupportUnstableFeatureController(setting, watchers, [ @@ -123,7 +126,7 @@ describe("ServerSupportUnstableFeatureController", () => { it("considered disabled if not all required features in one of the feature groups are supported", async () => { const cli = stubClient(); - cli.doesServerSupportUnstableFeature = jest.fn(async (featureName) => { + cli.doesServerSupportUnstableFeature = vi.fn(async (featureName) => { return featureName === "org.matrix.msc3827.stable"; }); diff --git a/apps/web/test/unit-tests/settings/controllers/SystemFontController-test.ts b/apps/web/src/settings/controllers/SystemFontController.test.ts similarity index 68% rename from apps/web/test/unit-tests/settings/controllers/SystemFontController-test.ts rename to apps/web/src/settings/controllers/SystemFontController.test.ts index 13212ce650..45553bbc2e 100644 --- a/apps/web/test/unit-tests/settings/controllers/SystemFontController-test.ts +++ b/apps/web/src/settings/controllers/SystemFontController.test.ts @@ -6,18 +6,20 @@ 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 { Action } from "../../../../src/dispatcher/actions"; -import dis from "../../../../src/dispatcher/dispatcher"; -import SystemFontController from "../../../../src/settings/controllers/SystemFontController"; -import SettingsStore from "../../../../src/settings/SettingsStore"; +import { vi, describe, it, expect } from "vitest"; -const dispatchSpy = jest.spyOn(dis, "dispatch"); +import { Action } from "../../dispatcher/actions"; +import dis from "../../dispatcher/dispatcher"; +import SystemFontController from "./SystemFontController"; +import SettingsStore from "../SettingsStore"; + +const dispatchSpy = vi.spyOn(dis, "dispatch"); describe("SystemFontController", () => { it("dispatches a system font update action on change", () => { const controller = new SystemFontController(); - const getValueSpy = jest.spyOn(SettingsStore, "getValue").mockImplementation((settingName): any => { + const getValueSpy = vi.spyOn(SettingsStore, "getValue").mockImplementation((settingName): any => { if (settingName === "useBundledEmojiFont") return false; if (settingName === "useSystemFont") return true; if (settingName === "systemFont") return "Comic Sans MS"; diff --git a/apps/web/test/unit-tests/stores/notifications/NotificationColor-test.ts b/apps/web/src/stores/notifications/NotificationLevel.test.ts similarity index 85% rename from apps/web/test/unit-tests/stores/notifications/NotificationColor-test.ts rename to apps/web/src/stores/notifications/NotificationLevel.test.ts index f6ad12829b..12f28d606c 100644 --- a/apps/web/test/unit-tests/stores/notifications/NotificationColor-test.ts +++ b/apps/web/src/stores/notifications/NotificationLevel.test.ts @@ -6,10 +6,9 @@ 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 { - humanReadableNotificationLevel, - NotificationLevel, -} from "../../../../src/stores/notifications/NotificationLevel"; +import { describe, it, expect } from "vitest"; + +import { humanReadableNotificationLevel, NotificationLevel } from "./NotificationLevel"; describe("NotificationLevel", () => { describe("humanReadableNotificationLevel", () => { diff --git a/apps/web/test/unit-tests/stores/notifications/RoomNotificationState-test.ts b/apps/web/src/stores/notifications/RoomNotificationState.test.ts similarity index 84% rename from apps/web/test/unit-tests/stores/notifications/RoomNotificationState-test.ts rename to apps/web/src/stores/notifications/RoomNotificationState.test.ts index 3ae4958de6..5e5a04d108 100644 --- a/apps/web/test/unit-tests/stores/notifications/RoomNotificationState-test.ts +++ b/apps/web/src/stores/notifications/RoomNotificationState.test.ts @@ -6,6 +6,9 @@ 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. */ +// @vitest-environment happy-dom + +import { vi, describe, it, expect, beforeEach, afterEach } from "vitest"; import { Room, MatrixEventEvent, @@ -17,15 +20,15 @@ import { RoomEvent, } from "matrix-js-sdk/src/matrix"; import { KnownMembership } from "matrix-js-sdk/src/types"; +import { mkEvent, muteRoom, stubClient } from "test-utils"; +import { createMessageEventContent } from "test-utils/events"; import type { MatrixClient } from "matrix-js-sdk/src/matrix"; -import { mkEvent, muteRoom, stubClient } from "../../../test-utils"; -import { RoomNotificationState } from "../../../../src/stores/notifications/RoomNotificationState"; -import { NotificationStateEvents } from "../../../../src/stores/notifications/NotificationState"; -import { NotificationLevel } from "../../../../src/stores/notifications/NotificationLevel"; -import { createMessageEventContent } from "../../../test-utils/events"; -import SettingsStore from "../../../../src/settings/SettingsStore"; -import * as UnreadModule from "../../../../src/Unread"; +import { RoomNotificationState } from "./RoomNotificationState"; +import { NotificationStateEvents } from "./NotificationState"; +import { NotificationLevel } from "./NotificationLevel"; +import SettingsStore from "../../settings/SettingsStore"; +import * as UnreadModule from "../../Unread"; describe("RoomNotificationState", () => { let room: Room; @@ -39,12 +42,12 @@ describe("RoomNotificationState", () => { }); afterEach(() => { - jest.resetAllMocks(); + vi.resetAllMocks(); }); function addThread(room: Room): void { const threadId = "thread_id"; - jest.spyOn(room, "eventShouldLiveIn").mockReturnValue({ + vi.spyOn(room, "eventShouldLiveIn").mockReturnValue({ shouldLiveInRoom: true, shouldLiveInThread: true, threadId, @@ -82,19 +85,19 @@ describe("RoomNotificationState", () => { it("updates on event decryption", () => { const roomNotifState = new RoomNotificationState(room, true); - const listener = jest.fn(); + const listener = vi.fn(); roomNotifState.addListener(NotificationStateEvents.Update, listener); const testEvent = { getRoomId: () => room.roomId, } as unknown as MatrixEvent; - room.getUnreadNotificationCount = jest.fn().mockReturnValue(1); + room.getUnreadNotificationCount = vi.fn().mockReturnValue(1); client.emit(MatrixEventEvent.Decrypted, testEvent); expect(listener).toHaveBeenCalled(); }); it("emits an Update event on marked unread room account data", () => { const roomNotifState = new RoomNotificationState(room, true); - const listener = jest.fn(); + const listener = vi.fn(); roomNotifState.addListener(NotificationStateEvents.Update, listener); const accountDataEvent = { getType: () => "m.marked_unread", @@ -102,14 +105,14 @@ describe("RoomNotificationState", () => { return { unread: true }; }, } as unknown as MatrixEvent; - room.getAccountData = jest.fn().mockReturnValue(accountDataEvent); + room.getAccountData = vi.fn().mockReturnValue(accountDataEvent); room.emit(RoomEvent.AccountData, accountDataEvent, room); expect(listener).toHaveBeenCalled(); }); it("does not update on other account data", () => { const roomNotifState = new RoomNotificationState(room, true); - const listener = jest.fn(); + const listener = vi.fn(); roomNotifState.addListener(NotificationStateEvents.Update, listener); const accountDataEvent = { getType: () => "else.something", @@ -117,7 +120,7 @@ describe("RoomNotificationState", () => { return {}; }, } as unknown as MatrixEvent; - room.getAccountData = jest.fn().mockReturnValue(accountDataEvent); + room.getAccountData = vi.fn().mockReturnValue(accountDataEvent); room.emit(RoomEvent.AccountData, accountDataEvent, room); expect(listener).not.toHaveBeenCalled(); }); @@ -209,8 +212,8 @@ describe("RoomNotificationState", () => { describe("computed attributes", () => { beforeEach(() => { - jest.spyOn(room, "getPendingEvents").mockReturnValue([]); - jest.spyOn(UnreadModule, "doesRoomHaveUnreadMessages").mockReturnValue(false); + vi.spyOn(room, "getPendingEvents").mockReturnValue([]); + vi.spyOn(UnreadModule, "doesRoomHaveUnreadMessages").mockReturnValue(false); }); it("should has invited at true", () => { @@ -220,7 +223,7 @@ describe("RoomNotificationState", () => { }); it("should has isUnsetMessage at true", () => { - jest.spyOn(room, "getPendingEvents").mockReturnValue([ + vi.spyOn(room, "getPendingEvents").mockReturnValue([ mkEvent({ status: EventStatus.NOT_SENT, user: "@foobar:example.org", type: "any.event", content: {} }), ]); const roomNotifState = new RoomNotificationState(room, false); @@ -236,11 +239,11 @@ describe("RoomNotificationState", () => { room.updateMyMembership(KnownMembership.Invite); expect(roomNotifState.isMention).toBe(false); - jest.spyOn(SettingsStore, "getValue").mockReturnValue(true); + vi.spyOn(SettingsStore, "getValue").mockReturnValue(true); room.updateMyMembership(KnownMembership.Knock); expect(roomNotifState.isMention).toBe(false); - jest.spyOn(room, "getPendingEvents").mockReturnValue([ + vi.spyOn(room, "getPendingEvents").mockReturnValue([ mkEvent({ status: EventStatus.NOT_SENT, user: "@foobar:example.org", type: "any.event", content: {} }), ]); room.updateMyMembership(KnownMembership.Join); @@ -255,7 +258,7 @@ describe("RoomNotificationState", () => { }); it("should has isActivityNotification at true", () => { - jest.spyOn(UnreadModule, "doesRoomHaveUnreadMessages").mockReturnValue(true); + vi.spyOn(UnreadModule, "doesRoomHaveUnreadMessages").mockReturnValue(true); const roomNotifState = new RoomNotificationState(room, false); expect(roomNotifState.isActivityNotification).toBe(true); @@ -263,9 +266,9 @@ describe("RoomNotificationState", () => { it("should has hasAnyNotificationOrActivity at true", () => { // Hidebold is disabled - jest.spyOn(SettingsStore, "getValue").mockReturnValue(false); + vi.spyOn(SettingsStore, "getValue").mockReturnValue(false); // Unread message, generate activity notification - jest.spyOn(UnreadModule, "doesRoomHaveUnreadMessages").mockReturnValue(true); + vi.spyOn(UnreadModule, "doesRoomHaveUnreadMessages").mockReturnValue(true); // Highlight notification setUnreads(room, 0, 1); @@ -281,12 +284,12 @@ describe("RoomNotificationState", () => { expect(roomNotifState.hasAnyNotificationOrActivity).toBe(true); // hidebold is enabled and we have an activity notification - jest.spyOn(SettingsStore, "getValue").mockReturnValue(true); + vi.spyOn(SettingsStore, "getValue").mockReturnValue(true); room.updateMyMembership(KnownMembership.Join); expect(roomNotifState.hasAnyNotificationOrActivity).toBe(false); // No unread - jest.spyOn(UnreadModule, "doesRoomHaveUnreadMessages").mockReturnValue(false); + vi.spyOn(UnreadModule, "doesRoomHaveUnreadMessages").mockReturnValue(false); room.updateMyMembership(KnownMembership.Join); expect(roomNotifState.hasAnyNotificationOrActivity).toBe(false); }); diff --git a/apps/web/test/unit-tests/utils/AutoDiscoveryUtils-test.tsx b/apps/web/src/utils/AutoDiscoveryUtils.test.ts similarity index 96% rename from apps/web/test/unit-tests/utils/AutoDiscoveryUtils-test.tsx rename to apps/web/src/utils/AutoDiscoveryUtils.test.ts index a4122578cf..ecfec22c24 100644 --- a/apps/web/test/unit-tests/utils/AutoDiscoveryUtils-test.tsx +++ b/apps/web/src/utils/AutoDiscoveryUtils.test.ts @@ -6,15 +6,17 @@ 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 { vi, describe, it, expect, afterAll, beforeEach } from "vitest"; +import { makeDelegatedAuthMetadata } from "test-utils/auth"; import { AutoDiscovery, AutoDiscoveryAction, type ClientConfig } from "matrix-js-sdk/src/matrix"; import { logger } from "matrix-js-sdk/src/logger"; -import fetchMock from "@fetch-mock/jest"; +import fetchMock from "@fetch-mock/vitest"; -import AutoDiscoveryUtils from "../../../src/utils/AutoDiscoveryUtils"; -import { makeDelegatedAuthMetadata } from "../../test-utils/auth"; +import AutoDiscoveryUtils from "./AutoDiscoveryUtils"; describe("AutoDiscoveryUtils", () => { beforeEach(() => { + fetchMock.mockReset(); fetchMock.catch({ status: 404, body: '{"errcode": "M_UNRECOGNIZED", "error": "Unrecognized request"}', @@ -27,13 +29,13 @@ describe("AutoDiscoveryUtils", () => { beforeEach(() => { // don't litter console with expected errors - jest.spyOn(logger, "error") + vi.spyOn(logger, "error") .mockClear() .mockImplementation(() => {}); }); afterAll(() => { - jest.spyOn(logger, "error").mockRestore(); + vi.spyOn(logger, "error").mockRestore(); }); const validIsConfig = { diff --git a/apps/web/test/unit-tests/utils/DMRoomMap-test.ts b/apps/web/src/utils/DMRoomMap.test.ts similarity index 91% rename from apps/web/test/unit-tests/utils/DMRoomMap-test.ts rename to apps/web/src/utils/DMRoomMap.test.ts index 69360a277d..99fd2c7844 100644 --- a/apps/web/test/unit-tests/utils/DMRoomMap-test.ts +++ b/apps/web/src/utils/DMRoomMap.test.ts @@ -6,7 +6,10 @@ 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 { mocked, type Mocked } from "jest-mock"; +// @vitest-environment happy-dom + +import { vi, describe, it, expect, beforeEach, type Mocked } from "vitest"; + import { logger } from "matrix-js-sdk/src/logger"; import { ClientEvent, @@ -16,9 +19,10 @@ import { type MatrixEvent, type Room, } from "matrix-js-sdk/src/matrix"; +import { mkEvent, stubClient } from "test-utils"; + +import DMRoomMap from "./DMRoomMap"; -import DMRoomMap from "../../../src/utils/DMRoomMap"; -import { mkEvent, stubClient } from "../../test-utils"; describe("DMRoomMap", () => { const roomId1 = "!room1:example.com"; const roomId2 = "!room2:example.com"; @@ -44,8 +48,8 @@ describe("DMRoomMap", () => { }; beforeEach(() => { - client = mocked(stubClient()); - jest.spyOn(logger, "warn"); + client = vi.mocked(stubClient()); + vi.spyOn(logger, "warn"); }); describe("when m.direct has valid content", () => { @@ -140,19 +144,19 @@ describe("DMRoomMap", () => { describe("getUniqueRoomsWithIndividuals()", () => { const bigRoom = { roomId: "!bigRoom:server.org", - getInvitedAndJoinedMemberCount: jest.fn().mockReturnValue(5000), + getInvitedAndJoinedMemberCount: vi.fn().mockReturnValue(5000), } as unknown as Room; const dmWithBob = { roomId: "!dmWithBob:server.org", - getInvitedAndJoinedMemberCount: jest.fn().mockReturnValue(2), + getInvitedAndJoinedMemberCount: vi.fn().mockReturnValue(2), } as unknown as Room; const dmWithCharlie = { roomId: "!dmWithCharlie:server.org", - getInvitedAndJoinedMemberCount: jest.fn().mockReturnValue(2), + getInvitedAndJoinedMemberCount: vi.fn().mockReturnValue(2), } as unknown as Room; const smallRoom = { roomId: "!smallRoom:server.org", - getInvitedAndJoinedMemberCount: jest.fn().mockReturnValue(3), + getInvitedAndJoinedMemberCount: vi.fn().mockReturnValue(3), } as unknown as Room; const mDirectContent = { @@ -163,7 +167,7 @@ describe("DMRoomMap", () => { beforeEach(() => { client.getAccountData.mockReturnValue(mkMDirectEvent(mDirectContent)); client.getRoom.mockImplementation( - (roomId: string) => + (roomId?: string) => [bigRoom, smallRoom, dmWithCharlie, dmWithBob].find((room) => room.roomId === roomId) ?? null, ); }); diff --git a/apps/web/test/unit-tests/utils/Feedback-test.ts b/apps/web/src/utils/Feedback.test.ts similarity index 74% rename from apps/web/test/unit-tests/utils/Feedback-test.ts rename to apps/web/src/utils/Feedback.test.ts index 089b1cefc1..31cf036cbd 100644 --- a/apps/web/test/unit-tests/utils/Feedback-test.ts +++ b/apps/web/src/utils/Feedback.test.ts @@ -6,18 +6,20 @@ 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 SdkConfig from "../../../src/SdkConfig"; -import { shouldShowFeedback } from "../../../src/utils/Feedback"; -import SettingsStore from "../../../src/settings/SettingsStore"; -import { UIFeature } from "../../../src/settings/UIFeature"; -import { BugReportEndpointURLLocal } from "../../../src/IConfigOptions"; +import { vi, describe, it, expect, afterEach } from "vitest"; + +import SdkConfig from "../SdkConfig"; +import { shouldShowFeedback } from "./Feedback"; +import SettingsStore from "../settings/SettingsStore"; +import { UIFeature } from "../settings/UIFeature"; +import { BugReportEndpointURLLocal } from "../IConfigOptions"; const realGetValue = SettingsStore.getValue; describe("shouldShowFeedback", () => { afterEach(() => { SdkConfig.reset(); - jest.restoreAllMocks(); + vi.restoreAllMocks(); }); it("should return false if bug_report_endpoint_url is falsey", () => { @@ -35,7 +37,7 @@ describe("shouldShowFeedback", () => { }); it("should return false if UIFeature.Feedback is disabled", () => { - jest.spyOn(SettingsStore, "getValue").mockImplementation((key, ...params) => { + vi.spyOn(SettingsStore, "getValue").mockImplementation((key, ...params) => { if (key === UIFeature.Feedback) { return false; } @@ -48,7 +50,7 @@ describe("shouldShowFeedback", () => { SdkConfig.put({ bug_report_endpoint_url: "https://rageshake.server", }); - jest.spyOn(SettingsStore, "getValue").mockImplementation((key, ...params) => { + vi.spyOn(SettingsStore, "getValue").mockImplementation((key, ...params) => { if (key === UIFeature.Feedback) { return true; } diff --git a/apps/web/test/unit-tests/utils/MediaEventHelper-test.ts b/apps/web/src/utils/MediaEventHelper.test.ts similarity index 86% rename from apps/web/test/unit-tests/utils/MediaEventHelper-test.ts rename to apps/web/src/utils/MediaEventHelper.test.ts index 803924f74c..32dda7ad72 100644 --- a/apps/web/test/unit-tests/utils/MediaEventHelper-test.ts +++ b/apps/web/src/utils/MediaEventHelper.test.ts @@ -5,10 +5,14 @@ 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 { MatrixEvent } from "matrix-js-sdk/src/matrix"; +// @vitest-environment happy-dom -import { MediaEventHelper } from "../../../src/utils/MediaEventHelper.ts"; -import { stubClient } from "../../test-utils"; +import { describe, it, expect } from "vitest"; + +import { MatrixEvent } from "matrix-js-sdk/src/matrix"; +import { stubClient } from "test-utils"; + +import { MediaEventHelper } from "./MediaEventHelper.ts"; describe("MediaEventHelper", () => { it("should set the mime type on the blob based on the event metadata", async () => { diff --git a/apps/web/test/unit-tests/utils/MultiInviter-test.ts b/apps/web/src/utils/MultiInviter.test.ts similarity index 79% rename from apps/web/test/unit-tests/utils/MultiInviter-test.ts rename to apps/web/src/utils/MultiInviter.test.ts index eda6d9baa6..f23434c0a8 100644 --- a/apps/web/test/unit-tests/utils/MultiInviter-test.ts +++ b/apps/web/src/utils/MultiInviter.test.ts @@ -6,17 +6,19 @@ 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 { mocked } from "jest-mock"; +// @vitest-environment happy-dom + +import { vi, describe, it, expect, beforeEach, type Mocked } from "vitest"; import { EventType, type MatrixClient, MatrixError, MatrixEvent, Room, RoomMember } from "matrix-js-sdk/src/matrix"; import { KnownMembership } from "matrix-js-sdk/src/types"; +import * as TestUtilsMatrix from "test-utils"; -import { MatrixClientPeg } from "../../../src/MatrixClientPeg"; -import Modal, { type ComponentType, type ComponentProps } from "../../../src/Modal"; -import SettingsStore from "../../../src/settings/SettingsStore"; -import MultiInviter, { type CompletionStates } from "../../../src/utils/MultiInviter"; -import * as TestUtilsMatrix from "../../test-utils"; -import AskInviteAnywayDialog from "../../../src/components/views/dialogs/AskInviteAnywayDialog"; -import ConfirmUserActionDialog from "../../../src/components/views/dialogs/ConfirmUserActionDialog"; +import { MatrixClientPeg } from "../MatrixClientPeg"; +import Modal, { type ComponentType, type ComponentProps } from "../Modal"; +import SettingsStore from "../settings/SettingsStore"; +import MultiInviter, { type CompletionStates } from "./MultiInviter"; +import AskInviteAnywayDialog from "../components/views/dialogs/AskInviteAnywayDialog"; +import ConfirmUserActionDialog from "../components/views/dialogs/ConfirmUserActionDialog"; const ROOMID = "!room:server"; @@ -34,19 +36,23 @@ const MXID_PROFILE_STATES: Record {}> = { }, }; -jest.mock("../../../src/Modal", () => ({ - createDialog: jest.fn(), +vi.mock("../Modal", () => ({ + default: { + createDialog: vi.fn(), + }, })); -jest.mock("../../../src/settings/SettingsStore", () => ({ - getValue: jest.fn(), - monitorSetting: jest.fn(), - watchSetting: jest.fn(), +vi.mock("../settings/SettingsStore", () => ({ + default: { + getValue: vi.fn(), + monitorSetting: vi.fn(), + watchSetting: vi.fn(), + }, })); const mockPromptBeforeInviteUnknownUsers = (value: boolean) => { - mocked(SettingsStore.getValue).mockImplementation( - (settingName: string, roomId: string, _excludeDefault = false): any => { + vi.mocked(SettingsStore.getValue).mockImplementation( + (settingName: string, roomId?: string | null, _excludeDefault = false): any => { if (settingName === "promptBeforeInviteUnknownUsers" && roomId === ROOMID) { return value; } @@ -55,12 +61,14 @@ const mockPromptBeforeInviteUnknownUsers = (value: boolean) => { }; const mockCreateTrackedDialog = (callbackName: "onInviteAnyways" | "onGiveUp") => { - mocked(Modal.createDialog).mockImplementation((Element: ComponentType, props?: ComponentProps) => { - if (Element === AskInviteAnywayDialog) { - (props as ComponentProps)[callbackName](); - } - return { close: jest.fn(), finished: new Promise(() => {}) }; - }); + vi.mocked(Modal.createDialog).mockImplementation( + (Element: ComponentType, props?: ComponentProps) => { + if (Element === AskInviteAnywayDialog) { + (props as ComponentProps)[callbackName](); + } + return { close: vi.fn(), finished: new Promise(() => {}) }; + }, + ); }; const expectAllInvitedResult = (result: CompletionStates) => { @@ -72,34 +80,34 @@ const expectAllInvitedResult = (result: CompletionStates) => { }; describe("MultiInviter", () => { - let client: jest.Mocked; + let client: Mocked; let inviter: MultiInviter; beforeEach(() => { - jest.resetAllMocks(); - mocked(Modal.createDialog).mockReturnValue({ close: jest.fn(), finished: new Promise(() => {}) }); + vi.resetAllMocks(); + vi.mocked(Modal.createDialog).mockReturnValue({ close: vi.fn(), finished: new Promise(() => {}) }); TestUtilsMatrix.stubClient(); - client = MatrixClientPeg.safeGet() as jest.Mocked; + client = vi.mocked(MatrixClientPeg.safeGet()); - client.invite = jest.fn(); + client.invite = vi.fn(); client.invite.mockResolvedValue({}); - client.getProfileInfo = jest.fn(); + client.getProfileInfo = vi.fn(); client.getProfileInfo.mockImplementation(async (userId: string) => { const m = MXID_PROFILE_STATES[userId]; if (m) return m(); throw new Error(); }); - client.unban = jest.fn(); + client.unban = vi.fn(); inviter = new MultiInviter(client, ROOMID); }); describe("invite", () => { it("should show a progress dialog while the invite happens", async () => { - const mockModalHandle = { close: jest.fn(), finished: new Promise<[]>(() => {}) }; - mocked(Modal.createDialog).mockReturnValue(mockModalHandle); + const mockModalHandle = { close: vi.fn(), finished: new Promise<[]>(() => {}) }; + vi.mocked(Modal.createDialog).mockReturnValue(mockModalHandle); const invitePromise = Promise.withResolvers<{}>(); client.invite.mockReturnValue(invitePromise.promise); @@ -163,7 +171,7 @@ describe("MultiInviter", () => { }); it("should show sensible error when attempting 3pid invite with no identity server", async () => { - client.inviteByEmail = jest.fn().mockRejectedValueOnce( + client.inviteByEmail = vi.fn().mockRejectedValueOnce( new MatrixError({ errcode: "ORG.MATRIX.JSSDK_MISSING_PARAM", }), @@ -175,7 +183,7 @@ describe("MultiInviter", () => { }); it("should ask if user wants to unban user if they have permission", async () => { - mocked(Modal.createDialog).mockImplementation( + vi.mocked(Modal.createDialog).mockImplementation( (Element: ComponentType, props?: ComponentProps): any => { // We stub out the modal with an immediate affirmative (proceed) return return { finished: Promise.resolve([true]) }; @@ -183,7 +191,7 @@ describe("MultiInviter", () => { ); const room = new Room(ROOMID, client, client.getSafeUserId()); - mocked(client.getRoom).mockReturnValue(room); + vi.mocked(client.getRoom).mockReturnValue(room); const ourMember = new RoomMember(ROOMID, client.getSafeUserId()); ourMember.membership = KnownMembership.Join; ourMember.powerLevel = 100; @@ -206,7 +214,7 @@ describe("MultiInviter", () => { }); it("should show sensible error when attempting to invite over federation with m.federate=false", async () => { - mocked(client.invite).mockRejectedValueOnce( + vi.mocked(client.invite).mockRejectedValueOnce( new MatrixError({ errcode: "M_FORBIDDEN", }), @@ -222,7 +230,7 @@ describe("MultiInviter", () => { room_id: ROOMID, }), ]); - mocked(client.getRoom).mockReturnValue(room); + vi.mocked(client.getRoom).mockReturnValue(room); await inviter.invite(["@user:other_server"]); expect(inviter.getErrorText("@user:other_server")).toMatchInlineSnapshot( @@ -231,7 +239,7 @@ describe("MultiInviter", () => { }); it("should show sensible error when attempting to invite over federation with m.federate=false to space", async () => { - mocked(client.invite).mockRejectedValueOnce( + vi.mocked(client.invite).mockRejectedValueOnce( new MatrixError({ errcode: "M_FORBIDDEN", }), @@ -248,7 +256,7 @@ describe("MultiInviter", () => { room_id: ROOMID, }), ]); - mocked(client.getRoom).mockReturnValue(room); + vi.mocked(client.getRoom).mockReturnValue(room); await inviter.invite(["@user:other_server"]); expect(inviter.getErrorText("@user:other_server")).toMatchInlineSnapshot( diff --git a/apps/web/test/unit-tests/utils/SearchInput-test.ts b/apps/web/src/utils/SearchInput.test.ts similarity index 74% rename from apps/web/test/unit-tests/utils/SearchInput-test.ts rename to apps/web/src/utils/SearchInput.test.ts index 79786601a5..ec8c2f17d2 100644 --- a/apps/web/test/unit-tests/utils/SearchInput-test.ts +++ b/apps/web/src/utils/SearchInput.test.ts @@ -5,21 +5,22 @@ Copyright 2023 Boluwatife Omosowon SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial Please see LICENSE files in the repository root for full details. */ -import { mocked } from "jest-mock"; -import { parsePermalink } from "../../../src/utils/permalinks/Permalinks"; -import { transformSearchTerm } from "../../../src/utils/SearchInput"; +import { vi, describe, it, expect } from "vitest"; -jest.mock("../../../src/utils/permalinks/Permalinks"); -jest.mock("../../../src/stores/WidgetStore"); -jest.mock("../../../src/stores/widgets/WidgetLayoutStore"); +import { parsePermalink } from "./permalinks/Permalinks"; +import { transformSearchTerm } from "./SearchInput"; + +vi.mock("./permalinks/Permalinks"); +vi.mock("../stores/WidgetStore"); +vi.mock("../stores/widgets/WidgetLayoutStore"); describe("transforming search term", () => { it("should return the primaryEntityId if the search term was a permalink", () => { const roomLink = "https://matrix.to/#/#element-dev:matrix.org"; const parsedPermalink = "#element-dev:matrix.org"; - mocked(parsePermalink).mockReturnValue({ + vi.mocked(parsePermalink).mockReturnValue({ primaryEntityId: parsedPermalink, roomIdOrAlias: parsedPermalink, eventId: "", @@ -33,7 +34,7 @@ describe("transforming search term", () => { it("should return the original search term if the search term is a permalink and the primaryEntityId is null", () => { const searchTerm = "https://matrix.to/#/#random-link:matrix.org"; - mocked(parsePermalink).mockReturnValue({ + vi.mocked(parsePermalink).mockReturnValue({ primaryEntityId: null, roomIdOrAlias: null, eventId: null, @@ -46,7 +47,7 @@ describe("transforming search term", () => { it("should return the original search term if the search term was not a permalink", () => { const searchTerm = "search term"; - mocked(parsePermalink).mockReturnValue(null); + vi.mocked(parsePermalink).mockReturnValue(null); expect(transformSearchTerm(searchTerm)).toBe(searchTerm); }); }); diff --git a/apps/web/test/unit-tests/utils/ShieldUtils-test.ts b/apps/web/src/utils/ShieldUtils.test.ts similarity index 94% rename from apps/web/test/unit-tests/utils/ShieldUtils-test.ts rename to apps/web/src/utils/ShieldUtils.test.ts index 43f978cd4a..de70905238 100644 --- a/apps/web/test/unit-tests/utils/ShieldUtils-test.ts +++ b/apps/web/src/utils/ShieldUtils.test.ts @@ -6,11 +6,12 @@ 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 { vi, describe, it, expect, beforeAll, afterAll } from "vitest"; import { type MatrixClient, type Room } from "matrix-js-sdk/src/matrix"; import { UserVerificationStatus } from "matrix-js-sdk/src/crypto-api"; -import { shieldStatusForRoom } from "../../../src/utils/ShieldUtils"; -import DMRoomMap from "../../../src/utils/DMRoomMap"; +import { shieldStatusForRoom } from "./ShieldUtils"; +import DMRoomMap from "./DMRoomMap"; function mkClient(selfTrust = false) { return { @@ -30,13 +31,13 @@ function mkClient(selfTrust = false) { } describe("mkClient self-test", function () { - test.each([true, false])("behaves well for self-trust=%s", async (v) => { + it.each([true, false])("behaves well for self-trust=%s", async (v) => { const client = mkClient(v); const status = await client.getCrypto()!.getDeviceVerificationStatus("@self:localhost", "DEVICE"); expect(status?.isVerified()).toBe(v); }); - test.each([ + it.each([ ["@TT:h", true], ["@TF:h", true], ["@FT:h", false], @@ -46,7 +47,7 @@ describe("mkClient self-test", function () { expect(status!.isCrossSigningVerified()).toBe(trust); }); - test.each([ + it.each([ ["@TT:h", true], ["@TF:h", false], ["@FT:h", true], @@ -62,11 +63,11 @@ describe("shieldStatusForMembership self-trust behaviour", function () { const mockInstance = { getUserIdForRoomId: (roomId: string) => (roomId === "DM" ? "@any:h" : null), } as unknown as DMRoomMap; - jest.spyOn(DMRoomMap, "shared").mockReturnValue(mockInstance); + vi.spyOn(DMRoomMap, "shared").mockReturnValue(mockInstance); }); afterAll(() => { - jest.spyOn(DMRoomMap, "shared").mockRestore(); + vi.spyOn(DMRoomMap, "shared").mockRestore(); }); it.each([ @@ -165,7 +166,7 @@ describe("shieldStatusForMembership other-trust behaviour", function () { const mockInstance = { getUserIdForRoomId: (roomId: string) => (roomId === "DM" ? "@any:h" : null), } as unknown as DMRoomMap; - jest.spyOn(DMRoomMap, "shared").mockReturnValue(mockInstance); + vi.spyOn(DMRoomMap, "shared").mockReturnValue(mockInstance); }); it.each([ diff --git a/apps/web/test/unit-tests/utils/WidgetUtils-test.ts b/apps/web/src/utils/WidgetUtils.test.ts similarity index 90% rename from apps/web/test/unit-tests/utils/WidgetUtils-test.ts rename to apps/web/src/utils/WidgetUtils.test.ts index b9c7e95f9d..00b2e3accd 100644 --- a/apps/web/test/unit-tests/utils/WidgetUtils-test.ts +++ b/apps/web/src/utils/WidgetUtils.test.ts @@ -7,8 +7,10 @@ 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 WidgetUtils from "../../../src/utils/WidgetUtils"; -import { mockPlatformPeg } from "../../test-utils"; +import { describe, it, expect, beforeEach } from "vitest"; +import { mockPlatformPeg } from "test-utils/platform"; + +import WidgetUtils from "./WidgetUtils"; describe("getLocalJitsiWrapperUrl", () => { beforeEach(() => { diff --git a/apps/web/test/unit-tests/utils/__snapshots__/AutoDiscoveryUtils-test.tsx.snap b/apps/web/src/utils/__snapshots__/AutoDiscoveryUtils.test.ts.snap similarity index 73% rename from apps/web/test/unit-tests/utils/__snapshots__/AutoDiscoveryUtils-test.tsx.snap rename to apps/web/src/utils/__snapshots__/AutoDiscoveryUtils.test.ts.snap index 8556db8ba8..a455808d44 100644 --- a/apps/web/test/unit-tests/utils/__snapshots__/AutoDiscoveryUtils-test.tsx.snap +++ b/apps/web/src/utils/__snapshots__/AutoDiscoveryUtils.test.ts.snap @@ -1,6 +1,6 @@ -// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`AutoDiscoveryUtils authComponentStateForError should return expected error for the registration page 1`] = ` +exports[`AutoDiscoveryUtils > authComponentStateForError > should return expected error for the registration page 1`] = ` { "serverDeadError":
diff --git a/apps/web/test/unit-tests/utils/membership-test.ts b/apps/web/src/utils/membership.test.ts similarity index 84% rename from apps/web/test/unit-tests/utils/membership-test.ts rename to apps/web/src/utils/membership.test.ts index e0ade64186..61a640ade7 100644 --- a/apps/web/test/unit-tests/utils/membership-test.ts +++ b/apps/web/src/utils/membership.test.ts @@ -6,6 +6,9 @@ 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. */ +// @vitest-environment happy-dom + +import { vi, describe, it, expect, beforeEach, afterEach, type Mocked } from "vitest"; import { type MatrixClient, type MatrixEvent, @@ -15,18 +18,17 @@ import { RoomStateEvent, } from "matrix-js-sdk/src/matrix"; import { KnownMembership } from "matrix-js-sdk/src/types"; -import { mocked } from "jest-mock"; +import { createTestClient, mkRoomMember, stubClient } from "test-utils"; -import { isKnockDenied, waitForMember } from "../../../src/utils/membership"; -import { createTestClient, mkRoomMember, stubClient } from "../../test-utils"; +import { isKnockDenied, waitForMember } from "./membership"; describe("isKnockDenied", () => { const userId = "alice"; - let client: jest.Mocked; + let client: Mocked; let room: Room; beforeEach(() => { - client = stubClient() as jest.Mocked; + client = vi.mocked(stubClient()); room = new Room("!room-id:example.com", client, "@user:example.com"); }); @@ -34,7 +36,7 @@ describe("isKnockDenied", () => { const roomMember = mkRoomMember(room.roomId, userId, KnownMembership.Leave, true, { membership: KnownMembership.Knock, }); - jest.spyOn(room, "getMember").mockReturnValue(roomMember); + vi.spyOn(room, "getMember").mockReturnValue(roomMember); expect(isKnockDenied(room)).toBe(true); }); @@ -45,7 +47,7 @@ describe("isKnockDenied", () => { { membership: KnownMembership.Leave, isKicked: true, prevMembership: KnownMembership.Join }, ])("checks that the user knock has been not denied", ({ membership, isKicked, prevMembership }) => { const roomMember = mkRoomMember(room.roomId, userId, membership, isKicked, { membership: prevMembership }); - jest.spyOn(room, "getMember").mockReturnValue(roomMember); + vi.spyOn(room, "getMember").mockReturnValue(roomMember); expect(isKnockDenied(room)).toBe(false); }); }); @@ -64,17 +66,17 @@ describe("waitForMember", () => { // getRoom() only knows about !stub_room, which has only one member const stubRoom = { - getMember: jest.fn().mockImplementation((userId) => { + getMember: vi.fn().mockImplementation((userId) => { return userId === STUB_MEMBER_ID ? ({} as RoomMember) : null; }), }; - mocked(client.getRoom).mockImplementation((roomId) => { + vi.mocked(client.getRoom).mockImplementation((roomId) => { return roomId === STUB_ROOM_ID ? (stubRoom as unknown as Room) : null; }); }); afterEach(() => { - jest.useRealTimers(); + vi.useRealTimers(); }); it("resolves with false if the timeout is reached", async () => { @@ -83,11 +85,11 @@ describe("waitForMember", () => { }); it("resolves with false if the timeout is reached, even if other RoomState.newMember events fire", async () => { - jest.useFakeTimers(); + vi.useFakeTimers(); const roomId = "!roomId:domain"; const userId = "@clientId:domain"; const resultProm = waitForMember(client, roomId, userId, { timeout }); - jest.advanceTimersByTime(50); + vi.advanceTimersByTime(50); expect(await resultProm).toBe(false); client.emit( RoomStateEvent.NewMember, @@ -98,7 +100,7 @@ describe("waitForMember", () => { userId: "@anotherClient:domain", } as RoomMember, ); - jest.useRealTimers(); + vi.useRealTimers(); }); it("resolves with true if RoomState.newMember fires", async () => { @@ -115,7 +117,7 @@ describe("waitForMember", () => { }); it("resolves immediately if the user is already a member", async () => { - jest.useFakeTimers(); + vi.useFakeTimers(); const resultProm = waitForMember(client, STUB_ROOM_ID, STUB_MEMBER_ID, { timeout }); expect(await resultProm).toBe(true); }); diff --git a/scripts/find-unused-settings.ts b/scripts/find-unused-settings.ts index b2379f85a1..16296b7d2e 100644 --- a/scripts/find-unused-settings.ts +++ b/scripts/find-unused-settings.ts @@ -36,7 +36,7 @@ const SETTINGS_FILE_RELATIVE = path.relative(ROOT, SETTINGS_FILE); // search terms, but keep this list as a manual escape hatch for cases the heuristic can't // see (e.g. usage mediated through a helper function rather than a literal reference). const KNOWN_USED_OVERRIDES = new Set([ - // e.g. "someSettingName", + "test_setting", // only used in tests ]); interface DeclaredSetting {