Skip to content

Commit 78f741a

Browse files
authored
test: Fix nextjs tests build error (#919)
* test: Fix nextjs tests build error * test: Fix nextjs tests flakyness
1 parent 0c0fd67 commit 78f741a

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

examples/for-tests-nextjs/app/components/home.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { SessionAuthForNextJS } from "./sessionAuthForNextJS";
88

99
import { getServerComponentSessionWithoutClaimValidation, init } from "supertokens-auth-react/nextjs/ssr";
1010
import { ssrConfig } from "../config/ssr";
11-
import { useState } from "react";
1211
import { MiddlewareServerActionButton } from "./middlewareServerActionButton";
1312
import { ProtectedActionButton } from "./protectedActionButton";
1413
import { UnprotectedActionButton } from "./unprotectedActionButton";

examples/for-tests-nextjs/app/components/protectedActionButton.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ export const ProtectedActionButton = () => {
1616
onClick={async () => {
1717
const result = await protectedAction();
1818
setActionResult(result);
19-
}}>
19+
}}
20+
>
2021
getServerActionSession
2122
</button>
22-
<div data-testid="getServerActionSession-result">
23-
{actionResult?.status}:{actionResult?.userId}
24-
</div>
23+
{actionResult ? (
24+
<div data-testid="getServerActionSession-result">
25+
{actionResult?.status}:{actionResult?.userId}
26+
</div>
27+
) : null}
2528
<button onClick={() => setActionResult(undefined)} data-testid="getServerActionSession-reset">
2629
Reset
2730
</button>

test/end-to-end/ssr.nextjs.test.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,14 @@ function encodeFrontToken(frontToken) {
7777
}
7878

7979
async function didSessionRefresh(page) {
80+
await page.waitForNetworkIdle({ idleTime: 500, timeout: 10000 });
8081
const cookies = await page.cookies();
8182
const refreshTokenIndicatorCookie = cookies.find((c) => c.name === SSR_REFRESH_SESSION_INDICATOR_COOKIE_NAME);
8283
assert.equal(refreshTokenIndicatorCookie?.value, "true");
8384
}
8485

8586
async function wasSessionRevoked(page) {
87+
await page.waitForNetworkIdle({ idleTime: 500, timeout: 10000 });
8688
let newCookies = await page.cookies();
8789
const refreshTokenIndicatorCookie = newCookies.find((c) => c.name === SSR_REVOKE_SESSION_INDICATOR_COOKIE_NAME);
8890
assert.equal(refreshTokenIndicatorCookie?.value, "true");
@@ -96,9 +98,10 @@ async function hasServerComponentUserId(page) {
9698

9799
async function triggerServerAction(page) {
98100
const actionButton = await page.waitForSelector("[data-testid='getServerActionSession-button']");
99-
await actionButton.click();
100-
await new Promise((res) => setTimeout(res, 1000));
101-
const userIdElement = await page.waitForSelector("[data-testid='getServerActionSession-result']");
101+
await Promise.all([await actionButton.click(), await page.waitForNetworkIdle({ idleTime: 500, timeout: 10000 })]);
102+
const userIdElement = await page.waitForSelector("[data-testid='getServerActionSession-result']", {
103+
timeout: 10000,
104+
});
102105
const textContent = await userIdElement.evaluate((el) => el.textContent);
103106
const [status, userId] = textContent.split(":");
104107
return { userId: userId.trim(), status: status.trim() };

0 commit comments

Comments
 (0)