Is it possible to use Cypress e2e testing with a firebase auth project?

后端 未结 6 1419
再見小時候
再見小時候 2021-02-19 03:16

I am exploring Cypress for e2e testing, looks like great software. The problem is Authentication, the Cypress documentation explains why using the UI is very bad here.

So

6条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-19 03:39

    This is certainly a hack but to get around the login part for the app I am working on I use the beforeEach hook to login to the application.

    beforeEach(() => {
      cy.resetTestDatabase().then(() => {
        cy.setupTestDatabase();
      });
    });
    

    Which is derived from my helper functions.

    Cypress.Commands.add('login', () => {
      return firebase
        .auth()
        .signInWithEmailAndPassword(Cypress.env('USER_EMAIL'), Cypress.env('USER_PASSWORD'));
    });
    
    Cypress.Commands.add('resetTestDatabase', () => {
      return cy.login().then(() => {
        firebase
          .database()
          .ref(DEFAULT_CATEGORIES_PATH)
          .once('value')
          .then(snapshot => {
            const defaultCategories = snapshot.val();
            const updates = {};
            updates[TEST_CATEGORIES_PATH] = defaultCategories;
            updates[TEST_EVENTS_PATH] = null;
            updates[TEST_STATE_PATH] = null;
            updates[TEST_EXPORT_PATH] = null;
    
            return firebase
              .database()
              .ref()
              .update(updates);
          });
      });
    });
    

    What I would like to know is how the information coming back from firebase ultimately gets saved to localStorage. I don't really have an answer to this but it works. Also, the app uses .signInWithPopup(new firebase.auth.GoogleAuthProvider()) whereas above it signs in with email and password. So I am kind of shortcutting the signin process only because cypress has the CORS limitation.

提交回复
热议问题