From df4a1f0c85a151b368ff9b32f3aa02e5a7c0aee7 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Fri, 3 Jul 2026 09:50:11 +0100 Subject: [PATCH] Migrate batch of tests to vitest (#34108) * Migrate utils/dm tests to vitest * Migrate utils/room tests to vitest * Migrate utils/location tests to vitest * Fix types * Satisfy tsc & knip --- .../utils/dm/createDmLocalRoom.test.ts} | 32 ++++++------ .../utils/dm/filterValidMDirect.test.ts} | 4 +- .../utils/dm/findDMForUser.test.ts} | 38 +++++++------- .../utils/dm/findDMRoom.test.ts} | 36 +++++++------ .../utils/location/isSelfLocation.test.ts} | 3 +- .../utils/location/links.test.ts} | 9 ++-- .../location/locationEventGeoUri.test.ts} | 6 ++- .../utils/location/parseGeoUri.test.ts} | 4 +- .../location/positionFailureMessage.test.ts} | 4 +- .../utils/room/canInviteTo.test.ts} | 42 ++++++++------- .../getJoinedNonFunctionalMembers.test.ts} | 28 +++++----- .../room/getRoomFunctionalMembers.test.ts} | 9 ++-- .../utils/room/getSectionTagForRoom.test.ts} | 15 +++--- .../utils/room/getTagsForRoom.test.ts} | 52 ++++++++++--------- .../utils/room/inviteToRoom.test.ts} | 21 ++++---- ...cryptRoomWithSingle3rdPartyInvite.test.ts} | 24 +++++---- .../utils/room/sortRoomsByRecency.test.ts} | 13 +++-- .../utils/room/tagRoom.test.ts} | 29 ++++++----- .../room-list-v3/skip-list/getMockedRooms.ts | 3 +- apps/web/tsconfig.json | 2 + apps/web/vitest.config.ts | 1 + 21 files changed, 209 insertions(+), 166 deletions(-) rename apps/web/{test/unit-tests/utils/dm/createDmLocalRoom-test.ts => src/utils/dm/createDmLocalRoom.test.ts} (79%) rename apps/web/{test/unit-tests/utils/dm/filterValidMDirect-test.ts => src/utils/dm/filterValidMDirect.test.ts} (94%) rename apps/web/{test/unit-tests/utils/dm/findDMForUser-test.ts => src/utils/dm/findDMForUser.test.ts} (84%) rename apps/web/{test/unit-tests/utils/dm/findDMRoom-test.ts => src/utils/dm/findDMRoom.test.ts} (57%) rename apps/web/{test/unit-tests/utils/location/isSelfLocation-test.ts => src/utils/location/isSelfLocation.test.ts} (95%) rename apps/web/{test/unit-tests/utils/location/map-test.ts => src/utils/location/links.test.ts} (87%) rename apps/web/{test/unit-tests/utils/location/locationEventGeoUri-test.ts => src/utils/location/locationEventGeoUri.test.ts} (77%) rename apps/web/{test/unit-tests/utils/location/parseGeoUri-test.ts => src/utils/location/parseGeoUri.test.ts} (97%) rename apps/web/{test/unit-tests/utils/location/positionFailureMessage-test.ts => src/utils/location/positionFailureMessage.test.ts} (90%) rename apps/web/{test/unit-tests/utils/room/canInviteTo-test.ts => src/utils/room/canInviteTo.test.ts} (64%) rename apps/web/{test/unit-tests/utils/room/getJoinedNonFunctionalMembers-test.ts => src/utils/room/getJoinedNonFunctionalMembers.test.ts} (66%) rename apps/web/{test/unit-tests/utils/room/getRoomFunctionalMembers-test.ts => src/utils/room/getRoomFunctionalMembers.test.ts} (89%) rename apps/web/{test/unit-tests/utils/room/getSectionTagForRoom-test.ts => src/utils/room/getSectionTagForRoom.test.ts} (78%) rename apps/web/{test/unit-tests/utils/room/getTagsForRoom-test.ts => src/utils/room/getTagsForRoom.test.ts} (69%) rename apps/web/{test/unit-tests/utils/room/inviteToRoom-test.ts => src/utils/room/inviteToRoom.test.ts} (70%) rename apps/web/{test/unit-tests/utils/room/shouldEncryptRoomWithSingle3rdPartyInvite-test.ts => src/utils/room/shouldEncryptRoomWithSingle3rdPartyInvite.test.ts} (81%) rename apps/web/{test/unit-tests/utils/room/sortRoomsByRecency-test.ts => src/utils/room/sortRoomsByRecency.test.ts} (82%) rename apps/web/{test/unit-tests/utils/room/tagRoom-test.ts => src/utils/room/tagRoom.test.ts} (87%) diff --git a/apps/web/test/unit-tests/utils/dm/createDmLocalRoom-test.ts b/apps/web/src/utils/dm/createDmLocalRoom.test.ts similarity index 79% rename from apps/web/test/unit-tests/utils/dm/createDmLocalRoom-test.ts rename to apps/web/src/utils/dm/createDmLocalRoom.test.ts index bb23b2f2d5..41cc50c648 100644 --- a/apps/web/test/unit-tests/utils/dm/createDmLocalRoom-test.ts +++ b/apps/web/src/utils/dm/createDmLocalRoom.test.ts @@ -6,23 +6,25 @@ 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 } from "vitest"; import { EventType, KNOWN_SAFE_ROOM_VERSION, type MatrixClient } from "matrix-js-sdk/src/matrix"; import { KnownMembership } from "matrix-js-sdk/src/types"; +import { createTestClient } from "test-utils"; -import { canEncryptToAllUsers } from "../../../../src/createRoom"; -import { type LocalRoom, LOCAL_ROOM_ID_PREFIX } from "../../../../src/models/LocalRoom"; -import { DirectoryMember, type Member, ThreepidMember } from "../../../../src/utils/direct-messages"; -import { createDmLocalRoom } from "../../../../src/utils/dm/createDmLocalRoom"; -import { privateShouldBeEncrypted } from "../../../../src/utils/rooms"; -import { createTestClient } from "../../../test-utils"; +import { canEncryptToAllUsers } from "../../createRoom"; +import { type LocalRoom, LOCAL_ROOM_ID_PREFIX } from "../../models/LocalRoom"; +import { DirectoryMember, type Member, ThreepidMember } from "../direct-messages"; +import { createDmLocalRoom } from "./createDmLocalRoom"; +import { privateShouldBeEncrypted } from "../rooms"; -jest.mock("../../../../src/utils/rooms", () => ({ - privateShouldBeEncrypted: jest.fn(), +vi.mock("../rooms", () => ({ + privateShouldBeEncrypted: vi.fn(), })); -jest.mock("../../../../src/createRoom", () => ({ - canEncryptToAllUsers: jest.fn(), +vi.mock("../../createRoom", () => ({ + canEncryptToAllUsers: vi.fn(), })); function assertLocalRoom(room: LocalRoom, targets: Member[], encrypted: boolean) { @@ -60,7 +62,7 @@ describe("createDmLocalRoom", () => { describe("when rooms should be encrypted", () => { beforeEach(() => { - mocked(privateShouldBeEncrypted).mockReturnValue(true); + vi.mocked(privateShouldBeEncrypted).mockReturnValue(true); }); it("should create an encrytped room for 3PID targets", async () => { @@ -71,7 +73,7 @@ describe("createDmLocalRoom", () => { describe("for MXID targets with encryption available", () => { beforeEach(() => { - mocked(canEncryptToAllUsers).mockResolvedValue(true); + vi.mocked(canEncryptToAllUsers).mockResolvedValue(true); }); it("should create an encrypted room", async () => { @@ -83,7 +85,7 @@ describe("createDmLocalRoom", () => { describe("for MXID targets with encryption unavailable", () => { beforeEach(() => { - mocked(canEncryptToAllUsers).mockResolvedValue(false); + vi.mocked(canEncryptToAllUsers).mockResolvedValue(false); }); it("should create an unencrypted room", async () => { @@ -96,7 +98,7 @@ describe("createDmLocalRoom", () => { describe("if rooms should not be encrypted", () => { beforeEach(() => { - mocked(privateShouldBeEncrypted).mockReturnValue(false); + vi.mocked(privateShouldBeEncrypted).mockReturnValue(false); }); it("should create an unencrypted room", async () => { diff --git a/apps/web/test/unit-tests/utils/dm/filterValidMDirect-test.ts b/apps/web/src/utils/dm/filterValidMDirect.test.ts similarity index 94% rename from apps/web/test/unit-tests/utils/dm/filterValidMDirect-test.ts rename to apps/web/src/utils/dm/filterValidMDirect.test.ts index ee21ed338a..cfe79f7827 100644 --- a/apps/web/test/unit-tests/utils/dm/filterValidMDirect-test.ts +++ b/apps/web/src/utils/dm/filterValidMDirect.test.ts @@ -6,7 +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 { filterValidMDirect } from "../../../../src/utils/dm/filterValidMDirect"; +import { describe, it, expect } from "vitest"; + +import { filterValidMDirect } from "./filterValidMDirect"; const roomId1 = "!room1:example.com"; const roomId2 = "!room2:example.com"; diff --git a/apps/web/test/unit-tests/utils/dm/findDMForUser-test.ts b/apps/web/src/utils/dm/findDMForUser.test.ts similarity index 84% rename from apps/web/test/unit-tests/utils/dm/findDMForUser-test.ts rename to apps/web/src/utils/dm/findDMForUser.test.ts index c5bd6eef38..bbf7ba9b08 100644 --- a/apps/web/test/unit-tests/utils/dm/findDMForUser-test.ts +++ b/apps/web/src/utils/dm/findDMForUser.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 { mocked } from "jest-mock"; +// @vitest-environment happy-dom + +import { vi, describe, it, expect, beforeEach } from "vitest"; import { type MatrixClient, Room } from "matrix-js-sdk/src/matrix"; import { KnownMembership } from "matrix-js-sdk/src/types"; +import { createTestClient, makeMembershipEvent, mkThirdPartyInviteEvent } from "test-utils"; -import DMRoomMap from "../../../../src/utils/DMRoomMap"; -import { createTestClient, makeMembershipEvent, mkThirdPartyInviteEvent } from "../../../test-utils"; -import { LocalRoom } from "../../../../src/models/LocalRoom"; -import { findDMForUser } from "../../../../src/utils/dm/findDMForUser"; -import { getFunctionalMembers } from "../../../../src/utils/room/getFunctionalMembers"; +import DMRoomMap from "../DMRoomMap"; +import { LocalRoom } from "../../models/LocalRoom"; +import { findDMForUser } from "./findDMForUser"; +import { getFunctionalMembers } from "../room/getFunctionalMembers"; -jest.mock("../../../../src/utils/room/getFunctionalMembers", () => ({ - getFunctionalMembers: jest.fn(), +vi.mock("../room/getFunctionalMembers", () => ({ + getFunctionalMembers: vi.fn(), })); describe("findDMForUser", () => { @@ -41,7 +43,7 @@ describe("findDMForUser", () => { mockClient = createTestClient(); // always return the bot user as functional member - mocked(getFunctionalMembers).mockReturnValue([botId]); + vi.mocked(getFunctionalMembers).mockReturnValue([botId]); room1 = new Room("!room1:example.com", mockClient, userId1); room1.getMyMembership = () => KnownMembership.Join; @@ -92,7 +94,7 @@ describe("findDMForUser", () => { mkThirdPartyInviteEvent(thirdPartyId, "third-party", room7.roomId), ]); - mocked(mockClient.getRoom).mockImplementation((roomId: string) => { + vi.mocked(mockClient.getRoom).mockImplementation((roomId?: string) => { return ( { [room1.roomId]: room1, @@ -102,14 +104,14 @@ describe("findDMForUser", () => { [room5.roomId]: room5, [room6.roomId]: room6, [room7.roomId]: room7, - }[roomId] || null + }[roomId!] || null ); }); dmRoomMap = { - getDMRoomForIdentifiers: jest.fn(), - getDMRoomsForUserId: jest.fn(), - getRoomIds: jest.fn().mockReturnValue( + getDMRoomForIdentifiers: vi.fn(), + getDMRoomsForUserId: vi.fn(), + getRoomIds: vi.fn().mockReturnValue( new Set([ room1.roomId, room2.roomId, @@ -122,8 +124,8 @@ describe("findDMForUser", () => { ]), ), } as unknown as DMRoomMap; - jest.spyOn(DMRoomMap, "shared").mockReturnValue(dmRoomMap); - mocked(dmRoomMap.getDMRoomsForUserId).mockImplementation((userId: string) => { + vi.spyOn(DMRoomMap, "shared").mockReturnValue(dmRoomMap); + vi.mocked(dmRoomMap.getDMRoomsForUserId).mockImplementation((userId: string) => { if (userId === userId1) { return [room1.roomId, room2.roomId, room3.roomId, room4.roomId, room5.roomId, unknownRoomId]; } @@ -138,8 +140,8 @@ describe("findDMForUser", () => { describe("for an empty DM room list", () => { beforeEach(() => { - mocked(dmRoomMap.getDMRoomsForUserId).mockReturnValue([]); - mocked(dmRoomMap.getRoomIds).mockReturnValue(new Set()); + vi.mocked(dmRoomMap.getDMRoomsForUserId).mockReturnValue([]); + vi.mocked(dmRoomMap.getRoomIds).mockReturnValue(new Set()); }); it("should return undefined", () => { diff --git a/apps/web/test/unit-tests/utils/dm/findDMRoom-test.ts b/apps/web/src/utils/dm/findDMRoom.test.ts similarity index 57% rename from apps/web/test/unit-tests/utils/dm/findDMRoom-test.ts rename to apps/web/src/utils/dm/findDMRoom.test.ts index 170ed774bf..fc557c999f 100644 --- a/apps/web/test/unit-tests/utils/dm/findDMRoom-test.ts +++ b/apps/web/src/utils/dm/findDMRoom.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 { mocked } from "jest-mock"; +// @vitest-environment happy-dom + +import { vi, describe, it, expect, beforeEach } from "vitest"; import { type MatrixClient, Room } from "matrix-js-sdk/src/matrix"; +import { createTestClient } from "test-utils"; -import { MatrixClientPeg } from "../../../../src/MatrixClientPeg"; -import { DirectoryMember, ThreepidMember } from "../../../../src/utils/direct-messages"; -import { findDMForUser } from "../../../../src/utils/dm/findDMForUser"; -import { findDMRoom } from "../../../../src/utils/dm/findDMRoom"; -import DMRoomMap from "../../../../src/utils/DMRoomMap"; -import { createTestClient } from "../../../test-utils"; +import { MatrixClientPeg } from "../../MatrixClientPeg"; +import { DirectoryMember, ThreepidMember } from "../direct-messages"; +import { findDMForUser } from "./findDMForUser"; +import { findDMRoom } from "./findDMRoom"; +import DMRoomMap from "../DMRoomMap"; -jest.mock("../../../../src/utils/dm/findDMForUser", () => ({ - findDMForUser: jest.fn(), +vi.mock("../dm/findDMForUser", () => ({ + findDMForUser: vi.fn(), })); describe("findDMRoom", () => { @@ -30,33 +32,33 @@ describe("findDMRoom", () => { beforeEach(() => { mockClient = createTestClient(); - jest.spyOn(MatrixClientPeg, "get").mockReturnValue(mockClient); + vi.spyOn(MatrixClientPeg, "get").mockReturnValue(mockClient); room1 = new Room("!room1:example.com", mockClient, userId1); dmRoomMap = { - getDMRoomForIdentifiers: jest.fn(), - getDMRoomsForUserId: jest.fn(), + getDMRoomForIdentifiers: vi.fn(), + getDMRoomsForUserId: vi.fn(), } as unknown as DMRoomMap; - jest.spyOn(DMRoomMap, "shared").mockReturnValue(dmRoomMap); + vi.spyOn(DMRoomMap, "shared").mockReturnValue(dmRoomMap); }); it("should return the room for a single target with a room", () => { - mocked(findDMForUser).mockReturnValue(room1); + vi.mocked(findDMForUser).mockReturnValue(room1); expect(findDMRoom(mockClient, [member1])).toBe(room1); }); it("should return undefined for a single target without a room", () => { - mocked(findDMForUser).mockReturnValue(undefined); + vi.mocked(findDMForUser).mockReturnValue(undefined); expect(findDMRoom(mockClient, [member1])).toBeNull(); }); it("should return the room for 2 targets with a room", () => { - mocked(dmRoomMap.getDMRoomForIdentifiers).mockReturnValue(room1); + vi.mocked(dmRoomMap.getDMRoomForIdentifiers).mockReturnValue(room1); expect(findDMRoom(mockClient, [member1, member2])).toBe(room1); }); it("should return null for 2 targets without a room", () => { - mocked(dmRoomMap.getDMRoomForIdentifiers).mockReturnValue(null); + vi.mocked(dmRoomMap.getDMRoomForIdentifiers).mockReturnValue(null); expect(findDMRoom(mockClient, [member1, member2])).toBeNull(); }); }); diff --git a/apps/web/test/unit-tests/utils/location/isSelfLocation-test.ts b/apps/web/src/utils/location/isSelfLocation.test.ts similarity index 95% rename from apps/web/test/unit-tests/utils/location/isSelfLocation-test.ts rename to apps/web/src/utils/location/isSelfLocation.test.ts index 927fa2ed62..02559daf6c 100644 --- a/apps/web/test/unit-tests/utils/location/isSelfLocation-test.ts +++ b/apps/web/src/utils/location/isSelfLocation.test.ts @@ -6,6 +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 { describe, it, expect } from "vitest"; import { M_TEXT, type ILocationContent, @@ -16,7 +17,7 @@ import { ContentHelpers, } from "matrix-js-sdk/src/matrix"; -import { isSelfLocation } from "../../../../src/utils/location"; +import { isSelfLocation } from "./isSelfLocation"; describe("isSelfLocation", () => { it("Returns true for a full m.asset event", () => { diff --git a/apps/web/test/unit-tests/utils/location/map-test.ts b/apps/web/src/utils/location/links.test.ts similarity index 87% rename from apps/web/test/unit-tests/utils/location/map-test.ts rename to apps/web/src/utils/location/links.test.ts index c2dcf75118..50024704eb 100644 --- a/apps/web/test/unit-tests/utils/location/map-test.ts +++ b/apps/web/src/utils/location/links.test.ts @@ -6,9 +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 { createMapSiteLinkFromEvent } from "../../../../src/utils/location"; -import { mkMessage } from "../../../test-utils"; -import { makeLegacyLocationEvent, makeLocationEvent } from "../../../test-utils/location"; +// @vitest-environment happy-dom + +import { describe, it, expect } from "vitest"; +import { mkMessage, makeLegacyLocationEvent, makeLocationEvent } from "test-utils"; + +import { createMapSiteLinkFromEvent } from "./links"; describe("createMapSiteLinkFromEvent", () => { it("returns null if event does not contain geouri", () => { diff --git a/apps/web/test/unit-tests/utils/location/locationEventGeoUri-test.ts b/apps/web/src/utils/location/locationEventGeoUri.test.ts similarity index 77% rename from apps/web/test/unit-tests/utils/location/locationEventGeoUri-test.ts rename to apps/web/src/utils/location/locationEventGeoUri.test.ts index c1c77f2844..f606499934 100644 --- a/apps/web/test/unit-tests/utils/location/locationEventGeoUri-test.ts +++ b/apps/web/src/utils/location/locationEventGeoUri.test.ts @@ -6,8 +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 { locationEventGeoUri } from "../../../../src/utils/location"; -import { makeLegacyLocationEvent, makeLocationEvent } from "../../../test-utils/location"; +import { describe, it, expect } from "vitest"; +import { makeLegacyLocationEvent, makeLocationEvent } from "test-utils/location"; + +import { locationEventGeoUri } from "./locationEventGeoUri"; describe("locationEventGeoUri()", () => { it("returns m.location uri when available", () => { diff --git a/apps/web/test/unit-tests/utils/location/parseGeoUri-test.ts b/apps/web/src/utils/location/parseGeoUri.test.ts similarity index 97% rename from apps/web/test/unit-tests/utils/location/parseGeoUri-test.ts rename to apps/web/src/utils/location/parseGeoUri.test.ts index 608ed52ba0..27bc56a4b2 100644 --- a/apps/web/test/unit-tests/utils/location/parseGeoUri-test.ts +++ b/apps/web/src/utils/location/parseGeoUri.test.ts @@ -6,7 +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 { parseGeoUri } from "../../../../src/utils/location/parseGeoUri"; +import { describe, it, expect } from "vitest"; + +import { parseGeoUri } from "./parseGeoUri"; describe("parseGeoUri", () => { it("fails if the supplied URI is empty", () => { diff --git a/apps/web/test/unit-tests/utils/location/positionFailureMessage-test.ts b/apps/web/src/utils/location/positionFailureMessage.test.ts similarity index 90% rename from apps/web/test/unit-tests/utils/location/positionFailureMessage-test.ts rename to apps/web/src/utils/location/positionFailureMessage.test.ts index f3eeacb83e..99baa49cdb 100644 --- a/apps/web/test/unit-tests/utils/location/positionFailureMessage-test.ts +++ b/apps/web/src/utils/location/positionFailureMessage.test.ts @@ -6,7 +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 { positionFailureMessage } from "../../../../src/utils/location/positionFailureMessage"; +import { describe, it, expect } from "vitest"; + +import { positionFailureMessage } from "./positionFailureMessage"; describe("positionFailureMessage()", () => { // error codes from GeolocationPositionError diff --git a/apps/web/test/unit-tests/utils/room/canInviteTo-test.ts b/apps/web/src/utils/room/canInviteTo.test.ts similarity index 64% rename from apps/web/test/unit-tests/utils/room/canInviteTo-test.ts rename to apps/web/src/utils/room/canInviteTo.test.ts index 2340c667dd..b16fc52ac4 100644 --- a/apps/web/test/unit-tests/utils/room/canInviteTo-test.ts +++ b/apps/web/src/utils/room/canInviteTo.test.ts @@ -6,22 +6,24 @@ 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, afterEach } from "vitest"; import { JoinRule, Room } from "matrix-js-sdk/src/matrix"; import { KnownMembership } from "matrix-js-sdk/src/types"; +import { getMockClientWithEventEmitter, mockClientMethodsUser } from "test-utils"; -import { shouldShowComponent } from "../../../../src/customisations/helpers/UIComponents"; -import { UIComponent } from "../../../../src/settings/UIFeature"; -import { canInviteTo } from "../../../../src/utils/room/canInviteTo"; -import { getMockClientWithEventEmitter, mockClientMethodsUser } from "../../../test-utils"; +import { shouldShowComponent } from "../../customisations/helpers/UIComponents"; +import { UIComponent } from "../../settings/UIFeature"; +import { canInviteTo } from "./canInviteTo"; -jest.mock("../../../../src/customisations/helpers/UIComponents", () => ({ - shouldShowComponent: jest.fn(), +vi.mock("../../customisations/helpers/UIComponents", () => ({ + shouldShowComponent: vi.fn(), })); describe("canInviteTo()", () => { afterEach(() => { - jest.restoreAllMocks(); + vi.restoreAllMocks(); }); const userId = "@alice:server.org"; @@ -32,14 +34,14 @@ describe("canInviteTo()", () => { ...mockClientMethodsUser(userId), }); const room = new Room(roomId, client, userId); - jest.spyOn(room, "getMyMembership").mockReturnValue(KnownMembership.Join); - jest.spyOn(room, "getJoinRule").mockReturnValue(JoinRule.Public); - jest.spyOn(room, "canInvite").mockReturnValue(true); + vi.spyOn(room, "getMyMembership").mockReturnValue(KnownMembership.Join); + vi.spyOn(room, "getJoinRule").mockReturnValue(JoinRule.Public); + vi.spyOn(room, "canInvite").mockReturnValue(true); return room; }; beforeEach(() => { - mocked(shouldShowComponent).mockReturnValue(true); + vi.mocked(shouldShowComponent).mockReturnValue(true); }); describe("when user has permissions to issue an invite for this room", () => { @@ -47,14 +49,14 @@ describe("canInviteTo()", () => { it("should return false when current user membership is not joined", () => { const room = makeRoom(); - jest.spyOn(room, "getMyMembership").mockReturnValue(KnownMembership.Invite); + vi.spyOn(room, "getMyMembership").mockReturnValue(KnownMembership.Invite); expect(canInviteTo(room)).toEqual(false); }); it("should return false when UIComponent.InviteUsers customisation hides invite", () => { const room = makeRoom(); - mocked(shouldShowComponent).mockReturnValue(false); + vi.mocked(shouldShowComponent).mockReturnValue(false); expect(canInviteTo(room)).toEqual(false); expect(shouldShowComponent).toHaveBeenCalledWith(UIComponent.InviteUsers); @@ -72,16 +74,16 @@ describe("canInviteTo()", () => { it("should return false when room is a private space", () => { const room = makeRoom(); - jest.spyOn(room, "getJoinRule").mockReturnValue(JoinRule.Invite); - jest.spyOn(room, "isSpaceRoom").mockReturnValue(true); - jest.spyOn(room, "canInvite").mockReturnValue(false); + vi.spyOn(room, "getJoinRule").mockReturnValue(JoinRule.Invite); + vi.spyOn(room, "isSpaceRoom").mockReturnValue(true); + vi.spyOn(room, "canInvite").mockReturnValue(false); expect(canInviteTo(room)).toEqual(false); }); it("should return false when room is just a room", () => { const room = makeRoom(); - jest.spyOn(room, "canInvite").mockReturnValue(false); + vi.spyOn(room, "canInvite").mockReturnValue(false); expect(canInviteTo(room)).toEqual(false); }); @@ -89,8 +91,8 @@ describe("canInviteTo()", () => { it("should return true when room is a public space", () => { const room = makeRoom(); // default join rule is public - jest.spyOn(room, "isSpaceRoom").mockReturnValue(true); - jest.spyOn(room, "canInvite").mockReturnValue(false); + vi.spyOn(room, "isSpaceRoom").mockReturnValue(true); + vi.spyOn(room, "canInvite").mockReturnValue(false); expect(canInviteTo(room)).toEqual(true); }); diff --git a/apps/web/test/unit-tests/utils/room/getJoinedNonFunctionalMembers-test.ts b/apps/web/src/utils/room/getJoinedNonFunctionalMembers.test.ts similarity index 66% rename from apps/web/test/unit-tests/utils/room/getJoinedNonFunctionalMembers-test.ts rename to apps/web/src/utils/room/getJoinedNonFunctionalMembers.test.ts index 1c397e143e..475e0ad590 100644 --- a/apps/web/test/unit-tests/utils/room/getJoinedNonFunctionalMembers-test.ts +++ b/apps/web/src/utils/room/getJoinedNonFunctionalMembers.test.ts @@ -6,14 +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 { mocked } from "jest-mock"; +import { vi, describe, it, expect, beforeEach } from "vitest"; import { type MatrixClient, Room, RoomMember } from "matrix-js-sdk/src/matrix"; -import { getFunctionalMembers } from "../../../../src/utils/room/getFunctionalMembers"; -import { getJoinedNonFunctionalMembers } from "../../../../src/utils/room/getJoinedNonFunctionalMembers"; +import { getFunctionalMembers } from "./getFunctionalMembers"; +import { getJoinedNonFunctionalMembers } from "./getJoinedNonFunctionalMembers"; -jest.mock("../../../../src/utils/room/getFunctionalMembers", () => ({ - getFunctionalMembers: jest.fn(), +vi.mock("./getFunctionalMembers", () => ({ + getFunctionalMembers: vi.fn(), })); describe("getJoinedNonFunctionalMembers", () => { @@ -23,7 +23,7 @@ describe("getJoinedNonFunctionalMembers", () => { beforeEach(() => { room = new Room("!room:example.com", {} as unknown as MatrixClient, "@user:example.com"); - room.getJoinedMembers = jest.fn(); + room.getJoinedMembers = vi.fn(); roomMember1 = new RoomMember(room.roomId, "@user1:example.com"); roomMember2 = new RoomMember(room.roomId, "@user2:example.com"); @@ -31,8 +31,8 @@ describe("getJoinedNonFunctionalMembers", () => { describe("if there are no members", () => { beforeEach(() => { - mocked(room.getJoinedMembers).mockReturnValue([]); - mocked(getFunctionalMembers).mockReturnValue([]); + vi.mocked(room.getJoinedMembers).mockReturnValue([]); + vi.mocked(getFunctionalMembers).mockReturnValue([]); }); it("should return an empty list", () => { @@ -42,8 +42,8 @@ describe("getJoinedNonFunctionalMembers", () => { describe("if there are only regular room members", () => { beforeEach(() => { - mocked(room.getJoinedMembers).mockReturnValue([roomMember1, roomMember2]); - mocked(getFunctionalMembers).mockReturnValue([]); + vi.mocked(room.getJoinedMembers).mockReturnValue([roomMember1, roomMember2]); + vi.mocked(getFunctionalMembers).mockReturnValue([]); }); it("should return the room members", () => { @@ -55,8 +55,8 @@ describe("getJoinedNonFunctionalMembers", () => { describe("if there are only functional room members", () => { beforeEach(() => { - mocked(room.getJoinedMembers).mockReturnValue([]); - mocked(getFunctionalMembers).mockReturnValue(["@functional:example.com"]); + vi.mocked(room.getJoinedMembers).mockReturnValue([]); + vi.mocked(getFunctionalMembers).mockReturnValue(["@functional:example.com"]); }); it("should return an empty list", () => { @@ -66,8 +66,8 @@ describe("getJoinedNonFunctionalMembers", () => { describe("if there are some functional room members", () => { beforeEach(() => { - mocked(room.getJoinedMembers).mockReturnValue([roomMember1, roomMember2]); - mocked(getFunctionalMembers).mockReturnValue([roomMember1.userId]); + vi.mocked(room.getJoinedMembers).mockReturnValue([roomMember1, roomMember2]); + vi.mocked(getFunctionalMembers).mockReturnValue([roomMember1.userId]); }); it("should only return the non-functional members", () => { diff --git a/apps/web/test/unit-tests/utils/room/getRoomFunctionalMembers-test.ts b/apps/web/src/utils/room/getRoomFunctionalMembers.test.ts similarity index 89% rename from apps/web/test/unit-tests/utils/room/getRoomFunctionalMembers-test.ts rename to apps/web/src/utils/room/getRoomFunctionalMembers.test.ts index 4f910f9283..1344323cee 100644 --- a/apps/web/test/unit-tests/utils/room/getRoomFunctionalMembers-test.ts +++ b/apps/web/src/utils/room/getRoomFunctionalMembers.test.ts @@ -6,10 +6,13 @@ 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 { Room, UNSTABLE_ELEMENT_FUNCTIONAL_USERS } from "matrix-js-sdk/src/matrix"; +// @vitest-environment happy-dom -import { getFunctionalMembers } from "../../../../src/utils/room/getFunctionalMembers"; -import { createTestClient, mkEvent } from "../../../test-utils"; +import { describe, it, expect } from "vitest"; +import { Room, UNSTABLE_ELEMENT_FUNCTIONAL_USERS } from "matrix-js-sdk/src/matrix"; +import { createTestClient, mkEvent } from "test-utils"; + +import { getFunctionalMembers } from "./getFunctionalMembers"; describe("getRoomFunctionalMembers", () => { const client = createTestClient(); diff --git a/apps/web/test/unit-tests/utils/room/getSectionTagForRoom-test.ts b/apps/web/src/utils/room/getSectionTagForRoom.test.ts similarity index 78% rename from apps/web/test/unit-tests/utils/room/getSectionTagForRoom-test.ts rename to apps/web/src/utils/room/getSectionTagForRoom.test.ts index 5c37a8f111..11158c5588 100644 --- a/apps/web/test/unit-tests/utils/room/getSectionTagForRoom-test.ts +++ b/apps/web/src/utils/room/getSectionTagForRoom.test.ts @@ -5,16 +5,19 @@ * Please see LICENSE files in the repository root for full details. */ +// @vitest-environment happy-dom + +import { vi, describe, it, expect } from "vitest"; import { type Room } from "matrix-js-sdk/src/matrix"; -import { DefaultTagID } from "../../../../src/stores/room-list-v3/skip-list/tag"; -import { CUSTOM_SECTION_TAG_PREFIX } from "../../../../src/stores/room-list-v3/section"; -import { getSectionTagForRoom } from "../../../../src/utils/room/getSectionTagForRoom"; -import { getTagsForRoom } from "../../../../src/utils/room/getTagsForRoom"; +import { DefaultTagID } from "../../stores/room-list-v3/skip-list/tag"; +import { CUSTOM_SECTION_TAG_PREFIX } from "../../stores/room-list-v3/section"; +import { getSectionTagForRoom } from "./getSectionTagForRoom"; +import { getTagsForRoom } from "./getTagsForRoom"; -jest.mock("../../../../src/utils/room/getTagsForRoom"); +vi.mock("./getTagsForRoom"); -const mockGetTagsForRoom = jest.mocked(getTagsForRoom); +const mockGetTagsForRoom = vi.mocked(getTagsForRoom); describe("getSectionTagForRoom", () => { const room = {} as Room; diff --git a/apps/web/test/unit-tests/utils/room/getTagsForRoom-test.ts b/apps/web/src/utils/room/getTagsForRoom.test.ts similarity index 69% rename from apps/web/test/unit-tests/utils/room/getTagsForRoom-test.ts rename to apps/web/src/utils/room/getTagsForRoom.test.ts index bec448834a..f17e573ee1 100644 --- a/apps/web/test/unit-tests/utils/room/getTagsForRoom-test.ts +++ b/apps/web/src/utils/room/getTagsForRoom.test.ts @@ -5,14 +5,16 @@ * Please see LICENSE files in the repository root for full details. */ +// @vitest-environment happy-dom + +import { vi, describe, it, expect, beforeEach } from "vitest"; import { JoinRule, type MatrixClient, type Room } from "matrix-js-sdk/src/matrix"; import { KnownMembership } from "matrix-js-sdk/src/types"; -import { mocked } from "jest-mock"; +import { createTestClient, mkRoom } from "test-utils"; -import { createTestClient, mkRoom } from "../../../test-utils"; -import { DefaultTagID } from "../../../../src/stores/room-list-v3/skip-list/tag"; -import { getTagsForRoom } from "../../../../src/utils/room/getTagsForRoom"; -import DMRoomMap from "../../../../src/utils/DMRoomMap"; +import { DefaultTagID } from "../../stores/room-list-v3/skip-list/tag"; +import { getTagsForRoom } from "./getTagsForRoom"; +import DMRoomMap from "../DMRoomMap"; describe("getTagsForRoom", () => { let client: MatrixClient; @@ -23,21 +25,21 @@ describe("getTagsForRoom", () => { rooms = []; const dmRoomMap = { - getUserIdForRoomId: jest.fn().mockReturnValue(undefined), + getUserIdForRoomId: vi.fn().mockReturnValue(undefined), } as unknown as DMRoomMap; DMRoomMap.setShared(dmRoomMap); }); function makeRoom(roomId: string): Room { mkRoom(client, roomId, rooms); - mocked(client).getRoom.mockImplementation((id) => rooms.find((r) => r.roomId === id) ?? null); - mocked(client).getRooms.mockImplementation(() => rooms); + vi.mocked(client).getRoom.mockImplementation((id) => rooms.find((r) => r.roomId === id) ?? null); + vi.mocked(client).getRooms.mockImplementation(() => rooms); return client.getRoom(roomId)!; } it("should return [Invite] for a room where the user is invited", () => { const room = makeRoom("!invited:server"); - (room.getMyMembership as jest.Mock).mockReturnValue(KnownMembership.Invite); + vi.mocked(room.getMyMembership).mockReturnValue(KnownMembership.Invite); const tags = getTagsForRoom(room); expect(tags).toEqual([DefaultTagID.Invite]); @@ -47,7 +49,7 @@ describe("getTagsForRoom", () => { "should return [Archived] for a room where the user has %s", (membership) => { const room = makeRoom(`!${membership.toLowerCase()}:server`); - (room.getMyMembership as jest.Mock).mockReturnValue(membership); + vi.mocked(room.getMyMembership).mockReturnValue(membership); const tags = getTagsForRoom(room); expect(tags).toEqual([DefaultTagID.Archived]); @@ -58,7 +60,7 @@ describe("getTagsForRoom", () => { describe("with no user-defined tags and not a DM", () => { it("should return [Untagged] when the room has no tags and is not a DM", () => { const room = makeRoom("!plain:server"); - (room.getMyMembership as jest.Mock).mockReturnValue(KnownMembership.Join); + vi.mocked(room.getMyMembership).mockReturnValue(KnownMembership.Join); (room as any).tags = {}; const tags = getTagsForRoom(room); @@ -68,10 +70,10 @@ describe("getTagsForRoom", () => { it("should return [DM] when the room is a DM", () => { const room = makeRoom("!dm:server"); - (room.getMyMembership as jest.Mock).mockReturnValue(KnownMembership.Join); + vi.mocked(room.getMyMembership).mockReturnValue(KnownMembership.Join); (room as any).tags = {}; - mocked(DMRoomMap.shared().getUserIdForRoomId as jest.Mock).mockReturnValue("@alice:server"); + vi.mocked(DMRoomMap.shared().getUserIdForRoomId).mockReturnValue("@alice:server"); const tags = getTagsForRoom(room); expect(tags).toContain(DefaultTagID.DM); @@ -81,7 +83,7 @@ describe("getTagsForRoom", () => { describe("rooms with user-defined tags", () => { it("should return the user-defined tags", () => { const room = makeRoom("!tagged:server"); - (room.getMyMembership as jest.Mock).mockReturnValue(KnownMembership.Join); + vi.mocked(room.getMyMembership).mockReturnValue(KnownMembership.Join); (room as any).tags = { "m.favourite": {}, "u.alice": {} }; const tags = getTagsForRoom(room); @@ -92,11 +94,11 @@ describe("getTagsForRoom", () => { it("should not check DM status when user-defined tags are already present", () => { const room = makeRoom("!tagged-dm:server"); - (room.getMyMembership as jest.Mock).mockReturnValue(KnownMembership.Join); + vi.mocked(room.getMyMembership).mockReturnValue(KnownMembership.Join); (room as any).tags = { "m.lowpriority": {} }; // Even if the room is a DM, user-defined tags take priority - mocked(DMRoomMap.shared().getUserIdForRoomId as jest.Mock).mockReturnValue("@alice:server"); + vi.mocked(DMRoomMap.shared().getUserIdForRoomId).mockReturnValue("@alice:server"); const tags = getTagsForRoom(room); expect(tags).toContain("m.lowpriority"); @@ -110,9 +112,9 @@ describe("getTagsForRoom", () => { "should include Conference tag for a call room with %s join rule", (joinRule) => { const room = makeRoom(`!call:${joinRule}:server`); - (room.getMyMembership as jest.Mock).mockReturnValue(KnownMembership.Join); - (room.isCallRoom as jest.Mock).mockReturnValue(true); - (room.getJoinRule as jest.Mock).mockReturnValue(joinRule); + vi.mocked(room.getMyMembership).mockReturnValue(KnownMembership.Join); + vi.mocked(room.isCallRoom).mockReturnValue(true); + vi.mocked(room.getJoinRule).mockReturnValue(joinRule); const tags = getTagsForRoom(room); expect(tags).toContain(DefaultTagID.Conference); @@ -123,9 +125,9 @@ describe("getTagsForRoom", () => { "should not include Conference tag for a call room with %s join rule", (joinRule) => { const room = makeRoom(`!call:${joinRule}:server`); - (room.getMyMembership as jest.Mock).mockReturnValue(KnownMembership.Join); - (room.isCallRoom as jest.Mock).mockReturnValue(true); - (room.getJoinRule as jest.Mock).mockReturnValue(joinRule); + vi.mocked(room.getMyMembership).mockReturnValue(KnownMembership.Join); + vi.mocked(room.isCallRoom).mockReturnValue(true); + vi.mocked(room.getJoinRule).mockReturnValue(joinRule); const tags = getTagsForRoom(room); expect(tags).not.toContain(DefaultTagID.Conference); @@ -134,10 +136,10 @@ describe("getTagsForRoom", () => { it("should include Conference alongside Untagged for a public call room with no other tags", () => { const room = makeRoom("!callPublicPlain:server"); - (room.getMyMembership as jest.Mock).mockReturnValue(KnownMembership.Join); + vi.mocked(room.getMyMembership).mockReturnValue(KnownMembership.Join); (room as any).tags = {}; - (room.isCallRoom as jest.Mock).mockReturnValue(true); - (room.getJoinRule as jest.Mock).mockReturnValue(JoinRule.Public); + vi.mocked(room.isCallRoom).mockReturnValue(true); + vi.mocked(room.getJoinRule).mockReturnValue(JoinRule.Public); const tags = getTagsForRoom(room); // Conference is added to the tag list before the Untagged fallback check, diff --git a/apps/web/test/unit-tests/utils/room/inviteToRoom-test.ts b/apps/web/src/utils/room/inviteToRoom.test.ts similarity index 70% rename from apps/web/test/unit-tests/utils/room/inviteToRoom-test.ts rename to apps/web/src/utils/room/inviteToRoom.test.ts index bee74e2d1b..50cf09e826 100644 --- a/apps/web/test/unit-tests/utils/room/inviteToRoom-test.ts +++ b/apps/web/src/utils/room/inviteToRoom.test.ts @@ -6,11 +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 { Room } from "matrix-js-sdk/src/matrix"; +// @vitest-environment happy-dom -import defaultDispatcher from "../../../../src/dispatcher/dispatcher"; -import { inviteToRoom } from "../../../../src/utils/room/inviteToRoom"; -import { getMockClientWithEventEmitter } from "../../../test-utils"; +import { vi, describe, it, expect, beforeEach, afterEach } from "vitest"; +import { Room } from "matrix-js-sdk/src/matrix"; +import { getMockClientWithEventEmitter } from "test-utils"; + +import defaultDispatcher from "../../dispatcher/dispatcher"; +import { inviteToRoom } from "./inviteToRoom"; describe("inviteToRoom()", () => { const userId = "@alice:server.org"; @@ -18,7 +21,7 @@ describe("inviteToRoom()", () => { const makeRoom = (): Room => { const client = getMockClientWithEventEmitter({ - isGuest: jest.fn(), + isGuest: vi.fn(), }); const room = new Room(roomId, client, userId); return room; @@ -26,17 +29,17 @@ describe("inviteToRoom()", () => { beforeEach(() => { // stub - jest.spyOn(defaultDispatcher, "dispatch").mockImplementation(() => {}); + vi.spyOn(defaultDispatcher, "dispatch").mockImplementation(() => {}); }); afterEach(() => { - jest.restoreAllMocks(); + vi.restoreAllMocks(); }); it("requires registration when a guest tries to invite to a room", () => { const room = makeRoom(); - jest.spyOn(room.client, "isGuest").mockReturnValue(true); + vi.spyOn(room.client, "isGuest").mockReturnValue(true); inviteToRoom(room); @@ -47,7 +50,7 @@ describe("inviteToRoom()", () => { it("opens the room inviter", () => { const room = makeRoom(); - jest.spyOn(room.client, "isGuest").mockReturnValue(false); + vi.spyOn(room.client, "isGuest").mockReturnValue(false); inviteToRoom(room); diff --git a/apps/web/test/unit-tests/utils/room/shouldEncryptRoomWithSingle3rdPartyInvite-test.ts b/apps/web/src/utils/room/shouldEncryptRoomWithSingle3rdPartyInvite.test.ts similarity index 81% rename from apps/web/test/unit-tests/utils/room/shouldEncryptRoomWithSingle3rdPartyInvite-test.ts rename to apps/web/src/utils/room/shouldEncryptRoomWithSingle3rdPartyInvite.test.ts index 8e3de54896..80ee961a2a 100644 --- a/apps/web/test/unit-tests/utils/room/shouldEncryptRoomWithSingle3rdPartyInvite-test.ts +++ b/apps/web/src/utils/room/shouldEncryptRoomWithSingle3rdPartyInvite.test.ts @@ -6,16 +6,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 { mocked } from "jest-mock"; +// @vitest-environment happy-dom + +import { vi, describe, it, expect, beforeAll, beforeEach } from "vitest"; import { type MatrixClient, type MatrixEvent, Room } from "matrix-js-sdk/src/matrix"; +import { mkRoomMemberJoinEvent, mkThirdPartyInviteEvent, stubClient } from "test-utils"; -import DMRoomMap from "../../../../src/utils/DMRoomMap"; -import { shouldEncryptRoomWithSingle3rdPartyInvite } from "../../../../src/utils/room/shouldEncryptRoomWithSingle3rdPartyInvite"; -import { privateShouldBeEncrypted } from "../../../../src/utils/rooms"; -import { mkRoomMemberJoinEvent, mkThirdPartyInviteEvent, stubClient } from "../../../test-utils"; +import DMRoomMap from "../DMRoomMap"; +import { shouldEncryptRoomWithSingle3rdPartyInvite } from "./shouldEncryptRoomWithSingle3rdPartyInvite"; +import { privateShouldBeEncrypted } from "../rooms"; -jest.mock("../../../../src/utils/rooms", () => ({ - privateShouldBeEncrypted: jest.fn(), +vi.mock("../rooms", () => ({ + privateShouldBeEncrypted: vi.fn(), })); describe("shouldEncryptRoomWithSingle3rdPartyInvite", () => { @@ -40,12 +42,12 @@ describe("shouldEncryptRoomWithSingle3rdPartyInvite", () => { mkRoomMemberJoinEvent(client.getSafeUserId(), roomWithOneThirdPartyInvite.roomId), thirdPartyInviteEvent, ]); - jest.spyOn(DMRoomMap.shared(), "getRoomIds").mockReturnValue(new Set([roomWithOneThirdPartyInvite.roomId])); + vi.spyOn(DMRoomMap.shared(), "getRoomIds").mockReturnValue(new Set([roomWithOneThirdPartyInvite.roomId])); }); describe("when well-known promotes encryption", () => { beforeEach(() => { - mocked(privateShouldBeEncrypted).mockReturnValue(true); + vi.mocked(privateShouldBeEncrypted).mockReturnValue(true); }); it("should return true + invite event for a DM room with one third-party invite", () => { @@ -56,7 +58,7 @@ describe("shouldEncryptRoomWithSingle3rdPartyInvite", () => { }); it("should return false for a non-DM room with one third-party invite", () => { - mocked(DMRoomMap.shared().getRoomIds).mockReturnValue(new Set()); + vi.mocked(DMRoomMap.shared().getRoomIds).mockReturnValue(new Set()); expect(shouldEncryptRoomWithSingle3rdPartyInvite(roomWithOneThirdPartyInvite)).toEqual({ shouldEncrypt: false, @@ -90,7 +92,7 @@ describe("shouldEncryptRoomWithSingle3rdPartyInvite", () => { describe("when well-known does not promote encryption", () => { beforeEach(() => { - mocked(privateShouldBeEncrypted).mockReturnValue(false); + vi.mocked(privateShouldBeEncrypted).mockReturnValue(false); }); it("should return false for a DM room with one third-party invite", () => { diff --git a/apps/web/test/unit-tests/utils/room/sortRoomsByRecency-test.ts b/apps/web/src/utils/room/sortRoomsByRecency.test.ts similarity index 82% rename from apps/web/test/unit-tests/utils/room/sortRoomsByRecency-test.ts rename to apps/web/src/utils/room/sortRoomsByRecency.test.ts index e9ae1fa62e..6480682cce 100644 --- a/apps/web/test/unit-tests/utils/room/sortRoomsByRecency-test.ts +++ b/apps/web/src/utils/room/sortRoomsByRecency.test.ts @@ -5,12 +5,15 @@ 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 Room } from "matrix-js-sdk/src/matrix"; +// @vitest-environment happy-dom -import { createTestClient } from "../../../test-utils"; -import { getMockedRooms } from "../../stores/room-list-v3/skip-list/getMockedRooms"; -import { DefaultTagID } from "../../../../src/stores/room-list-v3/skip-list/tag"; -import { compareRoomsByRecency, sortRoomsByRecency } from "../../../../src/utils/room/sortRoomsByRecency"; +import { describe, it, expect, beforeEach } from "vitest"; +import { type Room } from "matrix-js-sdk/src/matrix"; +import { createTestClient } from "test-utils"; + +import { getMockedRooms } from "../../../test/unit-tests/stores/room-list-v3/skip-list/getMockedRooms"; +import { DefaultTagID } from "../../stores/room-list-v3/skip-list/tag"; +import { compareRoomsByRecency, sortRoomsByRecency } from "./sortRoomsByRecency"; describe("sortRoomsByRecency", () => { let userId: string; diff --git a/apps/web/test/unit-tests/utils/room/tagRoom-test.ts b/apps/web/src/utils/room/tagRoom.test.ts similarity index 87% rename from apps/web/test/unit-tests/utils/room/tagRoom-test.ts rename to apps/web/src/utils/room/tagRoom.test.ts index 4b29c7252e..1dcf143d9c 100644 --- a/apps/web/test/unit-tests/utils/room/tagRoom-test.ts +++ b/apps/web/src/utils/room/tagRoom.test.ts @@ -6,15 +6,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 { Room } from "matrix-js-sdk/src/matrix"; +// @vitest-environment happy-dom -import RoomListActions from "../../../../src/actions/RoomListActions"; -import defaultDispatcher from "../../../../src/dispatcher/dispatcher"; -import { DefaultTagID, type TagID } from "../../../../src/stores/room-list-v3/skip-list/tag"; -import { CHATS_TAG, CUSTOM_SECTION_TAG_PREFIX } from "../../../../src/stores/room-list-v3/section"; -import { tagRoom } from "../../../../src/utils/room/tagRoom"; -import { getMockClientWithEventEmitter } from "../../../test-utils"; -import * as getSectionTagForRoomUtils from "../../../../src/utils/room/getSectionTagForRoom"; +import { vi, describe, it, expect, beforeEach, afterEach } from "vitest"; +import { Room } from "matrix-js-sdk/src/matrix"; +import { getMockClientWithEventEmitter } from "test-utils"; + +import RoomListActions from "../../actions/RoomListActions"; +import defaultDispatcher from "../../dispatcher/dispatcher"; +import { DefaultTagID, type TagID } from "../../stores/room-list-v3/skip-list/tag"; +import { CHATS_TAG, CUSTOM_SECTION_TAG_PREFIX } from "../../stores/room-list-v3/section"; +import { tagRoom } from "./tagRoom"; +import * as getSectionTagForRoomUtils from "./getSectionTagForRoom"; describe("tagRoom()", () => { const userId = "@alice:server.org"; @@ -23,23 +26,23 @@ describe("tagRoom()", () => { const makeRoom = (currentSectionTag: TagID | null = null): Room => { const client = getMockClientWithEventEmitter({ - isGuest: jest.fn(), + isGuest: vi.fn(), }); const room = new Room(roomId, client, userId); - jest.spyOn(getSectionTagForRoomUtils, "getSectionTagForRoom").mockReturnValue(currentSectionTag); + vi.spyOn(getSectionTagForRoomUtils, "getSectionTagForRoom").mockReturnValue(currentSectionTag); return room; }; beforeEach(() => { // stub - jest.spyOn(defaultDispatcher, "dispatch").mockImplementation(() => {}); - jest.spyOn(RoomListActions, "tagRoom").mockReturnValue({ action: "mocked_tag_room_action", fn: () => {} }); + vi.spyOn(defaultDispatcher, "dispatch").mockImplementation(() => {}); + vi.spyOn(RoomListActions, "tagRoom").mockReturnValue({ action: "mocked_tag_room_action", fn: () => {} }); }); afterEach(() => { - jest.restoreAllMocks(); + vi.restoreAllMocks(); }); it("does nothing when room tag is not allowed", () => { diff --git a/apps/web/test/unit-tests/stores/room-list-v3/skip-list/getMockedRooms.ts b/apps/web/test/unit-tests/stores/room-list-v3/skip-list/getMockedRooms.ts index 1aa0954ef9..af6858f023 100644 --- a/apps/web/test/unit-tests/stores/room-list-v3/skip-list/getMockedRooms.ts +++ b/apps/web/test/unit-tests/stores/room-list-v3/skip-list/getMockedRooms.ts @@ -7,6 +7,7 @@ Please see LICENSE files in the repository root for full details. import type { MatrixClient, Room } from "matrix-js-sdk/src/matrix"; import { mkMessage, mkStubRoom } from "../../../../test-utils"; +import { vi } from "../../../../setup/adapter.ts"; export function getMockedRooms(client: MatrixClient, roomCount: number = 100): Room[] { const rooms: Room[] = []; @@ -14,7 +15,7 @@ export function getMockedRooms(client: MatrixClient, roomCount: number = 100): R const roomId = `!foo${i}:matrix.org`; const room = mkStubRoom(roomId, `Foo Room ${i}`, client); const event = mkMessage({ room: roomId, user: `@foo${i}:matrix.org`, ts: i + 1, event: true }); - jest.spyOn(room.getLiveTimeline(), "getEvents").mockReturnValue([event]); + vi.spyOn(room.getLiveTimeline(), "getEvents").mockReturnValue([event]); rooms.push(room); } return rooms; diff --git a/apps/web/tsconfig.json b/apps/web/tsconfig.json index a567efa76d..6cb2422075 100644 --- a/apps/web/tsconfig.json +++ b/apps/web/tsconfig.json @@ -20,6 +20,8 @@ "paths": { "jest-matrix-react": ["./test/test-utils/jest-matrix-react"], "test-utils-rtl": ["./test/test-utils/jest-matrix-react"], + "test-utils": ["./test/test-utils"], + "test-utils/*": ["./test/test-utils/*"], "jest-mock-vitest-adapter": ["./test/setup/adapter.ts"] } }, diff --git a/apps/web/vitest.config.ts b/apps/web/vitest.config.ts index bd46a1a901..70cab9943f 100644 --- a/apps/web/vitest.config.ts +++ b/apps/web/vitest.config.ts @@ -12,6 +12,7 @@ export default defineProject({ resolve: { alias: [ { find: "test-utils-rtl", replacement: resolve(__dirname, "./test/test-utils/jest-matrix-react") }, + { find: "test-utils", replacement: resolve(__dirname, "./test/test-utils") }, // Stub out workers as they do not play well under test { find: /.*workers\/(.+)Factory/,