From 23cc1aff73795c90d0807af7f331d02f1743121d Mon Sep 17 00:00:00 2001 From: Faye Duxovni Date: Wed, 1 Jun 2022 04:57:53 -0400 Subject: [PATCH] Fix flakiness of cypress crypto tests (#8731) --- cypress/integration/7-crypto/crypto.spec.ts | 24 ++++++++++++--------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/cypress/integration/7-crypto/crypto.spec.ts b/cypress/integration/7-crypto/crypto.spec.ts index 2446e3bd2b..6f1f7aa6c8 100644 --- a/cypress/integration/7-crypto/crypto.spec.ts +++ b/cypress/integration/7-crypto/crypto.spec.ts @@ -19,13 +19,17 @@ limitations under the License. import type { MatrixClient } from "matrix-js-sdk/src/matrix"; import { SynapseInstance } from "../../plugins/synapsedocker"; -function waitForEncryption(cli: MatrixClient, roomId: string, win: Cypress.AUTWindow, resolve: () => void) { - cli.crypto.cryptoStore.getEndToEndRooms(null, (result) => { - if (result[roomId]) { - resolve(); - } else { - cli.once(win.matrixcs.RoomStateEvent.Update, () => waitForEncryption(cli, roomId, win, resolve)); - } +function waitForEncryption(cli: MatrixClient, roomId: string, win: Cypress.AUTWindow): Promise { + return new Promise(resolve => { + const onEvent = () => { + cli.crypto.cryptoStore.getEndToEndRooms(null, (result) => { + if (result[roomId]) { + cli.off(win.matrixcs.ClientEvent.Event, onEvent); + resolve(); + } + }); + }; + cli.on(win.matrixcs.ClientEvent.Event, onEvent); }); } @@ -61,15 +65,15 @@ describe("Cryptography", () => { cy.window(), ]).then(([bot, roomId, win]) => { cy.inviteUser(roomId, bot.getUserId()); - cy.visit("/#/room/" + roomId); cy.wrap( - new Promise(resolve => - waitForEncryption(bot, roomId, win, resolve), + waitForEncryption( + bot, roomId, win, ).then(() => bot.sendMessage(roomId, { body: "Top secret message", msgtype: "m.text", })), ); + cy.visit("/#/room/" + roomId); }); cy.get(".mx_RoomView_body .mx_cryptoEvent").should("contain", "Encryption enabled");