Skip to content

Commit b3fedf3

Browse files
authored
Prepare for jest->vitest (#5137)
* Skip unwritten tests Signed-off-by: Michael Telatynski <[email protected]> * Tidy jest fake timers Signed-off-by: Michael Telatynski <[email protected]> * Remove unnecessary sessionStorage mock Signed-off-by: Michael Telatynski <[email protected]> * Improve types Signed-off-by: Michael Telatynski <[email protected]> * Improve async assertions Signed-off-by: Michael Telatynski <[email protected]> * Improve error assertions Signed-off-by: Michael Telatynski <[email protected]> * Improve object assertions Signed-off-by: Michael Telatynski <[email protected]> * Remove assertion testing unclear mock This test failed when ran individually, same as after the clearAllMocks call Signed-off-by: Michael Telatynski <[email protected]> * Avoid awaiting non-thenables Signed-off-by: Michael Telatynski <[email protected]> * Pass nop function when stubbing out console, vitest won't accept it any other way Signed-off-by: Michael Telatynski <[email protected]> * Remove unnecessary mock which causes tests to fail after updating fetch-mock & fix typo Signed-off-by: Michael Telatynski <[email protected]> * Fix mistaken assertions not testing all values in array Signed-off-by: Michael Telatynski <[email protected]> * Fix hidden non-running tests in room.spec.ts Signed-off-by: Michael Telatynski <[email protected]> --------- Signed-off-by: Michael Telatynski <[email protected]>
1 parent 3d0ebdf commit b3fedf3

22 files changed

+96
-92
lines changed

spec/integ/crypto/cross-signing.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ describe("cross-signing", () => {
113113
);
114114

115115
afterEach(async () => {
116-
await aliceClient.stopClient();
116+
aliceClient.stopClient();
117117
fetchMock.mockReset();
118118
});
119119

spec/integ/crypto/crypto.spec.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ describe("crypto", () => {
276276
);
277277

278278
afterEach(async () => {
279-
await aliceClient.stopClient();
279+
aliceClient.stopClient();
280280

281281
// Allow in-flight things to complete before we tear down the test
282282
await jest.runAllTimersAsync();
@@ -724,7 +724,7 @@ describe("crypto", () => {
724724
syncResponder.sendOrQueueSyncResponse(syncResponse);
725725
await syncPromise(aliceClient);
726726

727-
await awaitDecryptionError;
727+
await expect(awaitDecryptionError).resolves.toBeUndefined();
728728
});
729729
});
730730

@@ -1448,7 +1448,7 @@ describe("crypto", () => {
14481448

14491449
await syncPromise(aliceClient);
14501450

1451-
// Verify that `/upload` is called on Alice's homesever
1451+
// Verify that `/upload` is called on Alice's homeserver
14521452
const { keysCount, fallbackKeysCount } = await uploadPromise;
14531453
expect(keysCount).toBeGreaterThan(0);
14541454
expect(fallbackKeysCount).toBe(0);
@@ -1718,7 +1718,6 @@ describe("crypto", () => {
17181718
* @param backupVersion - The version of the created backup
17191719
*/
17201720
async function bootstrapSecurity(backupVersion: string): Promise<void> {
1721-
mockSetupCrossSigningRequests();
17221721
mockSetupMegolmBackupRequests(backupVersion);
17231722

17241723
// promise which will resolve when a `KeyBackupStatus` event is emitted with `enabled: true`

spec/integ/crypto/megolm-backup.spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,7 @@ describe("megolm-keys backup", () => {
133133
});
134134

135135
afterEach(async () => {
136-
if (aliceClient !== undefined) {
137-
await aliceClient.stopClient();
138-
}
136+
aliceClient?.stopClient();
139137

140138
// Allow in-flight things to complete before we tear down the test
141139
await jest.runAllTimersAsync();

spec/integ/crypto/state-events.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ describe("Encrypted State Events", () => {
9696
}, 10000);
9797

9898
afterEach(async () => {
99-
await aliceClient.stopClient();
100-
await jest.runAllTimersAsync();
99+
aliceClient.stopClient();
101100
fetchMock.mockReset();
102101
});
103102

spec/integ/crypto/verification.spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,7 @@ describe("verification", () => {
141141
});
142142

143143
afterEach(async () => {
144-
if (aliceClient !== undefined) {
145-
await aliceClient.stopClient();
146-
}
144+
aliceClient?.stopClient();
147145

148146
// Allow in-flight things to complete before we tear down the test
149147
await jest.runAllTimersAsync();

spec/unit/autodiscovery.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ describe("AutoDiscovery", function () {
190190
};
191191
return Promise.all([
192192
httpBackend.flushAllExpected(),
193-
AutoDiscovery.findClientConfig("example.org").then(expect(expected).toEqual),
193+
AutoDiscovery.findClientConfig("example.org").then((config) => expect(config).toEqual(expected)),
194194
]);
195195
});
196196

spec/unit/event-timeline-set.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ describe("EventTimelineSet", () => {
228228
let thread: Thread;
229229

230230
beforeEach(() => {
231-
(client.supportsThreads as jest.Mock).mockReturnValue(true);
231+
mocked(client.supportsThreads).mockReturnValue(true);
232232
thread = new Thread("!thread_id:server", messageEvent, { room, client });
233233
});
234234

@@ -384,7 +384,7 @@ describe("EventTimelineSet", () => {
384384
let thread: Thread;
385385

386386
beforeEach(() => {
387-
(client.supportsThreads as jest.Mock).mockReturnValue(true);
387+
mocked(client.supportsThreads).mockReturnValue(true);
388388
thread = new Thread("!thread_id:server", messageEvent, { room, client });
389389
});
390390

spec/unit/interactive-auth.spec.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717

1818
import { type MatrixClient } from "../../src/client";
1919
import { logger } from "../../src/logger";
20-
import { InteractiveAuth, AuthType } from "../../src/interactive-auth";
20+
import { InteractiveAuth, AuthType, NoAuthFlowFoundError } from "../../src/interactive-auth";
2121
import { HTTPError, MatrixError } from "../../src/http-api";
2222
import { sleep } from "../../src/utils";
2323
import { secureRandomString } from "../../src/randomstring";
@@ -337,7 +337,9 @@ describe("InteractiveAuth", () => {
337337
throw err;
338338
});
339339

340-
await expect(ia.attemptAuth.bind(ia)).rejects.toThrow(new Error("No appropriate authentication flow found"));
340+
await expect(ia.attemptAuth.bind(ia)).rejects.toThrow(
341+
new NoAuthFlowFoundError("No appropriate authentication flow found", [], []),
342+
);
341343
});
342344

343345
it("should start an auth stage and reject if no auth flow but has session", async () => {
@@ -372,7 +374,9 @@ describe("InteractiveAuth", () => {
372374
throw err;
373375
});
374376

375-
await expect(ia.attemptAuth.bind(ia)).rejects.toThrow(new Error("No appropriate authentication flow found"));
377+
await expect(ia.attemptAuth.bind(ia)).rejects.toThrow(
378+
new NoAuthFlowFoundError("No appropriate authentication flow found", [], []),
379+
);
376380
});
377381

378382
it("should handle unexpected error types without data property set", async () => {
@@ -397,7 +401,7 @@ describe("InteractiveAuth", () => {
397401
throw err;
398402
});
399403

400-
await expect(ia.attemptAuth.bind(ia)).rejects.toThrow(new Error("myerror"));
404+
await expect(ia.attemptAuth.bind(ia)).rejects.toThrow(new HTTPError("myerror", 401));
401405
});
402406

403407
it("should allow dummy auth", async () => {

spec/unit/matrixrtc/CallMembership.spec.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import { type MatrixEvent } from "../../../src";
17+
import { mocked } from "jest-mock";
18+
19+
import { type IContent, type MatrixEvent } from "../../../src";
1820
import {
1921
CallMembership,
2022
type SessionMembershipData,
@@ -32,8 +34,8 @@ function makeMockEvent(originTs = 0): MatrixEvent {
3234
} as unknown as MatrixEvent;
3335
}
3436

35-
function createCallMembership(ev: MatrixEvent, content: unknown): CallMembership {
36-
(ev.getContent as jest.Mock).mockReturnValue(content);
37+
function createCallMembership(ev: MatrixEvent, content: IContent): CallMembership {
38+
mocked(ev.getContent).mockReturnValue(content);
3739
const data = CallMembership.membershipDataFromMatrixEvent(ev);
3840
return new CallMembership(ev, data, "xx");
3941
}
@@ -307,11 +309,11 @@ describe("CallMembership", () => {
307309
}).toThrow();
308310
});
309311

310-
it("considers memberships unexpired if local age low enough", () => {
312+
it.skip("considers memberships unexpired if local age low enough", () => {
311313
// TODO link prev event
312314
});
313315

314-
it("considers memberships expired if local age large enough", () => {
316+
it.skip("considers memberships expired if local age large enough", () => {
315317
// TODO link prev event
316318
});
317319

spec/unit/matrixrtc/MatrixRTCSession.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,6 +1345,13 @@ describe("MatrixRTCSession", () => {
13451345
});
13461346

13471347
describe("receiving", () => {
1348+
beforeAll(() => {
1349+
jest.useFakeTimers();
1350+
});
1351+
afterAll(() => {
1352+
jest.useRealTimers();
1353+
});
1354+
13481355
it("collects keys from encryption events", async () => {
13491356
const mockRoom = makeMockRoom([membershipTemplate]);
13501357
sess = MatrixRTCSession.sessionForSlot(client, mockRoom, callSession);

0 commit comments

Comments
 (0)