From c4868b851a6518f1073f0250b8dac077409fd043 Mon Sep 17 00:00:00 2001 From: Deepjyoti Barman Date: Mon, 7 Oct 2024 13:29:57 +0530 Subject: [PATCH] Add test for core being down and error handler called in next --- test/nextjs.test.js | 55 +++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/test/nextjs.test.js b/test/nextjs.test.js index 6883fc35f..ac334555d 100644 --- a/test/nextjs.test.js +++ b/test/nextjs.test.js @@ -958,30 +958,37 @@ if (major >= 18) { } }); - // it("should go to next error handler when withSession is called without core", async function () { - // const tokens = await getValidTokensAfterSignup({ tokenTransferMethod: "header" }); - - // const authenticatedRequest = new NextRequest("http://localhost:3000/api/get-user", { - // headers: { - // Cookie: `sAccessToken=${tokens.access}`, - // }, - // }); - - // // Manually kill to get error when withSession is called - // await killAllSTCoresOnly(); - - // const authenticatedResponse = await withSession(authenticatedRequest, async (err, session) => { - // if (err) return NextResponse.json(`CUSTOM_ERROR: ${err}`, { status: 500 }); - // return NextResponse.json({ - // userId: session.getUserId(), - // sessionHandle: session.getHandle(), - // accessTokenPayload: session.getAccessTokenPayload(), - // }); - // }); - // const responseJSON = await authenticatedResponse.json(); - // console.log(responseJSON); - // assert.strictEqual(responseJSON, {}, "test"); - // }); + it("should go to next error handler when withSession is called without core", async function () { + const tokens = await getValidTokensAfterSignup({ tokenTransferMethod: "header" }); + + const authenticatedRequest = new NextRequest("http://localhost:3000/api/get-user", { + headers: { + Cookie: `sAccessToken=${tokens.access}`, + }, + }); + + // Manually kill to get error when withSession is called + await killAllSTCoresOnly(); + + const authenticatedResponse = await withSession( + authenticatedRequest, + async (err, session) => { + if (err) return NextResponse.json(`CUSTOM_ERROR: ${err}`, { status: 500 }); + return NextResponse.json({ + userId: session.getUserId(), + sessionHandle: session.getHandle(), + accessTokenPayload: session.getAccessTokenPayload(), + }); + }, + { checkDatabase: true } + ); + const responseJSON = await authenticatedResponse.json(); + assert.strictEqual( + responseJSON, + "CUSTOM_ERROR: Error: No SuperTokens core available to query", + "should return custom error from next error handler" + ); + }); }); describe("session refresh test", async () => {