From e235100dd06fc17d2f46364b3510d0202b2a1f91 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 13 May 2025 10:27:08 +0100 Subject: [PATCH] Fix flaky jest tests (#29927) * Debug flaky jest test Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Iterate Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Discard changes to jest.config.ts --------- Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/Lifecycle.ts | 4 +++- src/utils/StorageManager.ts | 3 +-- test/unit-tests/components/structures/MatrixChat-test.tsx | 8 ++++---- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Lifecycle.ts b/src/Lifecycle.ts index 6f78c01357..5641f936ae 100644 --- a/src/Lifecycle.ts +++ b/src/Lifecycle.ts @@ -1115,7 +1115,9 @@ export async function onLoggedOut(): Promise { * @param {object} opts Options for how to clear storage. * @returns {Promise} promise which resolves once the stores have been cleared */ -async function clearStorage(opts?: { deleteEverything?: boolean }): Promise { +export async function clearStorage(opts?: { deleteEverything?: boolean }): Promise { + logger.info(`Clearing storage, deleteEverything=${opts?.deleteEverything}`); + if (window.localStorage) { // get the currently defined device language, if set, so we can restore it later const language = SettingsStore.getValueAt(SettingLevel.DEVICE, "language", null, true, true); diff --git a/src/utils/StorageManager.ts b/src/utils/StorageManager.ts index 3b63f0a636..478d30af6f 100644 --- a/src/utils/StorageManager.ts +++ b/src/utils/StorageManager.ts @@ -92,8 +92,7 @@ export async function checkConsistency(): Promise<{ if (dataInLocalStorage && cryptoInited && !dataInCryptoStore) { healthy = false; error( - "Data exists in local storage and crypto is marked as initialised " + - " but no data found in crypto store. " + + "Data exists in local storage and crypto is marked as initialised but no data found in crypto store. " + "IndexedDB storage has likely been evicted by the browser!", ); } diff --git a/test/unit-tests/components/structures/MatrixChat-test.tsx b/test/unit-tests/components/structures/MatrixChat-test.tsx index 4e95f651b9..5c10e6b465 100644 --- a/test/unit-tests/components/structures/MatrixChat-test.tsx +++ b/test/unit-tests/components/structures/MatrixChat-test.tsx @@ -68,6 +68,7 @@ import AutoDiscoveryUtils from "../../../../src/utils/AutoDiscoveryUtils"; import { type ValidatedServerConfig } from "../../../../src/utils/ValidatedServerConfig"; import Modal from "../../../../src/Modal.tsx"; import { SetupEncryptionStore } from "../../../../src/stores/SetupEncryptionStore.ts"; +import { clearStorage } from "../../../../src/Lifecycle"; jest.mock("matrix-js-sdk/src/oidc/authorize", () => ({ completeAuthorizationCodeGrant: jest.fn(), @@ -217,6 +218,9 @@ describe("", () => { }; beforeEach(async () => { + await clearStorage(); + Lifecycle.setSessionLockNotStolen(); + localStorage.clear(); jest.restoreAllMocks(); defaultProps = { @@ -344,10 +348,6 @@ describe("", () => { }, }); - jest.spyOn(logger, "error").mockClear(); - }); - - beforeEach(() => { loginClient = getMockClientWithEventEmitter(getMockClientMethods()); // this is used to create a temporary client during login jest.spyOn(MatrixJs, "createClient").mockReturnValue(loginClient);