supertokens-auth-react icon indicating copy to clipboard operation
supertokens-auth-react copied to clipboard

Any easy way to mock out sessions?

Open nodiesBlade opened this issue 1 year ago • 3 comments

Hi, we're creating tests for one of our web applications, and there doesn't seem to be an easy way to mock out Sessions where SessionAuth HoC is respected.

While we can mock out the auth endpoints, I'm not so sure how to mock out the actual session response. If we cannot mock out the actual session, then the SessionAuth HoC will block us from testing other components. Plus, we'd like to actually test Auth flows

https://github.com/supertokens/supertokens-core/issues/115 recommends spinning up a local supertokenscore with inmemory db, however, this is really cumberstone and complex for our use case.

nodiesBlade avatar May 14 '23 04:05 nodiesBlade

What you can do is to override the session recipe functions on the frontend like doesSessionExist or getAccessTokenPayloadSecurely etc to return mocked information if it's testing env. This should solve your issue

rishabhpoddar avatar May 15 '23 07:05 rishabhpoddar

Do you have an example?

I'm not too familiar with the AuthSession component and what it uses for valid session.

nodiesBlade avatar May 15 '23 07:05 nodiesBlade

We don't have a specific example for this, but do checkout this: https://supertokens.com/docs/session/advanced-customizations/frontend-functions-override/usage

You need to override all the session recipe functions like doesSessionExist and return the appropriate values from the functions.

rishabhpoddar avatar May 15 '23 17:05 rishabhpoddar