Preventing Firebase Auth from persisting across Cypress e2e test runs
Asked Answered
T

1

6

I'm using Cypress for e2e tests on my Firebase web app, and I want test runs to always start fresh from an non-authenticated state.

How do I prevent Firebase Auth sessions from persisting across e2e test runs?

Currently, I have a logout() function call at the end of the tests. But if a test fails before that point, logout() often doesn't get called, and I have to manually log out before the next test run.

Tocharian answered 10/8, 2018 at 17:14 Comment(8)
I don't know anything about firebase, but if it is storing the auth session as a cookie you could call cy.clearCookie("nameOfCookie") in your beforeEach()Candelariacandelario
Unfortunately, that doesn't work. All cookies and local storage is dumped automatically by Cypress at the beginning of every test run anyway. Firebase must use other mechanisms to persist state.Tocharian
It is supposed to blow away the cookies between tests, but I have found that to not be reliable and have had to add that call in my tests to reset the session. Good luck!Candelariacandelario
If logout() is working for you, call it in afterEach(), see this document.Irwin
@RichardMatsen The document you linked to specifically says that devs shouldn't clean up in afterEach() and then it lists the reasons why. Hence, I'm trying to find the correct way to implement.Tocharian
The 'dangling state' paragraph is talking about moving from test to test with previous state, which seems like good practice. But you are saying that you specifically want to logout each test - so logout between tests.Irwin
Nowhere do I indicate a desire to logout between each test. "Test run" refers to a single run-through of all tests.Tocharian
Were you able to mock creating users with cypress?Elisavetgrad
M
25

Hi as mentioned you should clear firebase auth token in your beforeEach() method.

However depending on the version of firebase the auth token might be stored in different storage. Localstorage and indexdDB should be cleared.

indexedDB.deleteDatabase('firebaseLocalStorageDb');
Maulmain answered 11/8, 2018 at 18:4 Comment(2)
The question can be read a couple of ways, but it seems from clarification in comments that the token should be cleared in a before() not in beforeEach().Coldiron
There's a more generic solution to clearing the indexedDb here: It cleans all listed dbs.Vanny

© 2022 - 2024 — McMap. All rights reserved.