cypress-firebase icon indicating copy to clipboard operation
cypress-firebase copied to clipboard

Cypress V10 - Unable to detect a Project Id in the current environment

Open pedrocarloto opened this issue 2 years ago • 6 comments

Describe the bug

I am doing this in a clean installation of cypress V10.1.0 and cypress-firebase V2.2.1.

I have went through all the steps described on the readme file to execute a simple test on firestore, but unfortunatelly I keep getting the error:

cy.task('callFirestore') failed with the following error: Unable to detect a Project Id in the current environment.

I am trying to reach my own firebase project. (First screenshot)

The same is true if I try to execute the example code provided in cypress-firebase, but I get a slight different error:

cy.task('callFirestore') failed with the following error: Could not load the default credentials.

(Second screenshot)

To Reproduce Steps to reproduce the behavior:

Main problem: Implement the code described on the Readme file in section "Setup" Connect it to your won firebase project with your credentials.

Secondary problem

  1. On the cypress-firebase code
  2. go to examples/basic
  3. run: yarn test:open
  4. run Firestore tests

Expected behavior/code The test would execute and pass.

Additional Context/Screenshots

First: image

Second: image

pedrocarloto avatar Jun 21 '22 14:06 pedrocarloto

@prescottprue, in theory I don't need a serviceAccount.json to be able to connect to firestore, correct? In the readme file that's not clear. (I will make some suggestions to the Readme file with a PR when I get this to work :) )

Anyway I tried adding a serviceAccount.json to my root folder and I still get the same error. Thank you.

pedrocarloto avatar Jun 22 '22 11:06 pedrocarloto

Could you provide the cypress.config.ts/js from your project?

andershoegh avatar Jun 23 '22 14:06 andershoegh

Of course!


import admin from "firebase-admin";
import { defineConfig } from "cypress";
import { plugin as cypressFirebasePlugin } from "cypress-firebase";

const cypressConfig = defineConfig({
  e2e: {
    baseUrl: "http://localhost:3000",
    supportFile: "cypress/support/e2e.ts",
    experimentalSessionAndOrigin: true,
    setupNodeEvents(on, config) {
      cypressFirebasePlugin(on, config, admin);
      // e2e testing node events setup code
    }
  }
});

export default cypressConfig;

cypress/support/e2e.ts :

import "./commands";

cypress/support/commands.ts

/// <reference types="cypress" />

import firebase from "firebase/compat/app";
import "firebase/compat/auth";
import "firebase/compat/database";
import "firebase/compat/firestore";
import { attachCustomCommands } from "cypress-firebase";

const fbConfig = {
  apiKey: Cypress.env("REACT_APP_FIREBASE_API_KEY"),
  authDomain: Cypress.env("REACT_APP_FIREBASE_AUTH_DOMAIN"),
  databaseURL: Cypress.env("REACT_APP_FIREBASE_DATABASE_URL"),
  projectId: Cypress.env("REACT_APP_FIREBASE_PROJECT_ID"),
  storageBucket: Cypress.env("REACT_APP_FIREBASE_STORAGE_BUCKET"),
  messagingSenderId: Cypress.env("REACT_APP_FIREBASE_MESSAGING_SENDER_ID"),
  appId: Cypress.env("REACT_APP_FIREBASE_APP_ID"),
  measurementId: Cypress.env("REACT_APP_FIREBASE_MEASUREMENT_I")
};

firebase.initializeApp(fbConfig);

attachCustomCommands({ Cypress, cy, firebase });

Thank you a lot for the help.

pedrocarloto avatar Jun 23 '22 14:06 pedrocarloto

What worked for me was setting a project ID when registering the node events like so:

cypressFirebasePlugin(on, config, admin, { projectId: "PROJECT_ID_FROM_CONFIG", });

andershoegh avatar Jun 23 '22 15:06 andershoegh

Oh, nice! It also worked for me. Thank you so much.

Then I got the error:

Could not load the default credentials.

But I solved it by adding to those optional parameters:

credential: admin.credential.cert(require("../../serviceAccount.json"))

Anyway I feel there is a problem with the package, since using the override you suggest should be optional, no?

pedrocarloto avatar Jun 23 '22 16:06 pedrocarloto

@pedrocarloto I would agree that this is still an issue that there is not clear messaging from the lib or documentation about it. Default credentials should be automatically used if they are available (i.e. GOOGLE_APPLICATION_CREDENTIALS environment variable points to the path instead of you requiring directly like you did) - or if in Github actions than the gcloud steps export default credentials

prescottprue avatar Jun 23 '22 17:06 prescottprue

What worked for me was setting a project ID when registering the node events like so:

cypressFirebasePlugin(on, config, admin, { projectId: "PROJECT_ID_FROM_CONFIG", });

FYI this solved my (not working cy.callFirestore('update')) issue https://github.com/prescottprue/cypress-firebase/issues/345#issuecomment-1284632330 🙏

AurelienLoyer avatar Oct 19 '22 22:10 AurelienLoyer

:tada: This issue has been resolved in version 2.2.4 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] avatar Dec 31 '22 01:12 github-actions[bot]

:tada: This issue has been resolved in version 2.3.0-alpha.2 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] avatar Dec 31 '22 03:12 github-actions[bot]