cypress: Cannot connect to Firestore emulator
Current behavior:
Connecting to the Firestore emulator within Cypress does work the way it does in a default browser. When attempting to open the app in cypress it shows the “Could Not reach Firestore backend” error. Here is a sample repo showing the issue - instructions to reproduce are in the README.
When connecting normally in the browser, data is loaded from both the Real Time Database and Firestore emulators.
This issue has also been mentioned by another user in this stackoverflow post.
Desired behavior:
Emulator should be able to connect the same way it does when being used in the browser.
Test code to reproduce
- Pull this sample repo
- Run
yarn install
- As described in the README run the following:
- Start the emulators:
yarn emulators
- In a new terminal window, seed the emulated databases with data:
yarn seed
- Start the app by running:
yarn start
- Visit
localhost:3000
in your browser - notice that data loads from both emulated databases - In another terminal window, open the test runner by running:
yarn test
- Run the projects tests by clicking on “Projects.spec.js”
- Start the emulators:
At this point the data does not load from the Firestore emulator within Cypress even though they will load in the browser (pictured below). This error appears in the console of the window opened by Cypress:
Versions
Happens with all cypress versions including:
- 3.8.3 - chrome and electron
- 4.0.1 - chrome and electron
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 15
- Comments: 18 (1 by maintainers)
If you are using emulators and include
experimentalForceLongPolling
, make sure to specify themerge
option as well:In our case
experimentalForceLongPolling
was overriding the emulator setup and cypress was trying to connect to the production DB.Thanks, this works for me.
For anyone looking to do this with @angular/fire I managed it this way in the
module.ts
where I am importingAngularFirestoreModule
, NB. I decided to check specifically for the presence of Cypress so that it uses normal settings for general development.I think this would possibly be an issue if you are using Angular Universal for SSR, but I’m not on this project and am not sure how you can get an SSR friendly
window
object in amodule.ts
file anyway!As noted in https://github.com/cypress-io/cypress/issues/2374 - passing
experimentalForceLongPolling: true
seems to make things work even in the provided repro.That said, I don’t think that is a good long term solution since it may be removed at some point (as noted in this issue on the Firebase JS SDK). It think it would be best to have full documentation around how this is handled in Cypress and ways to debug webchannel traffic in general (since Firestore is probably not the only setup like this)
Would it make sense to mention this somewhere in the official docs? I just wasted 2 days debugging this. Ran both into the @ANDREYDEN mentioned problem and the main,
experimentalForceLongPolling
problem.@ingusjan I believe you can use the
initializeFirestore
function like so:Also, you can pass emulator config that way, or as shown in the emulator setup docs using
connectFirestoreEmulator
. If you are using reactfire, you can leverage theuseInitFirestore
like so:Full example app of ^ available in generator-react-firebase react-firestore example with it’s SetupFirestore component (disclaimer - I’m the author of generator-react-firebase which is a tool for starting/scaffolding react/firebase projects)