From 35ba4074def134ebf8d4590d66e753d4696c3576 Mon Sep 17 00:00:00 2001 From: Andy Balaam Date: Tue, 12 Sep 2023 15:03:47 +0100 Subject: [PATCH] Avoid an infinite loop in the interactive-auth test (#3722) Reading the test "should fire stateUpdated callback with error when a request fails" it looks like this would always cause an infinite loop between doRequest and statusUpdated calls. I don't know why this wasn't a problem until we updated Jest to v29.5.4, but after that point it was. This change should fix the test failures for the Jest upgrade PR: https://github.com/matrix-org/matrix-js-sdk/pull/3670 . --- spec/unit/interactive-auth.spec.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/spec/unit/interactive-auth.spec.ts b/spec/unit/interactive-auth.spec.ts index bd3c8e653..0d4177f55 100644 --- a/spec/unit/interactive-auth.spec.ts +++ b/spec/unit/interactive-auth.spec.ts @@ -560,7 +560,7 @@ describe("InteractiveAuth", () => { expect(ia.getChosenFlow()?.stages).toEqual([AuthType.Password]); }); - it("should fire stateUpdated callback if with error when encountered", async () => { + it("should fire stateUpdated callback with error when a request fails", async () => { const doRequest = jest.fn(); const stateUpdated = jest.fn(); @@ -578,15 +578,20 @@ describe("InteractiveAuth", () => { }, }); - // first we expect a call here + // StateUpdated should be called. We call submitAuthDict() to trigger a request ... + let firstTime = true; stateUpdated.mockImplementation((stage) => { expect(stage).toEqual(AuthType.Password); - ia.submitAuthDict({ - type: AuthType.Password, - }); + // Only trigger the request the first time, to avoid an infinite loop + if (firstTime) { + firstTime = false; + ia.submitAuthDict({ + type: AuthType.Password, + }); + } }); - // .. which should trigger a call here + // .. which which we then reject, so we can test the behaviour in that case. doRequest.mockRejectedValue(new MatrixError({ errcode: "M_UNKNOWN", error: "This is an error" })); await Promise.allSettled([ia.attemptAuth()]);