Hi @alex57,
Welcome to the Auth0 Community!
Your commitment to using cacheLocation="memory" is absolutely the recommended and most secure approach.
It’s crucial to understand that there are two distinct sessions at play. The first is the Application Session , which the Auth0 SDK stores in your application’s memory. As you’ve noted as well, this gets lost on every page refresh when using cacheLocation="memory". The Auth0 documentation explains this is expected behavior (Why is authentication lost after refreshing my SPA?).
The second is the persistent Auth0 Session , managed by a secure cookie on Auth0’s domain, which remembers the user’s login status with the authentication server itself. When your test starts on a fresh page, the Application Session is empty, so the SDK uses the persistent Auth0 Session to silently request new tokens. This ~1-2 second background process is what creates the race condition: your test script asserts for a logged-in UI before the SDK has finished establishing the new in-memory Application Session.
To resolve this, I recommend ensuring your SDK configuration is optimized for silent authentication reliability. The following settings are ideal for this, as useRefreshTokens can speed up token acquisition and useCookiesForTransactions helps prevent issues with browser privacy features.
const auth0Config = {
useRefreshTokens: true,
cacheLocation: 'memory',
useCookiesForTransactions: true,
sessionCheckExpiryDays: 1,
cookieDomain: '.yourdomain.com'
};
The most effective testing strategy involves logging in only once in a global setup task to establish the persistent Auth0 Session and reusing that authenticated browser state for all subsequent tests. With that in place, the critical change is to make your individual tests resilient to the silent authentication delay. Instead of asserting immediately, your test should wait for a UI element that confirms authentication is complete. For example, rather than checking for a dashboard element right away, instruct your test to wait for a specific welcome message like “Welcome, User!” or the main user avatar to become visible. Some testing frameworks have built-in web assertions such as toBeVisible() that can be used for such scenarios. This type of change allows the silent authentication process the time it needs to finish successfully before your test validates the outcome.
I hope this helps and if you have further inquiries please let me know!
Best regards,
Remus