Skip to content

Commit

Permalink
test: backport some 0.42 test stability improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
porcellus committed May 28, 2024
1 parent 0f116aa commit 42b97db
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 78 deletions.
35 changes: 14 additions & 21 deletions test/end-to-end/signin-rrdv5.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
waitForSTElement,
backendBeforeEach,
setEnabledRecipes,
waitForUrl,
} from "../helpers";
import fetch from "isomorphic-fetch";
import { SOMETHING_WENT_WRONG_ERROR } from "../constants";
Expand Down Expand Up @@ -290,9 +291,9 @@ describe("SuperTokens SignIn with react router dom v5", function () {

// Redirected to onSuccessFulRedirectUrl
const onSuccessFulRedirectUrl = "/dashboard";
let pathname = await page.evaluate(() => window.location.pathname);

// Session.doesSessionExist returns true, allow to stay on /dashboard
assert.deepStrictEqual(pathname, onSuccessFulRedirectUrl);
await waitForUrl(page, onSuccessFulRedirectUrl);

await Promise.all([
page.goto(`${TEST_CLIENT_BASE_URL}/dashboard-no-auth`),
Expand Down Expand Up @@ -330,8 +331,7 @@ describe("SuperTokens SignIn with react router dom v5", function () {
page.goto(`${TEST_CLIENT_BASE_URL}/auth`),
page.waitForNavigation({ waitUntil: "networkidle0" }),
]);
pathname = await page.evaluate(() => window.location.pathname);
assert.deepStrictEqual(pathname, "/auth");
await waitForUrl(page, "/auth");
cookies = await page.cookies();
assert.deepStrictEqual(cookies.length, 1);
assert.deepStrictEqual(cookies[0].name, "st-last-access-token-update");
Expand Down Expand Up @@ -435,9 +435,9 @@ describe("SuperTokens SignIn with react router dom v5", function () {

// Redirected to onSuccessFulRedirectUrl
const onSuccessFulRedirectUrl = "/dashboard";
let pathname = await page.evaluate(() => window.location.pathname);

// Session.doesSessionExist returns true, allow to stay on /dashboard
assert.deepStrictEqual(pathname, onSuccessFulRedirectUrl);
await waitForUrl(page, onSuccessFulRedirectUrl);

// Test that sessionInfo was fetched successfully using axios and fetch (i.e. Interceptors work)
const axiosUserId = await getUserIdWithAxios(page);
Expand All @@ -459,8 +459,7 @@ describe("SuperTokens SignIn with react router dom v5", function () {
// Logout
const logoutButton = await getLogoutButton(page);
await Promise.all([await logoutButton.click(), page.waitForNavigation({ waitUntil: "networkidle0" })]);
pathname = await page.evaluate(() => window.location.pathname);
assert.deepStrictEqual(pathname, "/auth");
await waitForUrl(page, "/auth");
cookies = await page.cookies();
assert.deepStrictEqual(cookies.length, 1);
assert.deepStrictEqual(cookies[0].name, "st-last-access-token-update");
Expand Down Expand Up @@ -494,8 +493,7 @@ describe("SuperTokens SignIn with react router dom v5", function () {
page.goto(`${TEST_CLIENT_BASE_URL}/auth?rid=emailpassword&redirectToPath=%2Fredirect-heree`),
page.waitForNavigation({ waitUntil: "networkidle0" }),
]);
const pathname = await page.evaluate(() => window.location.pathname);
assert.deepStrictEqual(pathname, "/redirect-heree");
await waitForUrl(page, "/redirect-heree");
});

it("Successful Sign In with redirect to, redirectToPath directly without trailing slash", async function () {
Expand Down Expand Up @@ -543,11 +541,7 @@ describe("SuperTokens SignIn with react router dom v5", function () {
const backBtn = await waitForSTElement(page, "[data-supertokens~=logoutButton]");
backBtn.click();

const redirectUrl = await page.evaluate(() => {
return window.location.pathname + window.location.search;
});

assert.deepStrictEqual(redirectUrl, "/dashboard");
await waitForUrl(page, "/dashboard", false);
});

describe("Successful Sign In with redirect to, with EmailPasswordAuth", async function () {
Expand All @@ -572,8 +566,8 @@ describe("SuperTokens SignIn with react router dom v5", function () {
submitFormReturnRequestAndResponse(page, SIGN_IN_API),
page.waitForNavigation({ waitUntil: "networkidle0" }),
]);
const pathname = await page.evaluate(() => window.location.pathname);
assert.deepStrictEqual(pathname, "/redirect-to-this-custom-path");
await page.waitForSelector(".sessionInfo-user-id");
await waitForUrl(page, "/redirect-to-this-custom-path");
assert.deepStrictEqual(consoleLogs, [
"ST_LOGS SESSION OVERRIDE ADD_FETCH_INTERCEPTORS_AND_RETURN_MODIFIED_FETCH",
"ST_LOGS SESSION OVERRIDE ADD_AXIOS_INTERCEPTORS",
Expand Down Expand Up @@ -611,8 +605,8 @@ describe("SuperTokens SignIn with react router dom v5", function () {
submitFormReturnRequestAndResponse(page, SIGN_IN_API),
page.waitForNavigation({ waitUntil: "networkidle0" }),
]);
const pathname = await page.evaluate(() => window.location.pathname);
assert.deepStrictEqual(pathname, "/CasE/Case-SensItive1-PAth");
await page.waitForSelector(".sessionInfo-user-id");
await waitForUrl(page, "/CasE/Case-SensItive1-PAth");
assert.deepStrictEqual(consoleLogs, [
"ST_LOGS SESSION OVERRIDE ADD_FETCH_INTERCEPTORS_AND_RETURN_MODIFIED_FETCH",
"ST_LOGS SESSION OVERRIDE ADD_AXIOS_INTERCEPTORS",
Expand Down Expand Up @@ -648,8 +642,7 @@ describe("SuperTokens SignIn with react router dom v5", function () {
page.waitForNavigation({ waitUntil: "networkidle0" }),
]);

const pathname = await page.evaluate(() => window.location.pathname);
assert.deepStrictEqual(pathname, "/dashboard");
await waitForUrl(page, "/dashboard");
});
});
});
Expand Down
33 changes: 12 additions & 21 deletions test/end-to-end/signin-rrdv6.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
backendBeforeEach,
getInvalidClaimsJSON,
expectErrorThrown,
waitForUrl,
} from "../helpers";
import fetch from "isomorphic-fetch";
import { SOMETHING_WENT_WRONG_ERROR } from "../constants";
Expand Down Expand Up @@ -274,9 +275,8 @@ describe("SuperTokens SignIn with react router dom v6", function () {

// Redirected to onSuccessFulRedirectUrl
const onSuccessFulRedirectUrl = "/dashboard";
let pathname = await page.evaluate(() => window.location.pathname);
// Session.doesSessionExist returns true, allow to stay on /dashboard
assert.deepStrictEqual(pathname, onSuccessFulRedirectUrl);
await waitForUrl(page, onSuccessFulRedirectUrl);

await Promise.all([
page.goto(`${TEST_CLIENT_BASE_URL}/dashboard-no-auth`),
Expand Down Expand Up @@ -314,8 +314,7 @@ describe("SuperTokens SignIn with react router dom v6", function () {
page.goto(`${TEST_CLIENT_BASE_URL}/auth`),
page.waitForNavigation({ waitUntil: "networkidle0" }),
]);
pathname = await page.evaluate(() => window.location.pathname);
assert.deepStrictEqual(pathname, "/auth");
await waitForUrl(page, "/auth");
cookies = await page.cookies();
assert.deepStrictEqual(cookies.length, 1);
assert.deepStrictEqual(cookies[0].name, "st-last-access-token-update");
Expand Down Expand Up @@ -421,9 +420,8 @@ describe("SuperTokens SignIn with react router dom v6", function () {

// Redirected to onSuccessFulRedirectUrl
const onSuccessFulRedirectUrl = "/dashboard";
let pathname = await page.evaluate(() => window.location.pathname);
// Session.doesSessionExist returns true, allow to stay on /dashboard
assert.deepStrictEqual(pathname, onSuccessFulRedirectUrl);
await waitForUrl(page, onSuccessFulRedirectUrl);

// Test that sessionInfo was fetched successfully using axios and fetch (i.e. Interceptors work)
const axiosUserId = await getUserIdWithAxios(page);
Expand All @@ -445,8 +443,7 @@ describe("SuperTokens SignIn with react router dom v6", function () {
// Logout
const logoutButton = await getLogoutButton(page);
await Promise.all([await logoutButton.click(), page.waitForNavigation({ waitUntil: "networkidle0" })]);
pathname = await page.evaluate(() => window.location.pathname);
assert.deepStrictEqual(pathname, "/auth");
await waitForUrl(page, "/auth");
cookies = await page.cookies();
assert.deepStrictEqual(cookies.length, 1);
assert.deepStrictEqual(cookies[0].name, "st-last-access-token-update");
Expand Down Expand Up @@ -481,8 +478,7 @@ describe("SuperTokens SignIn with react router dom v6", function () {
page.goto(`${TEST_CLIENT_BASE_URL}/auth?rid=emailpassword&redirectToPath=%2Fredirect-heree`),
page.waitForNavigation({ waitUntil: "networkidle0" }),
]);
const pathname = await page.evaluate(() => window.location.pathname);
assert.deepStrictEqual(pathname, "/redirect-heree");
await waitForUrl(page, "/redirect-heree");
});

it("Successful Sign In with redirect to, redirectToPath directly without trailing slash", async function () {
Expand Down Expand Up @@ -530,11 +526,7 @@ describe("SuperTokens SignIn with react router dom v6", function () {
const backBtn = await waitForSTElement(page, "[data-supertokens~=logoutButton]");
backBtn.click();

const redirectUrl = await page.evaluate(() => {
return window.location.pathname + window.location.search;
});

assert.deepStrictEqual(redirectUrl, "/dashboard");
await waitForUrl(page, "/dashboard", false);
});

it("Should not redirect to onFailureRedirections result if it's the current url and set the context", async function () {
Expand Down Expand Up @@ -720,8 +712,8 @@ describe("SuperTokens SignIn with react router dom v6", function () {
submitFormReturnRequestAndResponse(page, SIGN_IN_API),
page.waitForNavigation({ waitUntil: "networkidle0" }),
]);
const pathname = await page.evaluate(() => window.location.pathname);
assert.deepStrictEqual(pathname, "/redirect-to-this-custom-path");
await page.waitForSelector(".sessionInfo-user-id");
await waitForUrl(page, "/redirect-to-this-custom-path");
assert.deepStrictEqual(consoleLogs, [
"ST_LOGS SESSION OVERRIDE ADD_FETCH_INTERCEPTORS_AND_RETURN_MODIFIED_FETCH",
"ST_LOGS SESSION OVERRIDE ADD_AXIOS_INTERCEPTORS",
Expand Down Expand Up @@ -760,8 +752,8 @@ describe("SuperTokens SignIn with react router dom v6", function () {
submitFormReturnRequestAndResponse(page, SIGN_IN_API),
page.waitForNavigation({ waitUntil: "networkidle0" }),
]);
const pathname = await page.evaluate(() => window.location.pathname);
assert.deepStrictEqual(pathname, "/CasE/Case-SensItive1-PAth");
await page.waitForSelector(".sessionInfo-user-id");
await waitForUrl(page, "/CasE/Case-SensItive1-PAth");
assert.deepStrictEqual(consoleLogs, [
"ST_LOGS SESSION OVERRIDE ADD_FETCH_INTERCEPTORS_AND_RETURN_MODIFIED_FETCH",
"ST_LOGS SESSION OVERRIDE ADD_AXIOS_INTERCEPTORS",
Expand Down Expand Up @@ -798,8 +790,7 @@ describe("SuperTokens SignIn with react router dom v6", function () {
page.waitForNavigation({ waitUntil: "networkidle0" }),
]);

const pathname = await page.evaluate(() => window.location.pathname);
assert.deepStrictEqual(pathname, "/dashboard");
await waitForUrl(page, "/dashboard");
});
});
});
Expand Down
31 changes: 12 additions & 19 deletions test/end-to-end/signin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import {
backendBeforeEach,
getInputField,
isReact16,
waitForUrl,
} from "../helpers";
import fetch from "isomorphic-fetch";
import { SOMETHING_WENT_WRONG_ERROR } from "../constants";
Expand Down Expand Up @@ -102,6 +103,7 @@ describe("SuperTokens SignIn", function () {
page = await browser.newPage();
page.on("console", (consoleObj) => {
const log = consoleObj.text();
// console.log(log);
if (log.startsWith("ST_LOGS")) {
consoleLogs.push(log);
}
Expand Down Expand Up @@ -285,9 +287,8 @@ describe("SuperTokens SignIn", function () {

// Redirected to onSuccessFulRedirectUrl
const onSuccessFulRedirectUrl = "/dashboard";
let pathname = await page.evaluate(() => window.location.pathname);
// Session.doesSessionExist returns true, allow to stay on /dashboard
assert.deepStrictEqual(pathname, onSuccessFulRedirectUrl);
await waitForUrl(page, onSuccessFulRedirectUrl);

await Promise.all([
page.goto(`${TEST_CLIENT_BASE_URL}/dashboard-no-auth`),
Expand Down Expand Up @@ -325,8 +326,7 @@ describe("SuperTokens SignIn", function () {
page.goto(`${TEST_CLIENT_BASE_URL}/auth`),
page.waitForNavigation({ waitUntil: "networkidle0" }),
]);
pathname = await page.evaluate(() => window.location.pathname);
assert.deepStrictEqual(pathname, "/auth");
await waitForUrl(page, "/auth");
cookies = await page.cookies();
assert.deepStrictEqual(cookies.length, 1);
assert.deepStrictEqual(cookies[0].name, "st-last-access-token-update");
Expand Down Expand Up @@ -432,9 +432,8 @@ describe("SuperTokens SignIn", function () {

// Redirected to onSuccessFulRedirectUrl
const onSuccessFulRedirectUrl = "/dashboard";
let pathname = await page.evaluate(() => window.location.pathname);
// Session.doesSessionExist returns true, allow to stay on /dashboard
assert.deepStrictEqual(pathname, onSuccessFulRedirectUrl);
await waitForUrl(page, onSuccessFulRedirectUrl);

// Test that sessionInfo was fetched successfully using axios and fetch (i.e. Interceptors work)
const axiosUserId = await getUserIdWithAxios(page);
Expand All @@ -456,8 +455,7 @@ describe("SuperTokens SignIn", function () {
// Logout
const logoutButton = await getLogoutButton(page);
await Promise.all([await logoutButton.click(), page.waitForNavigation({ waitUntil: "networkidle0" })]);
pathname = await page.evaluate(() => window.location.pathname);
assert.deepStrictEqual(pathname, "/auth");
await waitForUrl(page, "/auth");
cookies = await page.cookies();
assert.deepStrictEqual(cookies.length, 1);
assert.deepStrictEqual(cookies[0].name, "st-last-access-token-update");
Expand Down Expand Up @@ -525,11 +523,7 @@ describe("SuperTokens SignIn", function () {
const backBtn = await waitForSTElement(page, "[data-supertokens~=logoutButton]");
backBtn.click();

const redirectUrl = await page.evaluate(() => {
return window.location.pathname + window.location.search;
});

assert.deepStrictEqual(redirectUrl, "/dashboard");
await waitForUrl(page, "/dashboard", false);
});

it("Successful Sign In with redirect to, redirectToPath directly", async function () {
Expand Down Expand Up @@ -660,8 +654,8 @@ describe("SuperTokens SignIn", function () {
submitFormReturnRequestAndResponse(page, SIGN_IN_API),
page.waitForNavigation({ waitUntil: "networkidle0" }),
]);
const pathname = await page.evaluate(() => window.location.pathname);
assert.deepStrictEqual(pathname, "/redirect-to-this-custom-path");
await page.waitForSelector(".sessionInfo-user-id");
await waitForUrl(page, "/redirect-to-this-custom-path");
assert.deepStrictEqual(consoleLogs, [
"ST_LOGS SESSION OVERRIDE ADD_FETCH_INTERCEPTORS_AND_RETURN_MODIFIED_FETCH",
"ST_LOGS SESSION OVERRIDE ADD_AXIOS_INTERCEPTORS",
Expand Down Expand Up @@ -700,8 +694,8 @@ describe("SuperTokens SignIn", function () {
submitFormReturnRequestAndResponse(page, SIGN_IN_API),
page.waitForNavigation({ waitUntil: "networkidle0" }),
]);
const pathname = await page.evaluate(() => window.location.pathname);
assert.deepStrictEqual(pathname, "/CasE/Case-SensItive1-PAth");
await page.waitForSelector(".sessionInfo-user-id");
await waitForUrl(page, "/CasE/Case-SensItive1-PAth");
assert.deepStrictEqual(consoleLogs, [
"ST_LOGS SESSION OVERRIDE ADD_FETCH_INTERCEPTORS_AND_RETURN_MODIFIED_FETCH",
"ST_LOGS SESSION OVERRIDE ADD_AXIOS_INTERCEPTORS",
Expand Down Expand Up @@ -738,8 +732,7 @@ describe("SuperTokens SignIn", function () {
page.waitForNavigation({ waitUntil: "networkidle0" }),
]);

const pathname = await page.evaluate(() => window.location.pathname);
assert.deepStrictEqual(pathname, "/dashboard");
await waitForUrl(page, "/dashboard");
});
});
});
Expand Down
60 changes: 43 additions & 17 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,28 +67,54 @@ export async function waitFor(ms) {
*/

export async function waitForSTElement(page, selector, inverted = false) {
await page.waitForSelector(ST_ROOT_SELECTOR);
const res = await page.waitForFunction(
(elementSelector, rootSelector, inverted) => {
const root = document.querySelector(rootSelector);
if (!root || !root.shadowRoot) {
return false;
const start = Date.now();
while (start > Date.now() - 30000) {
try {
await page.waitForSelector(ST_ROOT_SELECTOR);
const res = await page.waitForFunction(
(elementSelector, rootSelector, inverted) => {
const root = document.querySelector(rootSelector);
if (!root || !root.shadowRoot) {
return false;
}
if (elementSelector === undefined) {
return true;
}
const elem = root.shadowRoot.querySelector(elementSelector);
return inverted ? elem === null : elem;
},
{ polling: 50 },
selector,
ST_ROOT_SELECTOR,
inverted
);
if (res) {
return res.asElement();
}
if (elementSelector === undefined) {
return true;
return res;
} catch (ex) {
if (start >= Date.now() - 30000) {
throw ex;
}
const elem = root.shadowRoot.querySelector(elementSelector);
return inverted ? elem === null : elem;
console.log("Caught exception in waitForSTElement, but retrying until timeout", ex);
}
}
throw new Error(`Waiting for ${selector} to ${inverted ? "disappear" : "appear"} timed out`);
}

export function waitForUrl(page, url, onlyPath = true) {
return page.waitForFunction(
(pathname, onlyPath) => {
return (
(onlyPath
? window.location.pathname
: window.location.pathname + window.location.search + window.location.hash) === pathname
);
},
{ polling: 50 },
selector,
ST_ROOT_SELECTOR,
inverted
url,
onlyPath
);
if (res) {
return res.asElement();
}
return res;
}

export async function waitForText(page, selector, text, timeout = 10000, pollDelay = 50) {
Expand Down

0 comments on commit 42b97db

Please sign in to comment.